Implementing cross-browser with Selenium

07 January, 2015
Simple code to launch cross browser using WebDriver


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;
    }
}




No comments:

Post a Comment