Implementing cross-browser with Selenium

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 !!

2 thoughts on “Implementing cross-browser with Selenium

  1. Hey It’s a good start there with trying to run your code on different browsers.
    I had started exactly the same way when I started learning WebDriver.
    The approach is good as that is exactly what we do in a framework, we try and have separate functions handling each browser. The most common places to look out for – Launching the browser, Handling alerts and popups, Navigation between frames and web pages, Capturing screenshots and more!

    Do let me know if you need more information.
    Cheers,
    Ady!

  2. If needed, here is a .NET port of the same code.

    public class WebDriverFactory
    {
    private static readonly Lazy<Dictionary> WebDrivers = new Lazy<Dictionary>();

    public static IWebDriver GetWebDriver(WebDriverType webDriverType)
    {
    if (!WebDrivers.Value.ContainsKey(webDriverType))
    {
    IWebDriver driverToAdd;
    switch (webDriverType)
    {
    case WebDriverType.Chrome:
    var options = new ChromeOptions();
    options.AddArgument(“–start-maximized”);
    driverToAdd = new ChromeDriver(options);
    break;

    case WebDriverType.InternetExplorer:
    driverToAdd = new InternetExplorerDriver();
    //driverToAdd.Manage().Window.Maximize();
    break;

    case WebDriverType.Firefox:
    driverToAdd = new FirefoxDriver();
    driverToAdd.Manage().Window.Maximize();
    break;

    case WebDriverType.Safari:
    driverToAdd = new SafariDriver();
    driverToAdd.Manage().Window.Maximize();
    break;

    case WebDriverType.Phantom:
    driverToAdd = new PhantomJSDriver();
    break;

    case WebDriverType.Android:
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.SetCapability(“automationName”, “Appium”);
    capabilities.SetCapability(CapabilityType.BrowserName, “Browser”);
    capabilities.SetCapability(CapabilityType.Platform, “Android”);
    capabilities.SetCapability(CapabilityType.Version, “4.4.2”);
    capabilities.SetCapability(“app”, @”C:\Users\admin\AppData\Local\Android\sdk\platform-tools\chrome.apk”);
    capabilities.SetCapability(“app-package”, “com.android.chrome”);
    capabilities.SetCapability(“platformName”, “Android”);
    capabilities.SetCapability(“deviceName”, “Nexus 7”);
    driverToAdd = new RemoteWebDriver(new Uri(“http://localhost:4723/wd/hub/”), capabilities);
    break;

    default:
    throw new NotSupportedException(string.Format(“Driver {0} is not currently supported”, webDriverType));
    }
    WebDrivers.Value.Add(webDriverType, driverToAdd);
    }

    return WebDrivers.Value[webDriverType];
    }

    When coupled with an app.config xlm file it allows the end user to specify the browser they wish to run the test on. Let me know if you require the code for implementation.

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.