[英]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.