selenium pass parameter to @Test from pageObjects Common function

I’m in the process of developing an automation framework using Selenium Webdriver.

For every test case, I have many items to verify (eg. is user Login to the web application, is Gender checkbox exists on the page)

Then it will using Excel WriteResultUtility to write result in Excel based on predefined column name.

I have 20 test case, and I don’t want to code it in each test case every time. (minimize the code for easy to maintain in future)

// Selenium Main Test Script


public class MyTestCase_0001 extends SuiteRFIBase{

Read_XLS FilePath = null;

Read_XLS FilePath_TestResult = null; //Location to store Output File

String TestCaseName = null;

static int DataSet=-1;

public WebDriver driver;
@BeforeTest

public void checkCaseToRun() throws IOException{
// To set .xls file's path In FilePath Variable.

FilePath_TestResult = MyTestCase_0001_TestResult; //Each test case will have own Output file

TestCaseName = this.getClass().getSimpleName();
}
@Test(dataProvider="MyTestCase_0001Data")

public void MyTestCase_0001Test(String ColTestCaseName, String ColUsername, String ColPassword) throws Exception{
DataSet++;
// Check is User Login to the web application

Boolean isUserLogin = driver.findElements(By.xpath(Object.getProperty("verify_isUserLogin"))).size()!= 0;

if (isUserLogin == true){

SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_IS_USER_LOGIN,

DataSet+1, Constant.KEYWORD_PASS);

}else{

SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_IS_USER_LOGIN,

DataSet+1, Constant.KEYWORD_FAIL);

Testfail=true;

}
// Check is Gender checkbox available on the page

Boolean isGenderChkbxExists = driver.findElements(By.xpath(Object.getProperty("verify_isUserLogin"))).size()!= 0;

if (isGenderChkbxExists == true){

SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_IS_GENDER_CHKBX_EXISTS,

DataSet+1, Constant.KEYWORD_PASS);

}else{

SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_IS_GENDER_CHKBX_EXISTS,

DataSet+1, Constant.KEYWORD_FAIL);

Testfail=true;

}

// pageObjects Login_Page.java (I want to use this as common function)(but I have problem on the FilePath_TestResult, TestCaseName variables as it is different value for each test case)


public static WebElement toCheck_isUserLogin() throws Exception{

try{

Boolean isUserLogin = driver.findElements(By.xpath(Object.getProperty("verify_isUserLogin"))).size()!= 0;

if (isUserLogin == true){

SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_IS_USER_LOGIN,

DataSet+1, Constant.KEYWORD_PASS);

}else{

SuiteUtility.WriteResultUtility(FilePath_TestResult, TestCaseName, Constant.COL_IS_USER_LOGIN,

DataSet+1, Constant.KEYWORD_FAIL);

Testfail=true;

}

}catch(Exception e){

throw(e);

}

return element;
}

So my main test will become like this (but I’m not able to pass the parameter to here)(I get java.lang.NullPointerException at  SuiteUtility.WriteResultUtility)


@Test(dataProvider="MyTestCase_0001Data")

public void MyTestCase_0001Test(String ColTestCaseName, String ColUsername, String ColPassword) throws Exception{
DataSet++;
Login_Page.toCheck_isUserLogin();

Register_Page.toCheck_isGenderChkbxExists();
}

//SuiteBase.java (Initialize test data file)


public class SuiteBase {
public static Read_XLS MyTestCase_01_TestResult = null;

public static Read_XLS MyTestCase_02_TestResult = null;

public static Read_XLS MyTestCase_03_TestResult = null;

public static WebDriver driver = null;
//Initialize Test Data file.

public void init() throws IOException{
// Test Result location

MyTestCase_01_TestResult = new Read_XLS(System.getProperty("user.dir")+"\\TestOutput\\MyTestCase_01.xls");

MyTestCase_02_TestResult = new Read_XLS(System.getProperty("user.dir")+"\\TestOutput\\MyTestCase_02.xls");

MyTestCase_03_TestResult = new Read_XLS(System.getProperty("user.dir")+"\\TestOutput\\MyTestCase_03.xls");

My goal are make it as common function so it can be reusable and minimize the code.

However, the FilePath_TestResult (refer to SuiteBase.java) is different variable for each test case. How can I achieve this goal. Thanks for help.

Add Comment
1 Answer(s)

Well your question is big and needs a careful look at the code. However I would suggest
1. Create a fixed template of test data that can be passed to your test. For eg first name, last name etc.
2. Create a class out of it or use an existing data structure like list, maps or whatever, whatever suits you.
3. In you data provider write the logic to fetch data and pass it as an Object to the method.

If its not clear let me know, I will look carefully at your code.

Thanks
Virender

Virender Singh Professor Answered on February 23, 2015.

Hi Virender Singh

Appreciate your comment and suggestion.
Can you provide example code on how to write the logic to fetch data and pass it as an Object to the method?

my @DataProvider code are below


@DataProvider

public Object[][] MyTestCase_0001Data(){

// To retrieve data from column of SuiteMyTestCase data Sheet.
return SuiteUtility.GetTestDataUtility(FilePath, TestCaseName);

}

on February 24, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.