Hello Welcome to Selenium tutorial, in this post we will see how to verify Error Message in Selenium.
Testing is nothing but verification and validation. Mostly 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().
getText() method will simply capture the error message or text and will return you a String then you can store in String variable and you can use in other application or you can verify as well.
Please refer below youtube video for the same.
Program for Verify Error Message in Selenium Webdriver
package demo; import org.openqa.selenium.By; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; public class TestNaukri { @Test public void TestError() { // Open browser FirefoxDriver driver=new FirefoxDriver(); // maximize browser driver.manage().window().maximize(); // Open URL driver.get("http://www.naukri.com/"); // Click on login button driver.findElement(By.id("p0submit")).click(); // This will capture error message String actual_msg=driver.findElement(By.id("emailId_err")).getText(); // Store message in variable String expect="plz enter valid email"; // Here Assert is a class and assertEquals is a method which will compare two values if// both matches it will run fine but in case if does not match then if will throw an //exception and fail testcases // Verify error message Assert.assertEquals(actual_msg, expect); } }
Output-
You can use getText() method for capturing simple text messages as well and using above method we can verify the same.
Note- If text does not match then TestNG will throw AssertionError and if we do not use Exception handling then it will simply terminate our application/ program.
For complete info visit TestNG Official site
Thanks for visiting my blog. Please comment if you finding any issue. Keep visiting.
Have a nice day 🙂
vijayalakshmi says
Now no fire bug and fire path in mozilla , how i can find xpath in mozilla firefox
Mukesh Otwani says
Hi Vijaylakshmi,
Use ChroPath addon https://addons.mozilla.org/en-US/firefox/addon/chropath-for-firefox/
Nish says
Hi Mukesh,
Can you help me in the Dataprovider related code .My requirement is to validate the login credentials i.e.username and password with different values by using Dataprovider. The values which I am giving as input are in browser.
Mukesh Otwani says
Hi Nish,
I hope this link http://learn-automation.com/data-driven-framework-in-selenium-webdriver/ will help you to get your requirement.
Ashrita says
Hey Mukesh, Can I get the tutorial for “growl” in selenium.
Mukesh Otwani says
Hi Ashrita,
Sorry, I don’t have.
megha says
if u don’t have locator then how will u locate elements??
Mukesh Otwani says
I Megha,
You have multiple options you can use Text you can use location and you can use following method of xpath.
I would suggest you to go through Xpath part 1 and Xpath part 2 for more details.
http://learn-automation.com/how-to-write-dynamic-xpath-in-selenium/
Gaurav Khurana says
I have pasted the code to help others as now the naukri site has changed little bit and the above code may not work
String str=”http://www.naukri.com”;
drv.get(str);
WebElement lgnBtn1=drv.findElement(By.linkText(“Login”));
lgnBtn1.click();
WebElement lgnBtn2=drv.findElement(By.xpath(“//button[@value=’Login’]”));
lgnBtn2.click();
WebElement lgnBtn3=drv.findElement(By.id(“eLogin_err”));
WebElement lgnBtn4=drv.findElement(By.id(“pLogin_err”));
Assert.assertEquals(lgnBtn3.getText(),”Please enter your Email ID”);
Assert.assertEquals(lgnBtn4.getText(),”Please enter your Password”);
Thread.sleep(10000);
WebElement lgnBtn5=drv.findElement(By.id(“fLogin_err”));
System.out.println(“There is one more error on the screen :- ” + lgnBtn5.getText());
Mukesh Otwani says
Thanks Gaurav.
ranjit says
Its a best site i am referring, easy also comments.
I am verifying a couple of pages similar way, but would like to know i have 10 left menus i have to verify like this, then what is the procedure and if there is error on any page, how can i be notified with screenshot of tht.
Thanks,
Ranjit
Mukesh Otwani says
Hey Ranjit,
in this case get all elements in List then iterate using for loop and keep assert accordingly. If test fails then you can capture screenshot.
Piyush Sharma says
Exception in thread “main” org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Mukesh Otwani says
Hi Piyush,
Here you go.
Solution
http://learn-automation.com/solve-elementnotvisibleexception-in-selenium-webdriver/
Piyush Sharma says
Mera dimag kharab hi raha hai .. locators maine ek baar nahi das baar dekh liye… saala locators find hi nahi kar pata .. dimag kaa maa bahen ho raha hai
driver.findElement(By.cssSelector(“input[id=’sbtLog’][name=’submit1′]”)).click();
driver.findElement(By.cssSelector(“input[id=’sbtLog’][name=’submit1′]”)).click();
driver.findElement(By.xpath(“//*[@id=’sbtLog’]”)).click();
muje ek baat bataoo yadi browser puri tarike se load nahi hota tab kya element locate main dikat padti hai ?
Mukesh Otwani says
Hi Piyush,
Please share the application as well.
Nitin says
LOL!! Interesting Post!! 😉 😀
Nanasaheb says
I think so you may use Thread.sleep(4000) method.
or implicit/explicit wait.
Mukesh Otwani says
Its always recommend using WebDriver waits instead of static wait