[フレーム]

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

by 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 🙂

Reader Interactions

Comments

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

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

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

  4. Piyush Sharma says

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

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

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.

AltStyle によって変換されたページ (->オリジナル) /