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.

Train Asked on October 28, 2014 in Selenium WebDriver.
Add Comment
  • 5 Answer(s)
    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

    Hi Mallik9292,

    You need to use Java Constants for that which is subtitute of Environment variable or Global Variable. Take a look over the below link, this is exactly what you have asked for:

    http://www.toolsqa.com/selenium-webdriver/constant-variables/

    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

    Hi Mallik,

    I think i mis read your question.

    I hope you are looking for a dynamic way to associate your test cases with right test data in the Excel sheet. I request you to go through the following links in sequential order:

    Constant Variables : http://www.toolsqa.com/selenium-webdriver/constant-variables/Data Driven Technique : http://www.toolsqa.com/selenium-webdriver/data-driven-testing-excel-poi/
    User Functions : http://www.toolsqa.com/selenium-webdriver/user-defined-functions/

    In the last link I have told how to pick up the testcase name from excel to get the test data of the current test cases dynamically.

    Regards,
    Lakshay Sharma

    Professor Answered on October 28, 2014.
    Add Comment

    Hi Lakshay,

    Thanks for your reply. I have implemented a different way.. So please let me know who to handle this. I am pasting the sample code of one of my function below for a post login page.

    Page object code for validate logo

    
    //Function to validate the Logo on Dashboard
    
    public static WebElement lnk_Home_Logo(WebDriver driver) throws Exception{
    
    Log.startTestCase(ExcelUtils.getCellData(1, 4));
    
    if(driver.findElement(By.xpath("/html/body/div/div/div/div[2]/a/img")).isDisplayed()) {
    
    Log.info("Logo Found");
    
    ExcelUtils.setCellData1("Pass", 1, 2);
    
    ExcelUtils.setCellData1("Logo Found", 1,3);
    
    System.out.println("Logo is displayed");
    
    }else{
    
    Log.info("Logo Not Found");
    
    ExcelUtils.setCellData1("Fail", 1, 2);
    
    ExcelUtils.my_red.setIndexed(1);
    
    ExcelUtils.setCellData1("Logo not Found", 1,3);
    
    System.out.println("Logo is not found");
    }
    
    Log.endTestCase("TestCase End");
    
    return element;
    
    

    App Module for acessing the function 

    
    public static void Validate_Post_Login(WebDriver driver) throws Exception {
    //To sign in to the application
    
    SignIn.Signin(driver);
    //To validate the logo is displayed on the page.
    
    Post_Login_Page.lnk_Home_Logo(driver);
    

    Suite Framework for execution:

    
    @Test
    
    public static void Test_User_Post_Login_A() throws Exception {
    User_Post_Login.Validate_Post_Login(driver);
    
    }
    
    Train Answered on October 28, 2014.
    Add Comment

    Hi Lakshay,

    Thanks for the reply… I am very new to java and selenium so i am trying to learn . I am using the below function

    //Function to validate the Logo on Dashboard
    public static WebElement lnk_Home_Logo(WebDriver driver) throws Exception{

     

      in a separate class. But  sTestcasename varible is in  Excelutils. So i am not able to acces the sTestcasename variable in this method


    //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));

    //Function to validate the Logo on Dashboard

    public static WebElement lnk_Home_Logo(WebDriver driver) throws Exception{
    int iTestCaseRow = ExcelUtils.getRowContains(Excelutils.sTestCaseName,Constant.Col_TestCaseName);
    Log.startTestCase(ExcelUtils.getCellData(iTestCaseRow, Constant.Col_XXXX));

     

    if i try using Excelutils.sTestCaseName  where i am declaring this variable in Excelutils class it doesnt give any error but its giving me null value.

    Please help on this

    Train Answered on October 29, 2014.

    Hi Mallik,

    I would not be able to do any more help in this, please get in touch with any developer in your team and ask him to debug the code for you to get the right error, it seems you are not providing right col & row to your Excel methods and it is hard to comment without debugging the code.

    But still I would suggest you to go through the complete Automation Framework chapters on ToolsQA, hopefully you will be able to resolve your problem. But please go through all the chapters with implementing the code given on the chapters.

    Regards,
    Lakshay Sharma

    on October 29, 2014.
    Add Comment
  • Your Answer

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