简体   繁体   English

如何捕获 Selenium 错误消息

[英]How can I catch the Selenium error message

I want to check if I'm getting an error message when navigating between pages.When the error message comes, the test will be terminated and reporting will be made.For this, I use the following element related to the error message on the site.我想检查在页面之间导航时是否收到错误消息。当错误消息出现时,测试将终止并进行报告。为此,我使用以下与站点上的错误消息相关的元素.

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("-----");
            }

However, when I set up this structure, if there is an error on the page, the test is successful, if there is no error, the test is unsuccessful.但是,当我设置这个结构时,如果页面有错误,则测试成功,如果没有错误,则测试不成功。 Therefore, it is not possible to switch to a different page.How can I control this point?因此,无法切换到不同的页面。如何控制这一点?

Well i would ask for HTML to see if the path your giving it is unique for your case i can suggest using Assert if you want the code to stop once the error is shown or soft assert if you want your code to continue even if there is the error with assertall() at the end.好吧,我会要求 HTML 看看你给它的路径是否对你的情况是唯一的,如果你希望代码在显示错误后停止,我可以建议使用 Assert,如果你希望代码继续,即使有最后带有 assertall() 的错误。 The way i control this:我控制这个的方式:

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>");
                }

If my code doesnt find the element then it will acces the soft assert the ScreenShot is just to take a picture of the page when the error was shown.如果我的代码没有找到该元素,那么它将访问软断言,ScreenShot 只是为了在显示错误时为页面拍照。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM