Selenium Webdriver, TestNG - data provider is tryi

2019-09-06 21:05发布

问题:

I am trying to pull data from excel sheet and pass those test data in the login field and password field for web application but I am getting error.

I am getting following error:

org.testng.TestNGException: 
The data provider is trying to pass 2 parameters but the method     com.access.Curam#setUp takes 3 and TestNG is unable in inject a suitable object

Following are xpath I kept in the property File:

URL = https://testexample.com
Email = //*[@id='j_username']
Pwd = html/body/div[2]/form/input[2]
Submit = html/body/div[2]/a/span/span/span
WellCometoTestingWorld = //*[@id='app-banner']/div[1]/div/h2

And this how I kept test data in the excel file:

Column1  Column2
UserName Password

And following is my testng.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="TestSuite" thread-count="2" parallel="tests" >

<test name="AllTest">

<classes>

<class name="com.Test1.StartTest">

</class>

</classes>

</test>

</suite>

Following are My code:

package com.access;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import jxl.Sheet;
import jxl.Workbook;

public class Curam {

    static WebDriver driver;
    Workbook wb;
    Sheet sh1;
    int numrow;
    String username;
    String password;


    @Test(dataProvider = "testdata")
    public void setUp(ArrayList<String> sqldata, String uname, String password1)
            throws InterruptedException, FileNotFoundException, IOException

    {
        System.setProperty("webdriver.chrome.driver", "C:\\Directory\\chromedriver.exe");
        driver = new ChromeDriver();

         Properties obj = new Properties();
         System.out.println("data>>>>>>"+sqldata);
         FileInputStream objfile = new FileInputStream(System.getProperty("user.dir") +"\\src\\com\\access\\Object.Properties");
         obj.load(objfile);
        driver.get(obj.getProperty("URL"));
        driver.manage().window().maximize();
        Thread.sleep(1000);
        driver.findElement(By.xpath(obj.getProperty("Email"))).clear();
        Thread.sleep(1000);
        driver.findElement(By.xpath(obj.getProperty("Email"))).sendKeys(uname);
//      Thread.sleep(1000);
//      driver.findElement(By.xpath("//*[@id='next']")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath(obj.getProperty("Pwd"))).clear();
        Thread.sleep(1000);
        driver.findElement(By.xpath(obj.getProperty("Pwd"))).sendKeys(password1);
        Thread.sleep(1000);
        driver.findElement(By.xpath(obj.getProperty("Submit"))).click();
        Thread.sleep(1000);
        Assert.assertEquals("Well Come to Testing World", driver.findElement(By.xpath(obj.getProperty("WellCometoTestingWorld"))).getText());
    }

    @DataProvider(name = "testdata")
    public Object[][] TestDataFeed() {

        try {

            // load workbook
            wb = Workbook.getWorkbook(new File("C://File//Book3.xls"));

            // load sheet in my case I am referring to first sheet only
            sh1 = wb.getSheet(0);

            // get number of rows so that we can run loop based on this
            numrow = sh1.getRows();
        } catch (Exception e)

        {
            e.printStackTrace();
        }

        // Create 2 D array and pass row and columns
        Object[][] logindata = new Object[numrow][sh1.getColumns()];

        // This will run a loop and each iteration it will fetch new row
        for (int i = 0; i < numrow; i++) {

            // Fetch first row username
            logindata[i][0] = sh1.getCell(0, i).getContents();
            // Fetch first row password
            logindata[i][1] = sh1.getCell(1, i).getContents();

        }

        // Return 2d array object so that test script can use the same
        return logindata;

    }

}

回答1:

TestNG tries to inject test data from DataProvider method (TestDataFeed) to your Test method (setup). In your data provider method, you have configured two columns namely username and a password. TestNG determines it by inspecting the two dimensional object array.

When TestNG tries to inject 2 parameters to setupmethod, it sees 3 parameters and it is failing so modify your setup method like below

 public void setUp(String uname, String password1) throws InterruptedException, FileNotFoundException, IOException

Hope this helps