I'm working on building out a testing framework for some sites using selenium webdriver, and my goal is to have a number of drivers running the same tests concurrently (aka a firefoxdriver, an internetexplorerdriver, and a chromedriver all running at the same time with some shared resources). However, I'm having trouble with logging which driver is doing what. I'm passing the drivers through a lot of my code, but as far as I can tell a webdriver has no knowledge of what specific type of driver it is. Is there any way to ask a webdriver element what it was instantiated as?
Asked
Active
Viewed 5,114 times
3 Answers
9
You can use instanceof
like
if( driver instanceof FirefoxDriver) {
System.out.println("Firefox it is!!");
}
else if( driver instanceof ChromeDriver) {
System.out.println("Chrome it is!!");
}
// and so on
For more details : What is the 'instanceof' operator used for?
-
Oh my god. *smacks head against desk*. Thanks. – iamthereplicant May 14 '15 at 15:07
-
What if our tests are executing remotely using RemoteWebDriver? – san1deep2set3hi Jul 03 '17 at 05:04
-
@san1deep2set3hi use `capabilities.getBrowserName()` – Ajinkya Jul 03 '17 at 06:56
0
/******************************************************************************************
* Name: getBrowserDetails | Description: Gets Browser Name and Version
******************************************************************************************/
public String getBrowserDetails() throws Exception {
Capabilities caps = ((RemoteWebDriver)BaseTest.driver).getCapabilities();
String browserName = caps.getBrowserName();
String browserVersion = caps.getVersion();
String browser = (browserName + " " + browserVersion).toUpperCase();
return browser;
}

Vasudev Reddy
- 33
- 11
-
Could we have some description of how this answers the question? – Stephen Rauch Oct 03 '17 at 17:58
-
This returns you the Browser name , based on the browser name we can decide what type of driver it is – Vasudev Reddy Oct 04 '17 at 18:03
0
If using instanceof
, be sure to also consider org.openqa.selenium.WrapsDriver
so as to handle EventFiringWebDriver
.

Jesse Glick
- 24,539
- 10
- 90
- 112