Hi guys !!
I wrote a simple example of howΒ we can implementing cross browser code.
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class CrossBrowsers {
public WebDriver Select(WebDriver driver) throws IOException, Exception {
try {
//Switch browsers
switch (driver.getBrowser()) {
case "firefox": {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 0);
driver.setDriver(new FirefoxDriver(profile));
driver.getEnvironment();
break;
}
case "ie": {
DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
Proxy proxy = new Proxy();
proxy.setNoProxy(environment);
cap.setCapability(CapabilityType.PROXY, proxy);
cap.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
cap.setCapability("ignoreZoomSetting", true);
cap.setCapability("ignoreProtectedModeSettings" , true);
cap.setCapability("initialBrowserUrl", driver.getEnvironment());
File file = new File("drivers//IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getCanonicalPath());
driver.setDriver(new InternetExplorerDriver(cap));
driver.get(environment);
break;
}
case "chrome": {
DesiredCapabilities cap = DesiredCapabilities.chrome();
Proxy proxy = new Proxy();
proxy.setNoProxy(driver.getEnvironment());
cap.setCapability(CapabilityType.PROXY, proxy);
cap.setCapability("chrome.switches", Arrays.asList("--disable-web-security"));
cap.setCapability("chrome.switches", Arrays.asList("--safebrowsing-disable-download-protection"));
cap.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
File file = new File("drivers//chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getCanonicalPath());
driver.setDriver(new ChromeDriver(cap));
driver.getEnvironment();
break;
}
}
} catch (IOException ex) {
System.print.out(ex);
}
return driver;
}
}
If you have any question or suggestion, please just write below onΒ the comments.
Have a nice week everyone, Bye !!