Integrate_CucumberBDD_With_Selenium-Part2

Tutorial​​ 15​​ ​​ ​​ ​​ Integrate​​ Cucumber​​ with​​ Selenium​​ – Part 2

What you will Learn :

  • Add a utility in DriverFactory

  • ThreadLocal​​ concept

  • init_driver() method​​ 

  • getDriver() method

  • delete cookies and maximize browser

  • synchronized keyword for parallel execution

  • ConfigReader​​ properties​​ file

Add a utility in DriverFactory​​ 

Let us go to our DriverFactory class. In this class, we will write a utility or a function which will return the driver.


Let us​​ start by​​ creating​​ the ‘driver’ object and importing​​ the WebDriver library​​ ​​ 

ThreadLocal​​ concept

Let’s say, we have 3 threads that are available, see​​ the​​ below​​ figure.​​ 

Now using threadlocal, we will write ‘Webdriver driver ​​ = new chromedriver’

Every thread wil get the same copy of driver because of thread local concept. So first thread will start first test case with chrome browser, second thread will start another test case in parallel with another chrome browser,​​ ​​ third thread will start another test case in parallel with another chrome browser​​ and so on…All the 3 threads have same copy of webdriver and hence parallel execution is possible

If we don’t use the thread local,​​ we will not be able to execute parallel execution in cucumber framework because we don’t have TestNG concept over here.​​ So lets say we​​ create multiple threads​​ using maven. So, in this case, if thread 1 is using the driver, thread 2 cannot use the same driver copy. It has to wait for the​​ previous test case execution​​ to​​ over

So,​​ in order to perform parallel testing,​​ we should create a driver session for every Test​​ to make Tests independent (Automation best practice). By default, WebDriver is not Thread Safe. We can use ThreadLocal class in Java to make WebDriver instance thread-safe:​​ 
ThreadLocal driver = new ThreadLocal()

ThreadLocal class has it’s setter/getter method, using which we can​​ set or get the driver instance for a particular thread:
driver.set(new ChromeDriver)
driver.get().get(url)

Keeping ThreadLocal concept in mind,​​ let us​​ create an​​ instance​​ of​​ ThreadLocal​​ class

init_driver() method

We will now create​​ init_driver​​ method​​ which accepts ‘browser’ as argument and the method​​ will initiate​​ the value​​ of WebDriver on the basis of the browser property

We will now​​ an SOP to print the value of ‘browser’ and also​​ write an ‘if’ loop and initiate the ChromeDriver, see below

We will next write the ‘else if’ loop for another driver

Next we will write an ‘else’ loop and pass an SOP as seen below

getdriver() method

In the above method we had ‘set’ the driver.​​ We will create another​​ method​​ that​​ will ‘get’ the reference of WebDriver​​ (example, chrome driver, firefox driver)

delete cookies and maximize browser

After initializing the driver, we can now call the getDriver method to delete the cookies and maximize the browser

After​​ maximizing the browser, we will return the getDriver() at runtime. So, at runtime, the current instance of WebDriver will be given, see below

Now line#30 above is throwing an error because the init_driver method type is void (line 13). Now, if you mouseover get() method, we see that getDriver() method is returning us WebDriver

So this means that line#30 is also returning us WebDriver. So, let us change void to WebDriver. Notice that erorr is resolved, see below

​​ 

Lets say multiple threads are running in parallel execution and they all will be calling the getDriver method.​​ We can use ‘synchronized’ keyword to ensure all the threads​​ are​​ in sync

So this completes our DriverFactory class, below is the complete code

ConfigReader​​ properties​​ file

Next, we will be creating a properties file to interact with below config.properties file

Create a ‘util’​​ folder under src/main/java and create ConfigReader.java file under util folder, see below

Let us now add a private variable ‘prop’ and create a method init_prop() to initialize the properties. We then create an object reference of Properties class, see below

Next, to interact with the config.properties file, we will create an object of FileInputStream class

We will now pass the location of config.properties file inside the FileInputStream​​ constructor

Just copy the location path starting from src\test onwards as seen below


Paste the path within double quotes

Next, put a .\\​​ at the starting of src​​ 
Note:​​ Dot . is used to indicate relative position within​​ the hierarchical tree of names

Next, surround with try/catch block to resolve the error

We will next load the properties file

Add​​ catch block to resolve the error

Next, we will return the properties

We are getting an error since the method type is void. Let us change the type to ‘Properties’.​​ 

Below is the complete code

We will continue​​ in the next tutorial.

Thank you​​ for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 10000 RS/149 USD
Attend All Live courses in just 10000 rs / $149 - offer ends 25th April 2024