Java:如何将两个String []对象放入一个对象并将它们传递给下一个方法?

时间:2013-08-09 10:49:45

标签: java testng

编辑:我对代码进行了非常重的重构,我解决了这个问题。固定代码低于错误的代码。

我有这两个:

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;
}

2 个答案:

答案 0 :(得分:1)

@DataProvider来自TestNG,是的,它需要是一个2D数组(所以测试可以从不同的数据集中运行多次)

你做错了是你的@Parameters(...)行。参数注释和DataProvider注释用于相同的目的,但它们以不同的方式执行。

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

最后,你发表评论说该课程是公开抽象的......你和@DataProvider在同一个班级考试吗?如果没有,带@Test的班级必须使用@DataProvider扩展班级。 (我建议将其抽象化,但这取决于你)

答案 1 :(得分:0)

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

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

它是一个多维数组没有逻辑意义,因为数组本身是Java中的对象,并且用于传递参数的注入方法可能会考虑到Object数组。因此,firstnamelastname数组可以像处理该一维数组中的任何Object后代一样轻松处理。