Cucumber / Selenium:使用try catch块阻止误报

时间:2017-03-15 13:35:47

标签: java selenium xpath 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会捕获异常,说会议创建失败,但黄瓜仍然会将其报告为传递,因为它实际上正确执行了该方法。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我相信如果测试执行中的某个地方出现断言失败,Cucumber将仅报告失败。您可以在某处添加(合法)断言来验证您期望看到的内容。