junit TestWatcher失败并完成了方法的触发时间 [英] junit TestWatcher failed and finished methods firing time

查看:70
本文介绍了junit TestWatcher失败并完成了方法的触发时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Webdriver编写一些功能测试,并使用JUnit执行它们.我试图利用TestWatcher类,以便每次发生事件时都可以执行特定操作.我重写了完成和失败的方法,但是似乎它们实际上是同时被触发的,因此驱动程序已经在完成失败的方法之前就已经由finish方法处置了.

I am writing some functional tests with webdriver and executing them with JUnit. I am trying to make use of TestWatcher class so everytime an event occurs a specific action could be done. I am overriding the finished and failed methods, but it seems to be that these are fired virtually simultaneously, so the driver has been already disposed by finish method before the failed method could do it's stuff.

TestWatcher正在关注:

The TestWatcher is following:

public class TestRules extends TestWatcher {

private static WebDriver driver;
private static final Logger Log = LogManager.getLogger(TestRules.class);

public TestRules() {
    if (driver == null)
        try {
            driver = TestHelpers.getWebDriver();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
}

@Override
protected void starting(Description description){
    Log.info(String.format("Started test: %s::%s", description.getClassName(), description.getMethodName()));
    try {
        new LoginTest().testDoLogin();
    } catch (Exception e) {
        Log.error(e);
    }
}

@Override
protected void failed(Throwable e, Description description) {
    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

    String path = String.format("C:\\localdev\\screenshot\\%d__%s_%s.png",
            Calendar.getInstance().getTimeInMillis(), description.getClassName(), description.getMethodName());
    try {
        FileUtils.copyFile(scrFile, new File(path));
    } catch (IOException ioe) {
        Log.error(ioe);
    }
    Log.error(String.format("Error: %s\nScreenshot: %s", e.getMessage(), path));
}

@Override
protected void finished(Description description) {
    Log.info(String.format("Finished test: %s::%s", description.getClassName(), description.getMethodName()));
    try {
        // This actually calling driver.quit() <- (driver == WebDriver)
        TestHelpers.close();
    } catch (Exception e) {
        Log.error(e);
    }
}

}

测试可能是这样的:

public class testSomething {
private static final Logger Log = LogManager.getLogger(testSomething.class);

@Rule
public TestRules testRules = new TestRules();

@Test
public void filterTableByNameWildcard() throws Exception {
    Log.info("Starting the test filterTable");

    MainView mainView = getMainView();
    String searchString = "A*";
    mainView.setFilterParametersAndDoFilter(searchString, "", true, true);
    mainView.validateWildCardSearchReturnsCorrectData(searchString);
    // Random failing point for testing
    Assert.assertTrue(false);

运行测试时,出现以下错误:

When i run my test, i am getting a following error:

java.lang.AssertionError: 
Expected :true
Actual   :false
 <Click to see difference>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:789)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at xxx.Tests.xxx.testSomething.filterSomething(TestSomething.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
.... skipped


org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:33:32'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_21'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569)
    at org.openqa.selenium.firefox.FirefoxDriver.getScreenshotAs(FirefoxDriver.java:316)
    at xxx.TestHelpers.TestRules.failed(TestRules.java:47)
    at org.junit.rules.TestWatcher.failedQuietly(TestWatcher.java:84)
    at org.junit.rules.TestWatcher.access$300(TestWatcher.java:46)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:62)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)

有什么想法可以通过吗?

Any ideas how to get pass it?

来自文档:

保护无效完成(描述说明) 测试方法完成(通过或失败)时调用

protected void finished(Description description) Invoked when a test method finishes (whether passing or failing)

受保护的无效无效(可抛出e,描述说明) 测试失败时调用

protected void failed(Throwable e, Description description) Invoked when a test fails

这是有道理的,首先调用失败",然后调用完成",但这似乎是相反的.

It would make sense, that firstly the "failed" is called and after that, the "finished" is called, but it seems to be the other way around.

推荐答案

您的问题是一个共享资源,并非每次执行时都会新鲜创建.我创建了一个小例子来说明这一点.看看我对您的评论的评论.

Your problem is a shared resource that isn't created fresh at every execution. I created a small example that will show this. Take a look at my comment to your comment.

这篇关于junit TestWatcher失败并完成了方法的触发时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆