【发布时间】: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 以及一般的包罗万象,否则该异常将被忽略。
-
我不认为 NPE 会在这个区块发生?但绝对是 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