Wednesday, 1 March 2017

Parameterizing the test cases

Till now we have discussed about the doLogin() method where we are passing the default username & password. But in our SignInTest there are multiple test scenarios with different combinations. So, we should write the script in such a way that it will run as per the scenarios. To achieve this, we can use @DataProvider for @Test method.


Before you go ahead with this step, make sure we have followed STEP 5: Add Required .xls Files of Data. & STEP 6: Add .xls File Reading Utility in Framework.
If you have gone through step by step as mentioned, then you can implement the below steps or else please go through STEP-5 & STEP-6 first.
For parameterizing the test cases, follow the below steps:
  • Download the .zip from here.
  • Extract the .zip file.
  • Copy the TestDataProvider.java file inside com.demo.pom.util package.
  • Copy & replace the SignInTest.java file inside com.demo.pom.testcases package.


Code Overview:
TestDataProvider.java
This class will contain all the data providers in a single class file. The class will have a @DataProvider annotation & the method name. The method name is the dataProvider name in @Test annotation. Please check the below code:
       @DataProvider()
       public static Object[][] getSignInTestData() throws IOException {
              return DataUtil.getTestData("SignInTest");
       }

In Step-6 we have already discussed about the DataUtil class. Here, in getSignInTestData() method we are returning a two dimensional object array which will contain the Column Name & Data for the SignInTest.
SignInTest.java
In this class, we have modified the @Test annotation by passing the below parameters:
  • dataProvider = "getSignInTestData”: The name of the data provider which will be same as the method name in TestDataProvider class.
  • dataProviderClass = TestDataProvider.class: Data provider class name. 
And in the signInTest() method we have passed an argument “Hashtable<String, String> data”. That means from the dataProvider class, it will get all the data of SignInTest & will store that in the Hashtable (data). Wherever we need any data we can call data.get(Key).
Key is nothing the column name in the excel sheet. e.g. in line no 27 Object page = lunchPage.gotoSignInPage().doLogIn(data.get("UserName"), data.get("Password")); we are getting the data for UserName & Password columns.

Once you have completed the above steps, Right Click on SignInTest.java > Run As > TestNG Suite & verify the results.

Challenges:
  1. While running the test cases you may observe that for each scenario it’s opening a new browser instance.
  2. There is no validation check whether the login is success or not.
  3. No check for Skiping the test case or test scenario.
We can solve these problems in next post. Till now if you faced any issue please comment below.



<<<Prev       Next>>>

No comments:

Post a Comment