Issue
The following code is using for g-mail login.
package gmail_Login; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Login { //public static void main(String[] args) { // TODO Auto-generated method stub // created reference variable for WebDriver WebDriver drv; @Before public void setup() throws InterruptedException { // initializing drv variable using FirefoxDriver drv=new FirefoxDriver(); // launching gmail.com on the browser drv.get("https://gmail.com"); // maximized the browser window drv.manage().window().maximize(); drv.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } @Test public void test() throws InterruptedException { // saving the GUI element reference into a "username" variable of WebElement type WebElement username = drv.findElement(By.id("Email")); // entering username username.sendKeys("fake mail id"); // clicking next button drv.findElement(By.id("next")).click();
}
I have to given fake id then click on next button error message is coming, but how to check error message.
Solution
After clicking on next button get text of error message using below steps:
Find the error message element
WebElement msg=driver.findElement(By.id("errormsg_0_Email"));
Get text of msg
String text=msg.getText();
Assert/Verify the text with expected text
Assert.assertEquals(text,expectedText);
Answered By - Chhagan