Thursday, 9 February 2017

SELENIUM: How to get error messages in selenium webdriver



How to get error messages in selenium web driver:


We know that  Testing is nothing Verification and validation.
In testing we verify title, some error messages , tooltip messages and so on.
To capture error message or simple text as well we can use predefined method called  getText() which is available in WebElement interface.
This getText() will return the text in the form of String.

Syntax: public String  getText()
ex: String message=
driver.findElement(By. xpath("//span[@id='errormsg_0_Passwd']"))getText();

Example:
package com.rameshsoft.rameshselenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ErrorMessageDemo {
          public static void main(String[] args) throws InterruptedException
          {
                   System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
                   WebDriver driver=new FirefoxDriver();                  
                   driver.manage().window().maximize();                  
                   driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("rameshsoft.selenium@gmail.com");                  
                   driver.findElement(By.id("next")).click();            
                   Thread.sleep(2000);
                   driver.findElement(By.id("Passwd")).sendKeys("12345");
                   driver.findElement(By.id("signIn")).click();                                   
                   String message=driver.findElement(By.xpath(".//span[@id='errormsg_0_Passwd']")).getText();
                   System.out.println(message);
                  
                   driver.quit();
                  
}
}

No comments:

Post a Comment