Java Selenium Webdriver中的断言条件失败时无法继续

Java Selenium Webdriver中的断言条件失败时无法继续,java,selenium,assert,Java,Selenium,Assert,当SeleniumWebDriver脚本中的断言条件失败时,我希望执行继续到结束。在我的代码中,当Assert条件失败时,它将停止执行其余代码 这是我写的代码。我需要帮助 try { Assert.assertEquals(false, status); } catch(Exception Ex) { System.out.println("Disabled user can register for workshop : case Failed "); } 通过代码使用被认为

当SeleniumWebDriver脚本中的断言条件失败时,我希望执行继续到结束。在我的代码中,当
Assert
条件失败时,它将停止执行其余代码

这是我写的代码。我需要帮助

try
{
    Assert.assertEquals(false, status);
}
catch(Exception Ex)
{
    System.out.println("Disabled user can register for workshop : case Failed ");
}
通过代码使用被认为是错误的做法(您正在捕获异常、编写错误消息并继续处理)

您可以直接检查条件,而不是使用
assertEquals

if (status == false) {
    System.out.println("Disabled user can register for workshop : case Failed ");
}
此外,如果您仍希望在测试失败时打印附加信息(并停止测试中的进一步检查),
断言通常为可选的最后一个参数提供重载,以便在测试失败时传递此类信息:

Assert.assertEquals(false, status, 
 "Disabled user can register for workshop : case Failed ");
JUnit有一个名为ErrorCollector的规则,可用于在执行结束时收集有关测试和报告的渐进统计信息

不要将Assert和ErrorCollector组合在一起,如果Assert导致测试失败,您将丢失ErrorCollector信息

在测试结束时,任何失败都将被收集并在JUnit输出中集体列出


祝你好运

在Junit屏幕截图中,我应该指出两个Junit测试是在同一个会话中运行的。testErrorCollectorWithAssert的证明在控制台中被调用,在控制台中,您只能看到两个测试的一个“Done”语句。其中只有一个完成了。
package stackoverflow.proof.junit;

import org.hamcrest.core.Is;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNull;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

/**
 * Test case for demonstrating some Junit Rule behaviors.
 * <p/>
 * Requires at least JUnit 4.7
 * 
 * @see "http://stackoverflow.com/questions/33183789/not-able-to-continue-when-an-assert-condition-fails-in-selenium-webdriver/33183882#33183882"
 * @author http://stackoverflow.com/users/5407189/jeremiah
 * @since Oct 17, 2015
 *
 */
public class JUnitRulesDemoTest {
    /**
     * Collector which can be used to track problems through an event series and still allow a test to run to
     * completion.
     */
    @Rule
    public ErrorCollector collector = new ErrorCollector();

    /**
     * Test illustrating ErrorCollector behavior.
     * 
     */
    @Test
    public void testErrorCollector() {
        collector.checkThat("These things don't match", new Object(), IsEqual.equalTo(new Object()));
        collector.checkThat(new Object(), IsNull.notNullValue());
        try {
            throw new Exception("Demonstration Exception in collector");
        } catch (Exception ex) {
            collector.addError(ex);
        }
        collector.checkThat("Status does not match!", true, Is.is(true));
        System.out.println("Done.");
    }
    
    /**
     * Test illustrating ErrorCollector behavior when an Assert Fails the Test.
     * 
     */
    @Test
    public void testErrorCollectorWithAssert() {
        collector.checkThat("These things don't match", new Object(), IsEqual.equalTo(new Object()));
        collector.checkThat(new Object(), IsNull.notNullValue());
        try {
            throw new Exception("Demonstration Exception in collector");
        } catch (Exception ex) {
            collector.addError(ex);
        }
        //Test stops here.  You won't see "done", nor will you see any of the previous ErrorCollector status'
        Assert.assertTrue(false);
        
        collector.checkThat("Status does not match!", true, Is.is(true));
        System.out.println("Done.");
    }

}
@Rule
public ErrorCollector collector = new ErrorCollector();

@Test
public void testSomething() {
  boolean status = doStatusEval(); 
  collector.checkThat("Disabled user can register for workshop", false, Is.is(status));
 //Continue adding things to the collector.
}