2015-10-17 101 views
-1

我想要在Selenium WebDriver脚本中的Assert条件失败时继续执行直到结束。在我的代码中,当Assert条件失败时,它将停止执行其余的代码。当Selenium WebDriver中的Assert条件失败时无法继续

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

try 
{ 
    Assert.assertEquals(false, status); 
} 
catch(Exception Ex) 
{ 
    System.out.println("Disabled user can register for workshop : case Failed "); 
} 

回答

0

使用exceptions to control the flow of logic通过代码被认为是不好的做法(你捕捉异常,写一个错误信息,并继续处理)。

而不是使用assertEquals的,你可以直接检查条件:

if (status == false) { 
    System.out.println("Disabled user can register for workshop : case Failed "); 
} 

另外,如果你仍然想在测试失败时打印的附加信息(并停止在测试进一步检查)时,Asserts通常提供了可选的最后一个参数的重载传达这样的信息时,该测试失败:

Assert.assertEquals(false, status, 
"Disabled user can register for workshop : case Failed "); 
0

的JUnit有一个名为ErrorCollector该CA规则n用于在执行结束时收集关于测试和报告的渐进统计信息。

不结合断言和ErrorCollector,你会如果你的测试从声明

未能在测试结束任何故障将被收集并在JUnit输出集中上市失去ErrorCollector信息。

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."); 
    } 

} 

JUnit Result

规则必须:

  • 被注解为@rule
  • 声明在您的测试公共

建议的解决方案:

在你列出你上面应该删除try/catch块,并使用匹配器,而不是org.junit.Assert.assertEquals的情况下:

@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. 
} 

祝你好运!

+0

在Junit截图中,我应该指出两个junit测试是在同一个会话中运行的。 * testErrorCollectorWithAssert *的证明在控制台中被调用,您只能在两个测试中看到一个“完成”语句。其中只有一个完成。 – Jeremiah