简体   繁体   English

如何使用不同的WebDriver配置多次运行junit测试套件

[英]How tu run junit test suite multiple times with different WebDriver configuration

I am using junit to run test on website with Selenium webdriver. 我正在使用junit在Selenium webdriver的网站上运行测试。 What I am trying to achieve is to run same test class multiple times but every time I want to change my login credentials for website so it can be tested with different access permissions. 我想要实现的是多次运行相同的测试类,但每次我想更改我的网站登录凭据,以便可以使用不同的访问权限进行测试。 I dont know how to approach this problem. 我不知道如何解决这个问题。

I am using TestSuite class to specify which test classes should be runned and WebDriver is being initialized in TestRunner class 我正在使用TestSuite类来指定应该运行哪些测试类,并在TestRunner类中初始化WebDriver

testSuite class: testSuite类:

@RunWith(Suite.class)

@Suite.SuiteClasses({
        DhcpReservationTest.class,
})

public class TestSuite {
}

TestRunner class: TestRunner类:

public class TestRunner {
    /**
     * Init web driver
     */
    private static WebDriver driver = DriverManager.createDriver(ProjectSettings.Roles.NETADMIN);

    /**
     * This static method serves for getting instance of web driver for executing tests.
     * @return web driver
     */
    public static WebDriver getWebDriver() {
        return driver;
    }

    static JUnitCore junitCore;
    static Class<?> testClasses;
    public static void main(String[] args) {
  System.out.println("Running Junit Test Suite.");

        junitCore = new JUnitCore();

        junitCore.addListener(new CustomExecutionListener());

        Result result = junitCore.run(TestSuite.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        System.out.println("Successful: " + result.wasSuccessful() + " ran " + result.getRunCount() + " tests");
    }
}

The goal is to change permission role in TestRunner when initializing webdriver from "NETADMIN" to eg "SYSADMIN" etc. everytime when tests are completed and run the same test suite again. 目标是在每次测试完成时将webdriver从“NETADMIN”初始化为例如“SYSADMIN”等时再更改TestRunner中的权限角色,并再次运行相同的测试套件。

Is there some way how to do it or should I approach this differently? 有什么方法可以做到这一点,还是应该以不同的方式处理? Thank you. 谢谢。

I am not good in Java, but I have done similar thing in C# and Nunit. 我不擅长Java,但我在C#和Nunit做过类似的事情。 I had all my test methods in a parent class. 我在父类中拥有所有测试方法。 And had multiple derived classes, which implement the method holding the difference. 并且有多个派生类,它们实现了保持差异的方法。

So you have something like that (pseudocode): 所以你有类似的东西(伪代码):

parentClass{
  [Test]
  testMethod1(){
    login();
    //test things
  }
  [Test]
  testMethod2(){
    login();
    //test other things
  }   
}
[TestSuite]
subclass1()<<parentClass{
  login(){
    //do first type of login
  }
}
subclass2()<<parentClass{
  login(){
    //do second type of login
  }
}

So tests are executed within the 2 derived classes, but they inherit all the test methods from the parent class. 因此,测试在2个派生类中执行,但它们从父类继承所有测试方法。 There will be probably some details that I miss, but this was my approach. 可能会有一些我想念的细节,但这是我的方法。

Actually I had the difference in a beforeEach methods in the derived classes, but I guess the above would be very similar. 实际上我在派生类中的beforeEach方法中有所区别,但我猜上面的内容非常相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不同的URL上使用Selenium和JUnit 5运行整个测试套件 - How to run an entire test suite with Selenium & JUnit 5 on different URLs 如果同一测试多次运行,如何区分junit测试运行于哪个测试套件? - How distinguish from which test suite was junit test run in case the same tests are run more times? 如何多次使用相同的测试数据多次运行同一JUnit测试? - How do I run the same JUnit test multiple times with different test data each time? 使用不同的参数使jUnitRunner运行测试类多次 - Make jUnit runner run test class multiple times with different parameters 在JUnit 4中使用不同的@BeforeClass设置多次运行测试用例 - Run a test case multiple times with different @BeforeClass setups in JUnit 4 使用不同的参数多次运行一个 junit 测试 - Run one junit test multiple times with different parameters 多次运行同一个Selenium测试套件 - Run the same Selenium test suite multiple times 配置文件,以在BrowserStack中的不同浏览器上运行JUnit Suite - Configuration file to run JUnit Suite on different browsers in BrowserStack 在Eclipse中,如何多次运行JUnit测试用例 - In Eclipse, how do I run a JUnit test case multiple times 在Maven中,如何作为测试套件的一部分而不是单独运行JUnit测试用例? - In Maven how to run a JUnit test case as part of a test suite and not alone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM