Browser, Chrome, collabration, Headless browser, Safari, Selenium

Dealing with multiple browsers in selenium

As technology grows we have to deal with multiple things. Things which are similar in behaviour and functioning but still different. One of the case comes up when we have to deal with multiple browser in selenium.

Here is the code snippet which can help you to initiate a browser seamlessly when added in project utility.

How to work with browsers other than default(Firefox) browser?

public static WebDriver getDriver() throws InterruptedException, IOException {
		OS = System.getProperty("os.name").toLowerCase();//Check for Operating system name
		WebDriver driver = ThreadDriver.get();
		String workingDir = System.getProperty("user.dir");
		if (driver == null) //Check for default browser
{
			if (browserType.equals("firefox")) {
				if (isWindows()) {
//This will set the property in the OS/System you are working on					System.setProperty("webdriver.gecko.driver", workingDir + "//driver//geckodriver.exe");
				} else if (isMac()) {
					System.setProperty("webdriver.gecko.driver", workingDir + "//driver//geckodriver.exe");
				} else if (isLinux()) {
//Setting capabilities to handle functioning like JS handling, alerts, certificates, etc 					System.setProperty("webdriver.gecko.driver", "/usr/bin/geckodriver");

				}
				DesiredCapabilities capabilities = DesiredCapabilities.firefox();
				capabilities.setCapability("marionette", true);
				capabilities.setBrowserName("firefox");
				capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
				driver = new EventFiringWebDriver(new FirefoxDriver());
				FirefoxProfile profile = new FirefoxProfile();
				profile.setAssumeUntrustedCertificateIssuer(false);
				profile.setAcceptUntrustedCertificates(false);
				profile.setPreference("dom.disable_beforeunload", true);
				profile.setPreference("javascript.enabled", true);
				ThreadDriver.set(driver);
				getDriver().manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
				wait = new WebDriverWait(driver, 30);
			}
//Repeat the same for other browsers
			if (browserType.equals("chrome")) {
				if (isWindows()) {
					System.setProperty("webdriver.chrome.driver", workingDir + "//driver//chromedriver.exe");
				} else if (isMac()) {
					System.setProperty("webdriver.chrome.driver", workingDir + "//driver//chromedriver");
				} else if (isLinux()) {
					System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
				}
				DesiredCapabilities capabilities = DesiredCapabilities.chrome();
				ChromeOptions options = new ChromeOptions();
				options.addArguments("test-type");
				options.addArguments("--start-maximized");
				options.addArguments("--disable-web-security");
				options.addArguments("--allow-running-insecure-content");
				options.addArguments("--js-flags=--expose-gc");
				options.addArguments("--enable-precise-memory-info");
				options.addArguments("--disable-popup-blocking");
				options.addArguments("--disable-default-apps");
				options.addArguments("test-type=browser");
				options.addArguments("disable-infobars");
				options.addArguments("--incognito");
				options.addArguments("--use-fake-ui-for-media-stream");
				capabilities.setCapability("chrome.binary", "./src//lib//chromedriver");
//Setting multiple capabilities as per the requirements				capabilities.setCapability(ChromeOptions.CAPABILITY, options);
				driver = new ChromeDriver(options);

				ThreadDriver.set(driver);
				if (isMac()) {
					maximizeScreen();
				}
				getDriver().manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
				wait = new WebDriverWait(driver, 30);
			}
			if (browserType.equals("ie")) {
				System.setProperty("webdriver.ie.driver", workingDir + "//driver//IEDriverServer.exe");
				driver = new EventFiringWebDriver(new InternetExplorerDriver());
				ThreadDriver.set(driver);
				getDriver().manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
				wait = new WebDriverWait(driver, 30);
			}
			if (browserType.equals("safari")) {
				System.setProperty("webdriver.safari.driver", "/driver/SafariDriver.safariextz");
				System.setProperty("webdriver.safari.noinstall", "true");
				DesiredCapabilities desiredCapabilities = DesiredCapabilities.safari();
				SafariOptions safariOptions = new SafariOptions();
				safariOptions.setUseCleanSession(true);
				safariOptions.getUseCleanSession();
				safariOptions.setUseCleanSession(true);
				desiredCapabilities.setCapability(SafariOptions.CAPABILITY, safariOptions);
				driver = new EventFiringWebDriver(new SafariDriver());
				ThreadDriver.set(driver);
				getDriver().manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
				wait = new WebDriverWait(driver, 30);
			}
		}
		return driver;
	}
Advertisements

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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

w

Connecting to %s