Selenium Webdriver,TestNG-数据提供程序尝试传递2个参数,但方法采用3个参数,而TestNG无法注入合适的对象 [英] Selenium Webdriver, TestNG - data provider is trying to pass 2 parameter but the method take 3 and TestNG is unable in inject a suitable object

查看:228
本文介绍了Selenium Webdriver,TestNG-数据提供程序尝试传递2个参数,但方法采用3个参数,而TestNG无法注入合适的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从excel工作表中提取数据,并在Web应用程序的登录字段和密码字段中传递这些测试数据,但出现错误.

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.

我遇到以下错误:

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

以下是我保存在属性File中的xpath:

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

这就是我将测试数据保存在excel文件中的方式:

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

Column1  Column2
UserName Password

以下是我的testng.xml文件:

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>

以下是我的代码:

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;

    }

}

推荐答案

TestNG尝试将测试数据从DataProvider方法(TestDataFeed)注入到您的Test方法(设置)中.在数据提供者方法中,您已经配置了两列,即用户名和密码. TestNG通过检查二维对象数组来确定它.

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.

当TestNG尝试向setupmethod中注入2个参数时,它会看到3个参数,但失败了,因此请按如下所示修改设置方法

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

希望这会有所帮助

这篇关于Selenium Webdriver,TestNG-数据提供程序尝试传递2个参数,但方法采用3个参数,而TestNG无法注入合适的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆