Get Current Webdriver as a variable


I noticed the link on here is broken:

and I was replying to a topic here:

This prompted me to add this to tips and tricks. I test in multiple environments sometimes so my screenshots which I save to a shared drive I amend the driver onto. This way one test that generates 10 images has a driver attached. So if I test in 3 I have 30 unique screenshots that tell me what browser was used.

Here is my code (modified from research online, not from scratch)

import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement

	public static getCurrentWebDriver(){
		WebDriver driver = DriverFactory.getWebDriver();
		String currentDriver = driver.toString();
		String[] browser = currentDriver.split("\\:")
		currentDriver = browser[0]
		return currentDriver;

I set it up under keywords but as a method and call it in like this:

String currentWebDriver = MiscMethods.getCurrentWebDriver()


Hey Blaze

That makes perfect sense in theory. But what about in practice? Any chance you can include a screen shot so we can see what you’re talking about?


sure it gets pretty intensive the way I set it up so I can reuse everything. All my test cases have a master file I import so when I change environments or screenshot locations I change it in one method and it affects all my test cases.

I’ll edit out our company data below and replace it with “program”

String programName_FilePath = program_FilePath.getProgramNameScreenShotFilePath()
String screenShotPath = programName_FilePath + 'nameForSection\\nameOfThisTest\\'

by importing that method above I can call my method which basically has a hard coded path of where I want to store my screenshots. Then I amend this in the test case with the name of the test and a folder for the suite. So after this I have “screenShotPath” which include the universal path and the name of the test case. I do this in every test case.

next I use the capture driver:

String currentWebDriver = MiscMethods.getCurrentWebDriver()

Then I do this whenever I capture a screenshot (again company data removed).

WebUI.takeScreenshot(screenShotPath + currentWebDriver + ' - 01 name of screen shot.jpg', FailureHandling.OPTIONAL)

so by combining my two strings I end up create all screenshots in a single folder with the webdriver in the name. I start with webdriver so all the chrome ones are together and all the firefoxes etc. I also use a number at the start of each screenshot to keep these in order. though I use the report that gets emailed I can also see that driver in those emails and I like to keep these screenshots in a location I can reference after a run. So I can use them for defects or test artifacts. Here is an example of the out put from some of these.

First the suite, I create a master suite with all tests and then add each of these suite into an all environment suite like this:

Then the screenshots are saved as follows:

That is three tests. Here is one set of three screenshots for a test in both chrome and firefox


Now I could add in a


If I wanted to see it during. Looks like though with the emails it tells you the browser and sends it out per set which is nice. This is mostly for if you want it in the saved screenshots. Which I do.


Commendations Blaze. I like the “due diligence” you’ve applied here.