为每个硒测试处理多个用户

时间:2015-12-01 15:05:41

标签: java selenium selenium-webdriver junit

我正在使用org.databene.benerator.anno.Source来处理每个测试的不同用户登录。例如:

@Test
@Source("allUsers.csv")
@Category(Deep.class)
public void testRenameFunctionality(String user, String password){
    login(user, password);
    createPlan.createPlan();
    planDetails.createDestinationReport();
    checkRenameFunctionality();
}

不同的用户位于csv文件(allUsers.csv)中,并与文件中的每个用户一起运行测试。这一直很好,直到我尝试将多浏览器测试与酱油结合起来。这是我的基类,它具有我的多浏览器凭据:

private String platform;
private String browserName;
private String browserVersion;

public BaseTest(String platform, String browserName, String browserVersion){
    super();
    this.browserName = browserName;
    this.browserVersion = browserVersion;
    this.platform = platform;
}

@Parameterized.Parameters
public static LinkedList data() throws Exception{
    LinkedList browsers = new LinkedList();

    if(host.equals("saucelabs")){
        browsers.add(new String[] {"firefox", "42", "Windows 7"});
        browsers.add(new String[] {"chrome", "46", "Windows 7"});
        browsers.add(new String[] {"internet explorer", "11", "Windows 7"});
    }else if(host.equals("localhost")){
        browsers.add(new String[] {"", "", ""});
    }

    return browsers;
}

//Rule for the before and after annotations.
@Rule
public ExternalResource resource = new ExternalResource() {
    @Override
    protected void before() throws Throwable {
        if(host.equals("saucelabs")){
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("browserName", browserName);
            capabilities.setCapability("version", browserVersion);
            capabilities.setCapability("platform", platform);
            capabilities.setCapability("name", testName);
            capabilities.setCapability("resolution","1280x1024");
            String sauceUrl = String.format("http://%s:%s@ondemand.saucelabs.com:80/wd/hub",
                    sauceUser, sauceKey);
            driver = new RemoteWebDriver(new URL(sauceUrl), capabilities);
            driver.manage().window().maximize();
            sessionId = ((RemoteWebDriver) driver).getSessionId().toString();
            sauceClient = new SauceREST(sauceUser, sauceKey);
        }else if(host.equals("localhost")){
            driver = new FirefoxDriver();
            driver.manage().window().maximize();
        }
    }

    @Override
    protected void after(){
        driver.quit();
    }
};

我也在其他测试类中扩展BaseTest:

public class NavAppsTest extends BaseTest implements Config {

NavAppObject selectApp;

public NavAppsTest(String platform, String browserName, String browserVersion) {
    super(platform, browserName, browserVersion);
}

这适用于没有参数的测试类,但是在尝试使用参数运行测试时我得到了这个initialError:

java.lang.Exception: Test class should have exactly one public zero-argument constructor
at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:171)
at org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:148)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at org.databene.feed4junit.Feeder.<init>(Feeder.java:66)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)

每个测试类都在扩展我的BaseTest,所以他们都有一个获取浏览器/操作系统信息的构造函数。我认为解决这个问题的最佳方法是找出一种不同的方法来处理处理每个测试用例的每个用户。有什么想法吗?谢谢!

布莱恩

0 个答案:

没有答案