简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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