Selenium Driver Class, Full Explanation

Hasan Ozyer
6 min readAug 26, 2023

--

Heello again people! Normally this publications are for noob junior test automation engineers like me who feels stuck about overwhelming Test Automation Concepts and tries to understand them, but lately i got a notification says that one of the biggest Test Automation author in this platform, dropped a follow on me. That encorugaes a lot and i want to thank him specifically before starting this article.

Today’s article, i will try to explain the singleton driver class model to younger me when i was having trouble learning it. This is crucial for anyone who is trying to become a real test automation engineer. I remember all of my questions when i was first learning this class(It was not a long time ago lol). Bear with me passionate QA Automation Engineer wannabe Hasan because i will answer all of your questions.

Let’s start with this. The below Singleton driver class model is today’s topic.

public class Driver {

private static WebDriver driver;

public static WebDriver getDriver() {
return getDriver(Browsers.CHROME);
}


public static WebDriver getDriver(Browsers browser) {

switch (browser) {
case EDGE -> {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
}
case FIREFOX -> {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
case SAFARI -> {
WebDriverManager.safaridriver().setup();
driver = new SafariDriver();
}
default -> {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
}
return driver;
}


}

This is what i will be explaining in this article. My approach will be to answer like it is a Q&A and tell which problem is solved by every feature of this class. All these questions were actual questions of mine when i was first learning it.

Q: Why I need to setup my driver in driver class while i can just create it on my own test class?

A: It is because to avoid confusion on hundreds of tests in your project. Not in every test you can setup your driver. It is against the most important rule of the coding DRY(Don’t Repeat Yourself).

A2: Even though you can come up with an arguments like writing a setupDriver method on your class and recalling on the test method, is just an extra work for every class you have, and tbh it just shows you are amateur(yeah!!). This is why we set our driver only once, when they have been needed with switch-case.

Q: Why do i have to check the driver with if condition wheter it is null or not? I don’t get it?!

A: Because it check’s wheter or not you have a driver working for you. I will give an example without the if statement. Let’s say you have a class like this;

public class Test1 {
@Test
public void test1() {
WebDriver driver = Driver.getDriver(Browsers.EDGE);
driver.get("https://google.com");
}

@Test
public void test2() {
WebDriver driver = Driver.getDriver(Browsers.EDGE);
driver.get("https://yahoo.com");
}


}

And your Driver class do not have if condition;



public class Driver {

private static WebDriver driver;

public static WebDriver getDriver() {
return getDriver(Browsers.CHROME);
}

public static WebDriver getDriver(Browsers browser) {

switch (browser) {
//we do not have if statement here
case EDGE -> {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
driver.manage().window().maximize();
}
case FIREFOX -> {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
case SAFARI -> {
WebDriverManager.safaridriver().setup();
driver = new SafariDriver();
driver.manage().window().maximize();
}
default -> {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
}
}
return driver;
}
}

In this case, It will create 2 instances of EDGE driver, because everytime you recall it, it recreates the new driver without checking if already one driver exists.

chatGPT: The if statement is a safeguard to prevent unnecessary reinitialization of the driver if it is already been created. If the driver is null, it means that there’s no active WebDriver instance available.

Q: Why is ENUM (Browsers) class is used instead of Strings?

A: Because it prevents the failures that might happen with a string. First lets understand the beauty of ENUM classes.

chatGPT: ENUMs make your code more readable, less error-prone, and easier to understand because they give meaningful names to possible values. They also restrict the variable to only hold one of the specified values, which helps catch mistakes early and makes your code more robust.

In our case ENUM class looks like this;

public enum Browsers {

CHROME,
EDGE,
FIREFOX,
SAFARI;
}

If you use a string method, you are giving yourself an oppourtunitity to do things wrong. Like this;

@Test
public void test2() {
WebDriver driver = Driver.getDriver("egde");
driver.get("https://yahoo.com");
}

When you execute it, it will give an error and waste your time. So instead of having this issue, ENUM allows you to write only it’s variables, see the below ss;

See how easy it is to type edge?

Q: Why do i have to initialize my driver instance with “private” access modifier?

A: Because, when coding especially with Java, you should be implementing OOP concepts in your code whenever it is possible. This OOP concept used here called Encapsulation(My explanation of it is here!). You are preventing yourself or others inside of your project from accessing this driver and changing it to something else. This variable must only be used here.

this was the only meme i found about private i m sorry :{

Q: Why it has be static bro?? I do not get this why it can’t be just normal…

A: Ok little bro, i think i will explain this with an example that taught me how static works.

Below i have a BaseClass with a method called getnum(); When this method is called from another class, it will generate a value between 0–10.

public class BaseClass {

protected int i;

{
i = new Random().nextInt(10);
}

protected int getNum() {
return i;
}
}

Now i have a BaseStatic class with static keyword, this is the same class as above, it just has a static keyword in it.

public class BaseStatic {
static int i;

static {
i = new Random().nextInt(10);
}

public static int getNum(){
return i;
}
}

Now let’s say i have two classes that is called Test1 and Test2, and both of them extends the BaseClass and prints their randomly generated i value.

public class Test1 extends BaseClass{

public void test01(){
System.out.println(getNum());
}
}
public class Test2 extends BaseClass{

public void test02(){
System.out.println(getNum());
}
}

Now i will have a MainApp class that initalizes variable from their extended class and access the method;

public static void main(String[] args) {

Test1 t1 = new Test1();
Test2 t2 = new Test2();

System.out.println("Test1 Value = " + t1.getNum());
System.out.println("Static Value = " + BaseStatic.getNum());
System.out.println("Test2 Value = " + t2.getNum());
System.out.println("Static Value = " + BaseStatic.getNum());


}

Here i created t1 and t2 variables(they both extend the same class remember) and a static initialization of BaseStatic.getNum(); method.

Now When i execute it, let’s see the results;

The static variable stays the same when accessed repeteadly.

In this example, we have seen that static variable can only be initalized once. Whenever you access it/recall it again, it will return the first created value.

Switch back to Drivers, when initialized with static, the driver is going to be generated once, and continue its journey from another tests when accessed again and again.

To sum up, i think i covered all of the questions i had in mind, if you have a better solutions than the one i have, please tell me. I would be more than happy to learn more effective and productive things in this field.

Connect me on: https://www.linkedin.com/in/hasan-ozyer/

My Github Profile: https://github.com/hasanozye

--

--

Hasan Ozyer
Hasan Ozyer

Written by Hasan Ozyer

Junior Test Automation Engineer. Documenting my IT journey for my future self. Sharing my opinions, learnings, and adventures in the tech world.

Responses (1)