Blogs

Data Driven Selenium Automation Testing – Part 1 (with Junit 5)

In this article, we would be using Junit 5 for automating Selenium code and also exploring the Data Driven Features of Junit 5.

The Problem statement is:

“Given a data set comprising of URLs to visit along with corresponding Page Titles to be verified using Data Driven features of Junit 5 : Data looks like below

URLS Page Titles to be verified
“https://www.google.com/” “Google”
“https://www.imdb.com/” “IMDb:”
“https://www.wikipedia.org/” “Wikipedia”

Project Setup:

  • Create a Maven Project in Eclipse
  • Need to include following maven dependencies required if we decide to use Junit-5 in the pom.xml
  • You need to download and save at src/test/resources/drivers folder of the maven project
    1. Chromedriver.exe
    1. Geckodriver.exe

Now let us develop the Junit 5 test class for the purpose and important Annotations need to be learnt that are used for this purpose are

  1. @BeforeEach  Annotation
  2. @AfterEach     Annotation
  3. @ParameterizedTest  Annotation
  4. @MethodSource(“dp”)
  5. Defining a static method to act as a data provider

                 Now we understand each item in detail

@BeforeEach 

This annotation marks the method as a setup method. All the activities like creating appropriate WebDriver object, setting window maximising and setting up Intrinsic waits etc are carried out in the setup method and such method is annotated by @BeforeEach tag and corresponding import statement has to placed appropriately.

In the above code snippet, presence of @BeforeEach Annotation declares the method beforeTest() as the setup method and carry out following setup activities

  1. At Line 23 & 24, setting up the required  property and driver initialisation happens.
  2. At Line 25, window is maximized with the help of driver object.
  3. AT Line 26, setting up Implicit wait of 15 sec that would be applicable throughout the program.

@AfterEach 

This annotation marks the following method as a teardown method. All the closing activities like releasing the resources by closing or quitting the driver objects etc are carried out in the teardown  method and such method is annotated by @AfterEach tag and corresponding import statement has to placed appropriately. And such teardown method, as indicated by its name, gets executed after each test method in the class.

In the above code snippet, presence of @AfterEach Annotation declares the method afterTest() as the teardown method and carry out following cleanup activity

  1. At Line 69, driver.quit() causes all the windows associated with current driver object are closed.

@ParameterizedTest  Annotation and @MethodSource(“dp”)

This annotations is placed before the test method and allows this test method to receive the parameters. This feature is really an improvement in Junit 5 as compared to Junit 4. And thus puts a solid foundation to enable data driven testing in conjunction with another annotation @MethodSource(“dp”) that maps test method with the data provider “dp”  .

Defining a static method to act as a data provider

At Line 40, data provider is annotated by @MethodSource(“dp”)  and the name “dp” should be a static method, usually return a two dimensional array Object.

In this example dp() method returns a two dimensional array of String with 3 rows x 2 cols. Each row is passed to the parameterised method with one row at a time. Thus effectively test method is executed as many times as many number of rows provided by the dp method. This can be replaced by a method which can read the data from an excel sheet or csv file or even Json file.

Imports

Most of the imports related to above discussions are given below

For The complete code, you may find at gitlab link

  1. Pom.xml
  2. Ex01_DataDriven_JU5.java

In the next article in the series, we would like to implement Data Driven approach with data in CSV Format.

*****

*******

For upcoming CP-SAT Events please look at the following URL for ATAEvent

ATAEVENTS.org

https://cpsat.agiletestingalliance.org/

You can also visit the CP-SAT page for more information

Leave a Comment