简体   繁体   中英

How to use @Parameters in @BeforeClass for Junit

I want to use Parameters in BeforeClass to login as a different user with different role.

I use:

  • JUnitParams 1.0.2
  • junit 4.12
  • Selenium wedbriver 2.53.1

Here is my code 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
}
}

User.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)  {

}

}

Here is mu output: java.lang.IllegalArgumentException: wrong number of arguments

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)

In this case the exception is caused by parameters for init method - they are not allowed. Furthermore, you cannot annotate @BeforeClass method with @Parameters - see discussion here .

JUnitParams supports parameterization only of test methods.

@BeforeClass is meant to be a one time action that is supposed to prepare system for tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM