• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / How to verify Error Message in Selenium Webdriver using Assert

How to verify Error Message in Selenium Webdriver using Assert

March 21, 2015 by Mukesh Otwani 19 Comments

Capture Error Message in Selenium

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 🙂

Filed Under: Basic Selenium Tagged With: Error message

Reader Interactions

Comments

  1. vijayalakshmi says

    July 18, 2019 at 4:03 PM

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

    Reply
    • Mukesh Otwani says

      July 18, 2019 at 9:37 PM

      Hi Vijaylakshmi,

      Use ChroPath addon https://addons.mozilla.org/en-US/firefox/addon/chropath-for-firefox/

      Reply
  2. Nish says

    April 14, 2019 at 12:31 AM

    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.

    Reply
    • Mukesh Otwani says

      April 14, 2019 at 9:03 AM

      Hi Nish,

      I hope this link http://learn-automation.com/data-driven-framework-in-selenium-webdriver/ will help you to get your requirement.

      Reply
  3. Ashrita says

    February 17, 2017 at 8:05 PM

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

    Reply
    • Mukesh Otwani says

      February 18, 2017 at 7:09 PM

      Hi Ashrita,

      Sorry, I don’t have.

      Reply
  4. megha says

    October 25, 2016 at 10:20 PM

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

    Reply
    • Mukesh Otwani says

      October 26, 2016 at 11:53 AM

      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/

      Reply
  5. Gaurav Khurana says

    October 1, 2016 at 3:03 PM

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

    Reply
    • Mukesh Otwani says

      October 3, 2016 at 4:30 PM

      Thanks Gaurav.

      Reply
  6. ranjit says

    June 9, 2016 at 1:00 PM

    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

    Reply
    • Mukesh Otwani says

      June 14, 2016 at 9:21 PM

      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.

      Reply
  7. Piyush Sharma says

    February 3, 2016 at 7:50 PM

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

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:17 PM

      Hi Piyush,

      Here you go.

      Solution
      http://learn-automation.com/solve-elementnotvisibleexception-in-selenium-webdriver/

      Reply
  8. Piyush Sharma says

    February 3, 2016 at 7:50 PM

    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 ?

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:18 PM

      Hi Piyush,

      Please share the application as well.

      Reply
      • Nitin says

        March 13, 2016 at 6:14 PM

        LOL!! Interesting Post!! 😉 😀

        Reply
    • Nanasaheb says

      December 9, 2020 at 5:25 PM

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

      Reply
      • Mukesh Otwani says

        December 9, 2020 at 9:18 PM

        Its always recommend using WebDriver waits instead of static wait

        Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?