RE: Implement global variable for Testcase number to execute from excel.

I am using Selenium webdriver with Testng for automation with data driven module where i am reading the testcases data from excel. I have hard coded the row numbers for each test case . If i want to add any cases in the middle again i have to change the numbering of all the cases. So how can i solve this issue.

mallik9292 Train Asked on October 28, 2014 in Selenium WebDriver.
Add Comment
5 Answers
Best answer

Hi Mallik,

Rather looking for a straight code to fit in your script, I suggest you to understand the logic of it. If you go through the links I have shared earlier, you will get all code. The code need little changes and it will work for you.

You have getCellData and setCellData methods in ExcelUtils, both of the methods required two inputs (row and col).

Column always remain same, so you can use constant variables for column like this:


public class Constant {

public static final int Col_TestCaseName = 1;

public static final int Col_Result = 2;

public static final int Col_Comment = 3;

}

Row is your test case row, to get the row you need your test case name. To acheive that name your test class same as your test case name in Excel. Get the Test Case name from your test class with the below method:


public static String getTestCaseName(String sTestCase)throws Exception{

String value = sTestCase;

try{

int posi = value.indexOf("@");

value = value.substring(0, posi);

posi = value.lastIndexOf(".");

value = value.substring(posi + 1);

return value;

}catch (Exception e){

Log.error("Class Utils | Method getTestCaseName | Exception desc : "+e.getMessage());

throw (e);

}

Once you get the Test case name, search the test case name in your Excel sheet with this method and get the row number of the test case name:


public static int getRowContains(String sTestCaseName, int colNum) throws Exception{

int i;

try {

int rowCount = ExcelWSheet.getLastRowNum();

for ( i=0 ; i<rowCount; i++){

if  (ExcelUtils.getCellData(i,colNum).equalsIgnoreCase(sTestCaseName)){

break;

}

}

return i;

}catch (Exception e){

Log.error("Class ExcelUtil | Method getRowContains | Exception desc : " + e.getMessage());

throw(e);

}

}

Your Test Case


//Function to validate the Logo on Dashboard
public static WebElement lnk_Home_Logo(WebDriver driver) throws Exception{
int iTestCaseRow = ExcelUtils.getRowContains(sTestCaseName,Constant.Col_TestCaseName);
Log.startTestCase(ExcelUtils.getCellData(iTestCaseRow, Constant.Col_XXXX));
if(driver.findElement(By.xpath("/html/body/div/div/div/div[2]/a/img")).isDisplayed()) {
Log.info("Logo Found");
ExcelUtils.setCellData1("Pass", iTestCaseRow , Col_Result);
ExcelUtils.setCellData1("Logo Found", iTestCaseRow ,Col_Comment);
System.out.println("Logo is displayed");
}else{
Log.info("Logo Not Found");
ExcelUtils.setCellData1("Fail", iTestCaseRow , Col_Result);
ExcelUtils.my_red.setIndexed(1);
ExcelUtils.setCellData1("Logo not Found", iTestCaseRow ,Col_Comment);
System.out.println("Logo is not found");

}
Log.endTestCase("TestCase End");
return element;

PS: Do not forget to close your question:
http://forumsqa.com/selenium-webdriver/how-to-choose-best-answer-insert-code-vote-up-an-answer/

Regards,
Lakshay Sharma

Professor Answered on October 28, 2014.
Add Comment

Your Answer

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