macOsX
macOsX

Reputation: 457

Selenium Webdriver Keyword driven with TestNG integration

I'm in the process of learning keyword driven framework with TestNG and ReportNG integration. My requirements are

  1. Triggering the automation using TestNG
  2. Read the keywords and values from the excel sheet (one sheet has keywords and actions. The other sheet has xpath for each object)
  3. Drive the automation using keyword driven approach
  4. Generate report using ReportNG

TestNG File:

<suite name="Test Results" parallel="false" thread-count="3">
<listeners>
    <listener class-name="org.uncommons.reportng.HTMLReporter"/>
    <listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
</listeners>
<test name="Automation Test">
    <classes>
        <class name="TestSuite1">
        </class>
    </classes>
</test>

Excel Sheet1 :

enter image description here

Excel Sheet2:

This can be moved to properties file but as of now I'm reading from excel file

enter image description here

TestSuite file:

@Test(dataProvider="hybridData")
public void testLandingScreen(String testcaseName,String testStep,String keyword,String execute,String objectName, String value) throws Exception {
        excelUtils.processXls(testcaseName, testStep, keyword, execute, objectName, value);
    }
}

@DataProvider(name="hybridData")
public Object[][] getDataFromDataprovider() throws IOException {
    Object[][] object = null;
    ReadExcelFile file = new ReadExcelFile();

    //Read keyword sheet
    Sheet sheet = file.readExcel(BaseData.XLS_PATH,"TestData.xlsx" , "TSData");
    //Find number of rows in excel file
    int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
    object = new Object[rowCount][6];
    for (int i = 0; i < rowCount; i++) {
        //Loop over all the rows
        Row row = sheet.getRow(i+1);
        //Create a loop to print cell values in a row
        for (int j = 0; j < row.getLastCellNum(); j++) {
            //Print excel data in console
            object[i][j] = row.getCell(j).toString();
            //object[i][j] = row.getCell(j);
        }
    }
    return object;
}

Can anyone help me in clarifying the below questions?

  1. When I run this test integrated with ReportNG, each excel row is considered as single test case and the report is showing the total number of row as test case count.

    Any suggestion on how to read the excel sheet based on TC name and generate ReportNG report based on that?

  2. If the answer for the above question is yes, then how can I call single @Test method to generate more than one test case?

Any help on this is highly appreciated. Thanks.

Upvotes: 0

Views: 2496

Answers (2)

aishah
aishah

Reputation: 43

I think the issue lies in the array used to store the test steps. You are using 2D array, and so there is no correlation between the Test Cases and the Test Steps.

I would use a Map instead and convert it to the 2D array of data provider.

Example:

Map<String, String> map = new HashMap<String, String>();
map.put("TS1", "step1");
map.put("TS2", "step2");
object[0][1] = map;

Upvotes: 0

Pedro Cuello
Pedro Cuello

Reputation: 19

Firstly, you need to understand what is a dataprovider which you are using in your implementation... http://toolsqa.com/selenium-webdriver/testng-data-provider-excel/ > this web can help you...

Data providers send parameters for the execution of your test.... each row will be parameters for the execution of each test... in other words...

Firs row will be parameters for your first test... second raw will be parameters for you second test... and so...

I think that you doing a wrong use of dataprovider... dataprovider is for providing data to the tests... for example and username or password...

For the location of element there are other practices as in the own code of the test.. in other functions or PAGEOBJECTS...

So, the short anwers is... please check what dataprovider is...

Best regards

Upvotes: -1

Related Questions