关闭浏览器在几个测试类之后。

huangapple 未分类评论51阅读模式
英文:

Close browser after several test classes

问题

我在一个自动化测试框架上工作,我需要在多个测试类的测试通过后关闭浏览器。

项目的包结构类似于这样:

  • PackageA
    • SomeTestClass1
    • SomeTestClass2
    • ...
  • PackageB
    • SomeOtherTestClass1
    • SomeOtherTestClass2
    • ...

注意:一个包内部可以有多层次的子包。

如果我使用 @AfterClass 或 @AfterAll JUnit 注解,我必须在每个测试类的 'end' 处关闭浏览器。
我的框架要求在执行结束时仅关闭一次已经启动的浏览器(作为静态变量),以加快执行速度并避免在每次浏览器启动和登录时耗费时间,因为我目前有大量需要在每个夜晚运行的测试。

是否有可能实现这种行为?

英文:

I work on an automated testing framework and I need to close the browser after passing tests on several test classes.

The package structure of the project is similar to this:

  • PackageA
  • SomeTestClass1
  • SomeTestClass2
  • ...
  • PackageB
  • SomeOtherTestClass1
  • SomeOtherTestClass2
  • ...

Note: There can be multiple levels of packages inside one another.

If I use @AfterClass or @AfterAll JUnit annotations I have to close the browser at the 'end' of each test class.
My framework requires launching the browser (as a static variable) and closing it only one time to speed up execution and avoid losing time for every browser launch and login, as I currently have a big number of tests that need to be run each night.

Is there any possiblity I could achieve this behaviour?

答案1

得分: 0

JUnit 4 中,你可以在与扩展 TestWatcher 抽象类 (org.junit.rules.TestWatcher) 结合使用 Rule 的概念。因此,您无需在一个被 @AfterClass 注解标记的方法中关闭浏览器。

例如:

public class StartFinishTestWatcher extends TestWatcher {
    @Override
    protected void starting(Description description) {
        System.out.println("Starting test: " + description.getDisplayName());
    }

    @Override
    protected void finished(Description description) {
        System.out.println("Finishing test: " + description.getDisplayName());
        // 在这里关闭浏览器
    }
}

然后,在您希望关闭浏览器的测试类中,您可以将此类放入 RuleChain 中。

public class MyTestInWhichIWantBrowserToClose {

    @Rule
    public RuleChain ruleChain = buildTestRuleChain();

    private RuleChain buildTestRuleChain() { // 可以放在一个超类中,设为 protected

        return RuleChain.outerRule(new StartFinishTestWatcher());
    }
}

JUnit 5 中,您可以创建自定义的 Test Suites,并在其中一个基类中实现 BeforeTestExecutionCallback 和(在您的情况下)AfterTestExecutionCallback 接口。

然后,您可以在方法 public void afterTestExecution(ExtensionContext context) 中实现自定义逻辑,以决定何时关闭浏览器。

英文:

In JUnit 4, you could use the concept of Rule in conjunction with extending TestWatcher abstract class (org.junit.rules.TestWatcher).
Hence you won't have to close the browser in a method annotated with @AfterClass.

e.g.

public class StartFinishTestWatcher extends TestWatcher {
    @Override
	protected void starting(Description description) {
		System.out.println("Starting test: " + description.getDisplayName());
	}

	@Override
	protected void finished(Description description) {
		System.out.println("Finishing test: " + description.getDisplayName());
        // close the browser here
    }
}

Then you would use this class in a RuleChain, in the test class(es) where you want to close the browser.

public class MyTestInWhichIWantBrowserToClose {

    @Rule
	public RuleChain ruleChain = buildTestRuleChain();

    private RuleChain buildTestRuleChain() { // could be made protected and put in a superclass

		return RuleChain.outerRule(new StartFinishTestWatcher());
	}
}

In JUnit 5 you could create custom Test Suites and implement BeforeTestExecutionCallback and (in your case) AfterTestExecutionCallback interfaces in one of your base class.

You would then implement the custom logic which decides to close the browser in method public void afterTestExecution(ExtensionContext context).

huangapple
  • 本文由 发表于 2020年4月6日 17:00:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/61056226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定