Selenium与TestNG中的数据提供者不匹配

Selenium与TestNG中的数据提供者不匹配

本文介绍了Selenium与TestNG中的数据提供者不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用硒编写以下代码,并且显示以下错误,请让我知道问题出在哪里.

I am writing the below code in selenium and below error is showing, please let me know where is the issue.

import org.testng.annotations.DataProvider;导入org.testng.annotations.Test;

import org.testng.annotations.DataProvider;import org.testng.annotations.Test;

public class testngexcel {

    public static ExcelReader excel = null;
    @Test(dataProvider = "newdata")
    public void testData(String username, String password, Integer age) {
        System.out.println(username + " - " + password + " - " + age);
    }

    @DataProvider(name = "newdata")
    public static Object[][] getData() {

            if (excel == null) {
                excel = new ExcelReader("C:\\Users\\Anjali.Nautiyal\\Desktop\\selenium\\testngdata.xlsx");
            }

            String sheetName = "login";
            int rows = excel.getRowCount(sheetName);
            int cols = excel.getColumnCount(sheetName);

            Object[][] data = new Object[rows - 1][cols];

            for (int rowNum = 2; rowNum <= rows; rowNum++) {
                for (int colNum = 0; colNum < cols; colNum++) {
                    data[rowNum - 2][colNum] = excel.getCellData(sheetName, colNum, rowNum);
                }
            }
            return data;

错误:

推荐答案

我看到的唯一问题是,您是从excel读取的,因此可能所有值都在String中,除非您将其转换为Integer.但是,在测试中,您希望第三个参数ageInteger

The only problem I see is this, You are reading from excel so may be all the values are coming in String, unless you are converting that to Integer. However, in your test you expect third argument age to be Integer

将类型更改为String应该可以解决问题

Changing type to String should resolve the issue

@Test(dataProvider = "newdata")
public void testData(String username, String password, String age) {
        System.out.println(username + " - " + password + " - " + age);
}

以下代码将引发相同的错误.

The following code would raise the same error.

@DataProvider(name = "newdata")
public static Object[][] getData() {
    return new Object[][]{
            {"20"},
            {"30"}
    };
}

@Test(dataProvider = "newdata")
public void testData(Integer age) {

    System.out.println(age);

}

这篇关于Selenium与TestNG中的数据提供者不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 15:46