【问题标题】:Getting a list of errors for a Selenium Test as opposed to Test stopping on a single error using the try-catch block获取 Selenium 测试的错误列表,而不是使用 try-catch 块在单个错误上停止测试
【发布时间】:2013-02-13 23:20:39
【问题描述】:

我一直在尝试使用 Selenium 中的“验证”选项而不是“断言”选项。我的想法是使用验证选项是我的测试可以完全运行,然后获取错误列表。在尝试这样做时,我意识到“代码明智”的验证选项与 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);    
        }    
    }

但即使在通过多种方式修改后,我仍然遇到同样的问题。我还尝试包围整个断言语句块,而不是像上面的代码中那样单独执行它们,但没有任何效果。我故意在一些断言中引入了错误,但每次我只得到 1 个错误的日志。

环境:

Selenium 2.30 Server-standalone
TestNG annotations and frameworks
Java code using WebDriver implementations
Eclipse
Windows 7 - 64bit (this should not matter though)

任何帮助指导我实现我想要实现的目标将不胜感激。

谢谢,

阿利斯特

【问题讨论】:

标签: java selenium error-handling try-catch testng


【解决方案1】:

它没有进入 catch 块的原因是 Asserts throw AssertionErrors。错误不同于异常。当您捕获异常时,您不会捕获 AssertionError。将 catch 块 Exception e 更改为 AssertionError 或 Throwable,这意味着任何异常或错误。

【讨论】:

  • Niharika,这对我有用。我相信我的问题是,由于某种原因,我的控制台没有显示堆栈跟踪的完整列表,这将显示所有错误。我之前尝试过 throwable ,但仍然无法看到错误。但是在深入探索控制台后,我发现它实际上打印了所有错误,但没有向我显示完整的消息。此外,为了给任何有类似问题的人补充这一点,我尝试将 catch 块异常更改为“AssertionError e”,但它的行为并不像我想要的那样,也不像“Throwable e”那样。
猜你喜欢
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 2016-05-04
  • 2010-11-14
  • 2018-06-12
相关资源
最近更新 更多