Cucumber/Selenium:使用 try catch 块停止误报
Cucumber/Selenium: Stopping a false positive with a try catch block
我正在使用 selenium 和 Cucumber 来自动化 Web 应用程序,目前我对 Cucumber 发送错误通行证以及 API 的构建方式有点困惑。基本上,我将大量命令封装在一个 try catch 块中,虽然它工作正常,但如果 xpath 发生变化(不太可能),我希望这个步骤基本上 "fail" 但不会完全终止程序。这是我的代码所在的位置:
@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 仍然会报告它通过,因为它实际上正确地执行了该方法。
有什么建议吗?
我相信只有在测试执行中某处存在失败断言时,Cucumber 才会报告失败。您可以在某处添加(合法的)断言来验证您期望看到的内容。
我正在使用 selenium 和 Cucumber 来自动化 Web 应用程序,目前我对 Cucumber 发送错误通行证以及 API 的构建方式有点困惑。基本上,我将大量命令封装在一个 try catch 块中,虽然它工作正常,但如果 xpath 发生变化(不太可能),我希望这个步骤基本上 "fail" 但不会完全终止程序。这是我的代码所在的位置:
@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 仍然会报告它通过,因为它实际上正确地执行了该方法。
有什么建议吗?
我相信只有在测试执行中某处存在失败断言时,Cucumber 才会报告失败。您可以在某处添加(合法的)断言来验证您期望看到的内容。