簡體   English   中英

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

我正在嘗試從excel工作表中提取數據,並在Web應用程序的登錄字段和密碼字段中傳遞這些測試數據,但出現錯誤。

我收到以下錯誤:

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:

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文件中的方式:

Column1  Column2
UserName Password

以下是我的testng.xml文件:

<?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嘗試將2個參數注入setupmethod時,它會看到3個參數並且失敗了,因此請按如下所示修改您的設置方法

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

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM