Parameterizing Tests using suite parameters

In Chapter 1, Introducing WebDriver and WebElements, we created a search test that performs a simple search on the application under test. This test searches for a given product and validates the title. We used a hardcoded value, phones, for the search, as shown in the following code snippet:

 @Test
 public void searchProduct() {

        // find search box and enter search string
        WebElement searchBox = driver.findElement(By.name("q"));

        searchBox.sendKeys("Phones");

        WebElement searchButton =
                driver.findElement(By.className("search-button"));

        searchButton.click();

        assertThat(driver.getTitle())
                .isEqualTo("Search results for: 'Phones'");
    }

Instead of using hardcoded values, we can parameterize these values and provide them to the test method using the suite-parameter feature of TestNG. This will help to remove using hardcoded values in the test method and move them into TestNG suite files. The parameterized values can be used in multiple tests. When we need to change these values, we don't have to go to each test and make a change,the  instead we can simply change these in suite file.

Now, let's look at steps for using the TestNG Parameters from the suite file. In Chapter 1, Introducing WebDriver and WebElements, we created a testng.xml file, which is located in the src/test/resources/suites folder. Let's modify the file and add the parameter declaration, as highlighted in the following code snippet: 

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Chapter 1" verbose="1">
<listeners>
<listener class-name="com.vimalselvam.testng.listener.ExtentTestNgFormatter"/>
</listeners>
<test name="Search Test">
<parameter name="searchWord" value="phones"/>
<parameter name="items" value="3"/>
<classes>
<class name="com.example.SearchTest"/>
</classes>
</test>
</suite>

We can add parameters in the TestNG suite file using the <parameter> tag. We have to provide the name and  value attributes for the parameter. In this example, we create two parameters: searchWord and items. These parameters store the search word and expected count of items returned by the application for that search word. 

Now, let's modify the test to use parameters instead of hardcoded values. First, we need to use the @Parameters annotation before the @Test annotation for the test method. In the @Parameters annotation, we need to supply the exact names and order of the parameters declared in the suite file. In this case, we will supply searchWord and items. We also need to add arguments to the test method along with the required data type to map the XML parameters. In this case, the String searchWord and int Items arguments are added to the searchProduct() test method. Finally, we need to replace the hardcoded values with the arguments in the test method, as shown in the following code snippet:

@Parameters({"searchWord", "items"})
@Test
public void searchProduct(String searchWord, int items) {

// find search box and enter search string
WebElement searchBox = driver.findElement(By.name("q"));

// use searchWord parameter value from XML suite file
searchBox.sendKeys(searchWord);

WebElement searchButton =
driver.findElement(By.className("search-button"));

searchButton.click();

assertThat(driver.getTitle())
.isEqualTo("Search results for: '" + searchWord + "'");

List<WebElement> searchItems = driver
.findElements(By.xpath("//h2[@class='product-name']/a"));

assertThat(searchItems.size())
.isEqualTo(items);
}

We have to run the parameterized tests via the testng.xml file for TestNG to read the parameters defined in the suite file and pass the values to the test method. 

During execution, TestNG will use the parameters defined in the XML suite file and map these in the same order to the Java parameters in test methods using the @Parameters annotation. It will pass the parameter values from the suite file using the arguments added in the test method. TestNG will throw an exception if the number of parameters between XML and the @Parameters annotation does not match.

In the next section, we will see a programmatic parameterization, which offers us the ability to run tests with multiple rows of test data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.2.15