Basic Selenium

How to verify Error Message in Selenium Webdriver using Assert

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-

Capture Error Message in Selenium
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 🙂

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

19 thoughts on “How to verify Error Message in Selenium Webdriver using Assert

  1. vijayalakshmi says:

    Now no fire bug and fire path in mozilla , how i can find xpath in mozilla firefox

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

  3. Ashrita says:

    Hey Mukesh, Can I get the tutorial for “growl” in selenium.

    1. Hi Ashrita,

      Sorry, I don’t have.

  4. megha says:

    if u don’t have locator then how will u locate elements??

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

      https://vistasadprojects.com/mukeshotwani-blogs-v2/how-to-write-dynamic-xpath-in-selenium/

  5. 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());

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

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

  7. Piyush Sharma says:

    Exception in thread “main” org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

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

    1. Hi Piyush,

      Please share the application as well.

      1. Nitin says:

        LOL!! Interesting Post!! 😉 😀

    2. Nanasaheb says:

      I think so you may use Thread.sleep(4000) method.
      or implicit/explicit wait.

      1. Its always recommend using WebDriver waits instead of static wait

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.