【问题标题】:Cucumber/Selenium: Stopping a false positive with a try catch blockCucumber/Selenium:使用 try catch 块停止误报
【发布时间】:2023-03-07 03:28:01
【问题描述】:

我正在使用 selenium 和 cucumber 来自动化 Web 应用程序,我目前对 cucumber 发送虚假通道以及 API 的构建方式有点困惑。基本上我在 try catch 块中封装了许多命令,虽然它工作正常,但如果 xpath 要更改(不太可能),我希望该步骤基本上“失败”但不完全杀死程序。这是我的代码所在的位置:

@Then("^Create a meeting$")
public void meetingCreation() throws Throwable{
    try{
        //check to make sure driver is on calendar page

        ieBreakingThings = ieDriverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@ng-click='CreateMeeting()'][@aria-label='Add Meeting']")));
        chromeBreakingThings = chromeDriverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@ng-click='CreateMeeting()'][@aria-label='Add Meeting']")));

        if (!ieBreakingThings.isDisplayed() || !chromeBreakingThings.isDisplayed())
        {
            chromeDriver.findElement(By.xpath("//div[@class='navigationLinkIcon icon-calendar']")).click();
            ieDriver.findElement(By.xpath("//div[@class='navigationLinkIcon icon-calendar']")).click();
            ieDriver.findElement(By.xpath("//button[@ng-click='CreateMeeting()'][@aria-label='Add Meeting']")).click();
            chromeDriver.findElement(By.xpath("//button[@ng-click='CreateMeeting()'][@aria-label='Add Meeting']")).click();
        }
        else
        {   
            ieDriver.findElement(By.xpath("//button[@ng-click='CreateMeeting()'][@aria-label='Add Meeting']")).click();
            chromeDriver.findElement(By.xpath("//button[@ng-click='CreateMeeting()'][@aria-label='Add Meeting']")).click();
        }
        //fill in text fields
        ieBreakingThings = ieDriverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name = 'meetingName'][@ng-model='MeetingName']")));
        chromeBreakingThings = chromeDriverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name = 'meetingName'][@ng-model='MeetingName']")));
        chromeDriver.findElement(By.xpath("//input[@name = 'meetingName'][@ng-model='MeetingName']")).click();
        chromeDriver.findElement(By.xpath("//input[@name = 'meetingName'][@ng-model='MeetingName']")).sendKeys("Web Release Test Meeting");
        ieDriver.findElement(By.xpath("//input[@name = 'meetingName'][@ng-model='MeetingName']")).click();
        ieDriver.findElement(By.xpath("//input[@name = 'meetingName'][@ng-model='MeetingName']")).sendKeys("Web Release Test Meeting");
        chromeDriver.findElement(By.xpath("//input[@ng-model = 'MeetingLocation']")).click();
        chromeDriver.findElement(By.xpath("//input[@ng-model = 'MeetingLocation']")).sendKeys("some address");
        ieDriver.findElement(By.xpath("//input[@ng-model = 'MeetingLocation']")).click();
        ieDriver.findElement(By.xpath("//input[@ng-model = 'MeetingLocation']")).sendKeys("some address");
    }catch(TimeoutException | ElementNotVisibleException ex){
        System.out.println("Meeting Creation Failed");
    }
    }

所以上述工作正常,但是如果要更改 xpath,它将捕获异常,例如会议创建失败,但 cucumber 仍会报告它通过,因为它实际上正确执行了该方法。

有什么建议吗?

【问题讨论】:

  • 您还应该考虑其他可能的异常,例如 NoSuchElementException 或 NullPointerException 以及一般的包罗万象,否则该异常将被忽略。
  • 我不认为 N​​PE 会在这个区块发生?但绝对是 NoSuchElementException。
  • 在重新阅读您的帖子后,您提到它确实检测到了 XPath 更改,但在 Cucumber 中没有捕捉到它。也许 ElelementNoVisibleException 正在工作。您可以考虑尝试放置一个 Assert.assertFail();在 catch 块中,我认为 Cucumber 会检测到它。我使用类似的 JBehave,并且确实如此。
  • Assert.fail() 确实允许 cucumber 将其识别为失败的步骤,但它也会停止任何进一步的执行,这是我不想要的。我希望黄瓜将其识别为失败,但继续执行程序的其余部分。
  • 如果您使用的是 TestNG(不幸的是,我们使用的是 JUnit,它不支持我将要建议的内容),您可以使用软断言。它相当于验证命令,允许您继续。我不记得语法。对于像 Cucumber/JBehave 这样从事 BDD 的人来说,通常 JUnit/TestNG 不会受到质疑,但如果你还没有到 TestNG,这将是我转向 TestNG 的一个原因。

标签: java selenium xpath cucumber


【解决方案1】:

我相信 Cucumber 只会在测试执行中的某个地方出现失败的断言时才会报告失败。你可以在某处添加一个(合法的)断言来验证你期望看到的东西。

【讨论】:

  • 正确,如果我在 catch 块中放入一个失败的断言来测试异常是否正常工作,它会工作,但会停止所有进一步的执行。
  • 我正在为我的代码寻找“内置调试”,所以它会说该方法失败,但继续执行,以便我可以快速转到该方法并查看问题所在是。
  • 很遗憾,我无法对您的问题本身发表评论,但我同意 Bill Hileman 关于使用 TestNG 的 SoftAssert:testng.org/javadocs/org/testng/asserts/SoftAssert.html
  • @TrevorScanlon 您应该查看 junit4 的 ErrorCollector 规则...由于规则在 cucumber 中不起作用,您需要扩展 ErrorCollector 类。覆盖 verify() 方法,将其公开并使用 super.verify() 调用超类方法。您可以使用 addError() 方法添加错误,最后调用 verify()...
猜你喜欢
  • 1970-01-01
  • 2016-05-04
  • 1970-01-01
  • 2019-12-04
  • 2013-02-13
  • 2017-01-30
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
相关资源
最近更新 更多