0
BeforeClassでパラメータを使用して、異なる役割を持つ別のユーザーとしてログインしたいと考えています。@BeforeClassで@ParametersをJunit用に使用する方法
は、私が使用:
- JUnitParams 1.0.2
- のJUnit 4.12
- セレンwedbriver 2.53.1ここ
私のコードです FirefoxLogin.java
public class FirefoxLogin {
public static WebDriver login(User user, String language){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.example.org");
WebElement loginField = driver.findElement(By.id("username"));
WebElement passwordField = driver.findElement(By.id("password"));
WebElement commit = driver.findElement(By.id("kc-login"));
loginField.sendKeys(user.getLogin());
passwordField.sendKeys(user.getPassword());
commit.click();
setLanguage(driver, user, language);
return driver;
}
public static void logout(WebDriver driver) {
driver.findElement(By.linkText("Logout")).click();
driver.quit();
}
protected static void setLanguage(WebDriver driver, User user, String language) {
// method to set language
}
}
ユーザー。 Javaの
public class User {
public static final User USERONE = new User("loginOne", "passwordOne");
public static final User USERTWO = new User("loginTwo", "passwordTwo");
private String login;
private String password;
User(String login, String password) {
this.login=login;
this.password=password;
}
public String getLogin(){
return login;
}
public String getPassword(){
return password;
}
public static List<User> getUser() {
List<User> users = new ArrayList<>();
users.add(USERONE);
users.add(USERTWO);
return users;
}
}
AddFieldTest.java
@RunWith(JUnitParamsRunner.class)
public class AddFieldTest {
private static WebDriver driver;
private static final Object[] userValues() {
return $(
$(User.USERONE, "en"),
$(User.USERTWO, "en")
);
}
@Parameters(method = "userValues")
@BeforeClass
public static void init(User mUser ,String mLanguage) {
driver = FirefoxLogin.login(mUser, mLanguage);
}
@AfterClass
public static void close() {
FirefoxLogin.logout(driver);
}
private Object[] valuesForTest() {
return $(
$("street", null, null),
$(null, "city", null),
$(null, null, "country"),
$("ulica", "city", null),
$("ulica", "city", "country")
);
}
@Test
@Parameters(method = "valuesForTest")
public void test(String mStreet, String mCity, String mCountry) {
}
}
ここでは、ミュー出力されます: java.lang.IllegalArgumentExceptionが:引数の数が間違っ
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)