编辑:我对代码进行了非常重的重构,并修复了这个问题。固定代码位于错误代码下方。
我有这两个:
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;
}