Selenium – Which driver’s instance is my WebDriver?

Readers, while working on web automation using Selenium, you must have come across situations where you need to handle something differently in the code based on whether your Selenium WebDriver is an instance of Chrome or Edge or Internet Explorer or Safari etc.

One method is to pass on an argument to a function which explicitly states or indicates which browser is being used currently. Let me share another method. I am going to talk about how you can get the WebDriver driver and straightaway decide which driver’s instance is it – Chrome, Edge, Firefox, Safari etc.

I am going to give you an example code in Java too which you can execute and use.

First let us remind ourselves that WebDriver is an interface. ChromeDriver, FirefoxDriver, EdgeDriver are classes which implement WebDriver. So, the idea is to check if the WebDriver is an instance of any of the driver classes which implement it. Please go through the code which will make things clear for you.

I hope the code is useful for you.

package test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
* This is a demo of how it can be identified that which driver's instance is WebDriver.
* Is it chrome/ edge/ firefox and so on.
* The main class calls the driver based on enum from an array and uses another function to print the instance of the driver.
* @author swagatasengupta
*
*/
public class InstanceOfDemo {

public static enum Browser{
CHROME,
EDGE,
FIREFOX
}

/**
* uses an array to store browser enumerators
* calls a function passing the enums one by one and gets driver instance
* calls a function passing the driver instance to get it detected
* quits the driver when done
* @param args
*/
public static void main(String[] args) {

Browser[] browserList= {Browser.CHROME, Browser.FIREFOX, Browser.EDGE};

for(Browser browser : browserList) {
System.out.println("Getting driver for " + browser);
WebDriver driver = getWebDriver(browser);
printInstance(driver);
System.out.println("Quitting for " + browser);
driver.quit();
}

}

/**
* returns the driver instance based on enumerator
* Sets path of the drivers
* PLEASE CHANGE THE PATH AS PER THE LOCATION OF RESPECTIVE DRIVER IN YOUR MAHICNE
* @param browser
* @return
*/
public static WebDriver getWebDriver(Browser browser) {

switch(browser){
case CHROME:
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
return new ChromeDriver();
case FIREFOX:
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
return new FirefoxDriver();
case EDGE:
System.setProperty("webdriver.edge.driver", "C:\\MicrosoftWebDriver.exe");
return new EdgeDriver();
default: return null;
}

}
public static void printInstance(WebDriver driver) {

if(driver instanceof ChromeDriver) {
System.out.println("Chrome Driver detected.");
//DO WHAT YOU WANT FOR CHROME
return;
}
if(driver instanceof FirefoxDriver) {
System.out.println("Firefox Driver detected.");
//DO WHAT YOU WANT FOR FIRE FOX
return;
}
if(driver instanceof EdgeDriver) {
System.out.println("Edge Driver detected.");
//DO WHAT YOU WANT FOR EDGE
return;
}

};
}

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 )

Connecting to %s