selenium webdriver detect UI pop up error messages

Tags: , ,



I have this react application being tested using selenium webdriver.

if my login is wrong, how do i detect the text using selenium webdriver? I am unable to find the code/ figure out how to trap the pop up message . ‘authentication failed’

@Test
public void failed_login() {
    System.setProperty("webdriver.chrome.driver",
            "C:\Users\rahul\Downloads\chromedriver_win32_83\chromedriver.exe");
    
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get("http://testingapp.workspez.com");
    driver.manage().window().maximize();
    WebElement username = driver.findElement(By.id("field_email"));
    WebElement password = driver.findElement(By.id("field_password"));
    WebElement login = driver.findElement(By.xpath("//*[text()='Log In']"));
    
    username.sendKeys("wrongemail@gmail.com");
    password.sendKeys("wrongpassword");
    login.click();
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
    String url = driver.getCurrentUrl();
    assertEquals(url, "http://testingapp.workspez.com/login");
}

enter image description here

Answer

You can use below code to verify if authentication failed pop up is displayed or not:

List<WebElement> popUpElement = driver.findElements(By.id("client-snackbar");
if(popUpElement.size() != 0){
  System.out.println("Pop up is Present "+popUpElement.get(0).getText());
}else{
  System.out.println("Pop up is Absent");
}


Source: stackoverflow