Singleton Design Pattern in Selenium

Let’ see how can you use Singleton Design Pattern in Selenium. In this tutorial, We will learn about the Framework Design Pattern using singleton class in Selenium.

In the Previous tutorial we have seen, How can we run the Test using Page Factory. Now question is, Will we repeat the Browser invoke and Logger code again and again in each and every Test.

 

No, To resolve this issue we will use the Singleton Pattern. We will design the Single Test Base class.

 

Singleton Design Pattern in Selenium

First, Let’s see what is Singleton Pattern.  With the Singleton design pattern you can:

  • Ensure that only one instance of a class is created

  • Provide a global point of access to the object

In TestBase class, we will Design the Singleton Pattern, We will write the command code here and all Test Classes will inherit this TestBase class.

 

In the previous Example, we have seen we were invoking WebDriver instance in each @Test. Here, We will Implement the WebDriver initialization in TestBase class and All other Test Classes will use this Instance of WebDriver.

 

This TestBase class will have the parent-child relationship with all other Test Classes. In the TestBase class, We will make the all Generic functions.

 

Let’s see the Code of TestBase Class.

Now let’s understand the above class function.

 

In TestBase class, First, we have declared Global Variables of Logger, Properties, and WebDriver.

  • initConfig() – We are initializing the Logger and Properties file.

  • initBrowser() – We are initializing browser instance on the basis of Browser Name mentioned in the Properties file.

  • invokeApplication() – We are invoking the target Application. Here we are providing the target web application URL.

  • closeBrowser() – In this function we are closing the current instance of the browser.

 

After this, we have declared two annotations @BeforeTest and @AfterTest.

  • @BeforeTest – In this annotation, we are calling invokeConfig, invoke browser and invoke the application. So this annotation will execute before all Test Classes.

  • @AfterTest – In this annotation, We are closing the current open Browser Instance. So browser will be closed when All Tests of one class will complete.

 

Now let’s see the structure of our project and see how it will work.

 POM Selenium Framework

 

In the above structure, we have 4 Packages. Let’s understand the functionality of all.

 

1. Data – In this package we will keep all Data files. They may be XML, excel or CSV. We are using Excel file. We have only one file named TestData.xlsx

NoteIn case you are using our code in your Test Project then please Include the Apache POI in your project.

 

2. Pages – In this package, we will keep all Page Class. One page class for one page and every Page class will extend the Page.java class. Because in Page.java class we have kept our comman code which is required for each page class.

In Specific page class we will keep the locators and Action on these locators for that specific page. (We have already seen this in our previous POM Toturials)

 

3. TestCases – In this package, We will keep our Test Cases classes. Test Classes should be specific to Pages and One Test Class should be present for each Page. TestBase.java class is the comman class which will be inherited by each test class.

 

4. Utilily – Next we have the util package. In this package, we will keep all utilities related code and structure. Let’s understand our Util Package and classes –

  • Constants.java – In this file, we will keep all constants like our file paths and other constant values.

  • TestUtil.java – In this file, we have declared all utility function. Currently only two functions are present which will read the TestData.xlsx file and check, Is TestCase executable?
    We will mention the TestCase name in this file and Test will only if Test Name present in the TestData.xlsx file.

  • Xls_Reader.java – In this file, we have mentioned the common code through which we will read Excel file.

 

Download the Complete Selenium Project code form here. (Selenium POM Framework)

 

You can get the code from here and try it on your local machine. This is complete Framework you can use it Automate your Project.

0 Comment

Leave a Reply