0

编辑:我对代码进行了非常重的重构,并修复了这个问题。固定代码位于错误代码下方。

我有这两个:

public String[] firstname = new String[2];
public String[] lastname = new String[2];

我想将它们传递给这个方法(它已经接受了来自其他地方的一些数据):

    @Parameters({ "driver", "wait", "firstname[]", "lastname[]" })
    @Test(dataProvider = "dataProvider")
public void oneUserTwoUser(WebDriver driver, WebDriverWait wait, String[] firstname, String[] lastname) {

        //code
    }

这是另一个类的数据提供者:

public class AppData extends CSVReader { //was public abstrac class before
    public static WebDriver driver;
    public WebDriverWait wait;
    public String[] firstname = new String[2];
    public String[] lastname = new String[2];
    public String firstname1;
    public String firstname2;
    public String lastname1;
    public String lastname2;

    @DataProvider(name = "dataProvider")
    public Object[][] setUp(ArrayList<ArrayList<String>> array) throws Exception {

        driver = new EventFiringWebDriver(new FirefoxDriver(ffox, profile, dc))
                .register(eventListener);
        wait = new WebDriverWait(driver, timeoutInSeconds);

        int randomUser1 = randomizer (1, 250);
        int randomUser2 = randomizer (1, 250);
        firstname1 = array.get(randomUser1).get(0);
        firstname2 = array.get(randomUser2).get(0);
        lastname1 = array.get(randomUser1).get(1);
        lastname2 = array.get(randomUser2).get(1);

        firstname[0] = firstname1.replace(" ", "");
        firstname[1] = firstname2.replace(" ", "");
        lastname[0] = lastname1.replace(" ", "");
        lastname[1] = lastname2.replace(" ", "");


    Object[][] setUp = new Object[1][4];
    setUp[0][0] = driver;
    setUp[0][1] = wait;
    setUp[0][2] = firstname;
    setUp[0][3] = lastname;
    return setUp;
    }

你能告诉我我在这里做错了什么吗?我是 Java 新手 - 4 周。

我收到此错误:

SKIPPED: oneUserTwoUser
java.lang.IllegalArgumentException: wrong number of arguments
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

编辑这是有效的重构代码

    @Parameters({ "driver", "wait", "array" })
    @Test(dataProvider = "dataProvider")
    public void oneUserTwoUser(WebDriver driver, WebDriverWait wait, ArrayList<ArrayList<String>> array) throws Exception {
        String[] firstname = new String[2];
        String[] lastname = new String[2];
        String firstname1;
        String firstname2;
        String lastname1;
        String lastname2;
        //parametrize tests by reading the array passed from CSV reader class
                int randomUser1 = randomizer(1, 250);
                int randomUser2 = randomizer(1, 250);
                firstname1 = array.get(randomUser1).get(0);
                firstname2 = array.get(randomUser2).get(0);
                lastname1 = array.get(randomUser1).get(1);
                lastname2 = array.get(randomUser2).get(1);
                firstname[0] = firstname1.replace(" ", "");
                firstname[1] = firstname2.replace(" ", "");
                lastname[0] = lastname1.replace(" ", "");
                lastname[1] = lastname2.replace(" ", "");
                System.out.println(firstname[0]);
                System.out.println(lastname[0]);
                System.out.println(firstname[1]);
                System.out.println(lastname[1]);

}

public abstract class AppData extends Mailer {
    public static WebDriver driver;
    public static WebDriverWait wait;
    static AppTest3 instance;

    @DataProvider(name = "dataProvider")
    public Object[][] setUp() throws Exception {


        // change to false if testing on local machine, so selenium will pick up the correct firefox binary.
        Boolean testingOnServer1 = false; //
        File firefoxPath;
        if (testingOnServer1 == true) {
            firefoxPath = new File(System.getProperty("lmportal.deploy.firefox.path", "/opt/firefox/firefox")); // usr/bin/firefox - another firefox isntallation
        } else {
            firefoxPath = new File(System.getProperty("lmportal.deploy.firefox.path", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"));
            // "c:\\Program Files\\Mozilla Firefox\\firefox.exe")); //for testing at home
        }

        //prepare Firefox and run it on Xvfb
        long timeoutInSeconds = 30;
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("dom.max_chrome_script_run_time", "120");
        profile.setPreference("dom.max_script_run_time", "120");
        FirefoxBinary ffox = new FirefoxBinary(firefoxPath);
        ffox.setEnvironmentProperty("DISPLAY", ":21");
        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
        WebDriverEventListener eventListener = new MyEventListener();
        driver = new EventFiringWebDriver(new FirefoxDriver(ffox, profile, dc)).register(eventListener);
        wait = new WebDriverWait(driver, timeoutInSeconds);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


        final String FILE_PATH = "C:\\250.csv";

            CSVReader reader = new CSVReader(new FileReader(FILE_PATH));
            ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
            String[] nextLine;
            while ((nextLine = reader.readNext()) != null) {
                ArrayList<String> list = new ArrayList<String>();
                for (int i = 0; i < 5; i++) { // 5 is the number of sheets
                    list.add(nextLine[i]);
                }
                array.add(list);

            }
            instance = new AppTest3(); 
            instance.oneUserTwoUser(driver, wait, array);
            reader.close();


        Object[][] setUp = new Object[1][3];
        setUp[0][0] = driver;
        setUp[0][1] = wait;
        setUp[0][2] = array;
        return setUp;
}
4

2 回答 2

1

@DataProvider来自TestNG,是的,它需要是一个二维数组(因此可以从不同的数据集多次运行测试)

你做错了什么是你的@Parameters(...)线。Parameters 注释和 DataProvider 注释用于相同的目的,但它们以不同的方式实现。

但是,我也对你的 setUp 函数感到困惑......因为 DataProvider 函数不应该接受参数(除了Method)。考虑一种不同的方式,ArrayList<ArrayList<String>>例如这篇文章中解释的方式。

最后,您评论说该课程是公共抽象的……您的测试与您的测试在同一个课程中@DataProvider吗?如果不是,带有 的类@Test必须扩展带有 的类@DataProvider。(我建议将其抽象化,但这取决于你)

于 2013-08-09T12:28:23.143 回答
0

我不知道 Selenium API,但根据你的堆栈跟踪,如果你自动注入参数并且它抛出了那个错误,你可能想试试这个。

    Objec[] data = new Object[4];
    data[0] = driver;
    data[1] = wait;
    data[2] = firstname;
    data[3] = lastname;

将其作为多维数组没有逻辑意义,因为数组本身就是 Java 中的对象,并且将参数作为Object数组传递的注入方法可能会考虑到这一点。因此,firstnameandlastname数组可以像处理Object该一维数组中的任何后代一样容易。

于 2013-08-09T11:20:23.800 回答