WebDriver scripts with parameters

Some test automation projects require the ability to run the same test script multiple times using different values for the script parameters.

To use an example, the test script can do the following:

1. open the www.vpl.ca site
2. do a keyword search using java as the keyword
3. validate that the number of results is greater than 0
4. on the results page, change the sort order from the default sort order to Author
5. verify that the new sort order is correct

A few changes are needed for running this script multiple times using parameters:

1. open the www.vpl.ca site
2. do a keyword search using a {keyword parameter}
3. validate that the number of results is greater than 0
4. on the results page, change the sort order from the default sort order to a {sort parameter value}
5. verify that the new sort order is correct

A list of values for the 2 parameters (search keyword and sort order) is needed too:

search keyword sort order
java                  Author
vancouver Title
cats                  Rating
dogs                 Relevance

The script with parameters will be run as follows:

1. the first pair of values is selected from the list of values
2. the keyword parameter from the test script will take the keyword value from the pair of values
3. the sort order parameter from the test script will take the sort order value from the pair of values
4. the test script is executed using the selected values
5. the next pair of values is selected
6. continue from step 2

Going back to web driver, this is how the initial script looks like:

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class tests {


public WebDriver driver;

//use this class member for the search keyword value

private String keywordValue = "java";

//use this class member for the new sort order

private String sortOrderValue = "Author";

private String fullPatternValue = "Found (\\d+)(,*)(\\d+) items";
private String numberPatternValue = "(\\d+)(,*)(\\d+)";

@Before
public void setUp() 
{

System.setProperty("webdriver.chrome.driver",            "C:\\Users\\asiminiuc\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"); 

driver = new ChromeDriver();         
       
}
   
@After
public void tearDown() 
{
driver.quit();
}

@Test
public void tests1() throws InterruptedException
{  

HomePage homePage = new HomePage(driver);
ResultsPage resultsPage = homePage.search(keywordValue);

assertTrue(new PatternExtract(resultsPage.getItemsNumber(), 
                                                 fullPatternValue).matches() == true);    
PatternExtract patternExtract = new PatternExtract(resultsPage.getItemsNumber(), 
                                                                  numberPatternValue);  

assertTrue(patternExtract.matches() == true);      
assertTrue(patternExtract.getValue() > 0);

resultsPage = resultsPage.changeSortOrder(sortOrderValue);
assertTrue(resultsPage.getSortOrderValue().equals(sortOrderValue));

}



The script with parameters looks like this:



import static org.junit.Assert.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

@RunWith(value = Parameterized.class)
public class testsPARAMETERS {

public WebDriver driver;

private String stringValue;

private String keywordValue, sortOrderValue;

private String fullPatternValue = "Found (\\d+)(,*)(\\d+) items";
private String numberPatternValue = "(\\d+)(,*)(\\d+)";  

public testsPARAMETERS(String stringValue) {
this.stringValue = stringValue;
}

@Parameters
public static Collection<Object[]> data() throws IOException {
       
Object[][] data = {
          {"java-Author"}, 
       {"vancouver-Title"}, 
       {"cats-Rating"}, 
       {"dogs-Relevance"}
                  };

return Arrays.asList(data);   
}

@Before
public void setUp(){
System.setProperty("webdriver.chrome.driver", 
"C:\\Users\\asiminiuc\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"); 

driver = new ChromeDriver();               
}
   
@After
public void tearDown() 
{
driver.quit();
}
@Test
public void tests1() throws InterruptedException {

String[] values = stringValue.split("-");
keywordValue = values[0];
sortOrderValue = values[1];

HomePage homePage = new HomePage(driver);
ResultsPage resultsPage = homePage.search(keywordValue);

assertTrue(new PatternExtract(resultsPage.getItemsNumber(),  
            fullPatternValue).matches() == true);  

PatternExtract patternExtract = new PatternExtract(resultsPage.getItemsNumber(), 
                                                                  numberPatternValue);  

assertTrue(patternExtract.matches() == true);      
assertTrue(patternExtract.getValue() > 0);

resultsPage = resultsPage.changeSortOrder(sortOrderValue);
assertTrue(resultsPage.getSortOrderValue().equals(sortOrderValue));
}

}


These are the differences between the 2 scripts (highlighted above in red):

- a few additional libraries are added:

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

- the test class uses a special annotation:

@RunWith(value = Parameterized.class)

- a new member is added to the class:

private String stringValue;

This is the variable that will store the current pair of values for the test script parameters

- a new constructor is added to the test class:

public testsPARAMETERS(String stringValue) 
{
this.stringValue = stringValue;
}

It is in the constructor that the stringValue is initialized with the current pair of values.

- a new method is added to the class:

@Parameters
public static Collection<Object[]> data() throws IOException

This method defines the list of the parameter data and provides the selected pairs of values to the test script

- code is added to the test method for breaking the pair of values in the keyword and the sort order:

String[] values = stringValue.split("-");
keywordValue = values[0];
sortOrderValue = values[1];





_____________________________________________________________________________


Do you want to learn more about test automation and Java?
I will start an SELENIUM online group training on October 15.
Please see the training details here.

Share this