我一直在尝试使用Selenium中的"Verify“选项,而不是"Assert”选项。我的想法是,使用验证选项,我的测试可以完全运行,然后获得错误列表。在尝试这样做的过程中,我意识到“代码明智”的Verify选项与Assert相同,但它只是被try-catch块所包围。我已经尝试了各种方法来实现这一点,但我面临的问题是,测试仍然会在中断的语句上停止,而不是给我一个错误列表。
我的结构是:
public class SelTests {
public StringBuffer verificationErrors = new StringBuffer();
public WebDriver driver;
@BeforeTest
public void setup() throws Exception {
// DO SETUP STUFF
}
@Test
public void TestScript1() throws Exception {
try { //assertEquals("string1", "string2") } catch (Exception e) {verificationErrors.append(e.toString());}
try { //assertEquals("string1", "string2") } catch (Exception e) {verificationErrors.append(e.toString());}
try { //assertEquals("string1", "string2") } catch (Exception e) {verificationErrors.append(e.toString());}
@AfterTest
public void tearDown() throws Exception{
driver.close();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
System.out.println(verificationErrorString);
}
}但即使在对其进行了很多方面的修改之后,我仍然有同样的问题。我还尝试将整个assert语句块包围起来,而不是像上面的代码那样单独执行它们,但都不起作用。我故意在一些断言中引入错误,但每次我只得到1个错误的日志。
环境:
Selenium 2.30 Server-standalone
TestNG annotations and frameworks
Java code using WebDriver implementations
Eclipse
Windows 7 - 64bit (this should not matter though)在引导我实现我正在尝试实现的目标方面,任何帮助都将不胜感激。
谢谢,
Alister
发布于 2013-02-28 12:15:46
它没有进入catch块的原因是断言抛出了AssertionErrors。错误与异常是不同的。当你捕捉到一个异常时,你并没有捕捉到AssertionError。要么将catch块异常e更改为AssertionError,要么将其更改为Throwable,这将意味着任何异常或错误。
https://stackoverflow.com/questions/15124163
复制相似问题