【问题标题】:How can I catch the Selenium error message如何捕获 Selenium 错误消息
【发布时间】:2022-01-08 18:25:33
【问题描述】:

我想检查在页面之间导航时是否收到错误消息。当错误消息出现时,将终止测试并进行报告。为此,我使用以下与错误消息相关的元素在网站上。

try {
                WebElement element=driver.findElement(By.className("p-toast-message-text"));
                element.isDisplayed();
                boolean s= element.isDisplayed();
                System.out.println(s);
            }
            catch (NoSuchElementException e) {
                throw new RuntimeException("-----");
            }

但是,当我设置这个结构时,如果页面有错误,则测试成功,如果没有错误,则测试不成功。因此,无法切换到不同的页面。如何控制这一点?

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    好吧,我会要求 HTML 来查看您给它的路径对于您的情况是否是唯一的,如果您希望代码在显示错误后停止,我可以建议使用 Assert,如果您希望代码继续,我可以建议使用软断言如果最后有 assertall() 错误。 我控制这个的方式:

    Boolean NoCanceledAppointement = driver.findElements(By.xpath("//div[@class=\"empty-result\"]/h2[normalize-space(text())=\"Vous n’avez pas de Rendez-vous annulés !\"]")).size() > 0;
                    if(NoCanceledAppointement == true) {
                        Reporter.log(FormatMessageSucces + " No Canceled Appointement Exist</font>");
                    }
                    else {
                        Sa.assertEquals(false, true, "The Page didn't show the message of 'You have no canceled appointments!'");
                        ScreenShot(driver);
                        Reporter.log(FormatMessageError + " The Page didn't show the message of 'You have no canceled appointments!'</font>");
                    }
    

    如果我的代码没有找到该元素,那么它将访问软断言,ScreenShot 只是为了在显示错误时为页面拍照。

    【讨论】:

      猜你喜欢
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 2012-07-08
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      相关资源
      最近更新 更多