Integrate_CucumberBDD_With_Selenium-Part3

Tutorial​​ 16​​ ​​ ​​ ​​ Integrate​​ Cucumber​​ with​​ Selenium​​ – Part​​ 3

What you will Learn :

  • Create application​​ hook file

  • @Before hook to read property file

  • @Before hook to launch browser

  • @After hook to quit the browser

  • @After hook to​​ capture screenshot

Create application​​ hook file​​ 

Create ApplicationHooks.java file under src/test/java/Hooks folder

This will​​ be​​ our ‘Base Test Class’​​ where we will be writing our pre/post annotations.​​ 

It is hook’s responsibility to launch a browser, so we will be writing this as a pre-condition.​​ 

From our previous tutorial, recall that DriverFactory class has init_driver method to set the driver

So we will create an object reference of DriverFactory​​ so that we can call init_driver method. We will also create an object reference of​​ WebDriver

Also, recall that ConfigReader.java is the class in which we are loading the config.properties file. This file has the actual browser, see below

So we will also create an object of ConfigReader.java so that we can call the actual browser by reading the properties file

@Before hook to read property file

We can now create @Before annotation, create an object of ConfigReader() class and then call the init_prop method to initialize the property, see below​​ 

Now, if you mouseover init_prop, notice that this method return us the ‘Properties’

So we can​​ create a ‘Properties’ variable ‘prop’ and use this varibale to store the properties that are returned in line 18, see below​​ 

@Before hook to launch browser

We can now use this ‘prop’ variable to launch our browser. To do that, we will create another @Before annotation to launch browser, see below​​ 

Our key is “browser” that is defined in config.properties file​​ 

So we will replace ‘key’ with “browser”

Next, if you mouseover getProperty method, you would notice that it returns us a String

So​​ we will create a String variable, see below

Now that we have ‘browserName’, we can create an object of DriverFactory() class,​​ call init_driver method​​ and pass the ‘browserName’

Next, if you mouseover init_driver method, notice that it returns us the WebDriver

Right now, the driver variable in line#11​​ is pointing to null​​ 

We thus will use this variable in line#25 to store the browser

The​​ above​​ 2 @Before annotations will be executed before every scenario in our feature file.

@After hook to quit the browser

We will now write the @After annotation (with order 0) to quit the browser. Recall that, in the case of @After annotation, order = 0 will be the last​​ (we have already studied this in our hooks tutorial). So, once the scenario gets executed, the below hook will be called to quit the browser

@After hook to capture screenshot

We will now create @After annotation that will take a screenshot when a scenario fails, see below. We create an object of ‘Scenario’ and then call the isFailed() method

So the first thing we want to do when the scenario fails is to get the scenario name and replace all white spaces in the scenario name by underscore

If you mouseover getName method, notice that it returns a String

So we have

Next, below is the standard line of code to take the screenshot and store it in a sourcePath variable

Next we will call the ‘attach’ method. This method will attach the screenshot to one of our reports, example, Extent Report​​ 

Replace ‘data’ with sourcePath, mediaType with “image/png”, ‘name’ with screenshotName​​ 

Below is our complete hooks code:


We will continue in our 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 - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024