Home » Software Testing » Automation Testing » Selenium WebDriver Tutorial » How to use assertEquals() in WebDriver using driver.getText()

How to use assertEquals() in WebDriver using driver.getText()

When we start making some  web application, We start with some UI and Every UI has menu, Check box, Button, Combo Box , Radio button, Text Field and other element of Web Application.

But most of the time we need to verify that Text or value associated with above mentioned elements of Web Application are correct or not and for this we commonly use two function
1- getText()
2- assertEquals().

getText() helps in retrieving the Text from an element by using WebElement class.This method returns the value of  inner text attribute of Element.

So why not take a look on this how it works

Recommended Paid Courses to Lean Selenium and Locators:

In this script
1- I have used xpath to find the Google Search button
2- By using findElement method we could find Google Search Button
3- Once we get the element, By using getText() method we could find the text on Button
4- By using assertEquals(), we verify that Google Search Text is available on Google Search Button. If text retrieved from getText() match with the String inserted in to asserEquals() method then test is passed otherwise it is  failed.

Also Read : Mobile emulation in Google Chrome using Selenium WebDriver

Result after execution of this code

There are other assert methods that could be used to verify the text in place of assertEquals()


If this text pattern match then test get passed other wise test get failed.

52 thoughts on “How to use assertEquals() in WebDriver using driver.getText()”

  1. Pingback: Selenium Best Links | Learn Testing

  2. Hi Dwarika,

    I tried using assertequals in my webdriver code but I am not able to understand where can we give the statement for logs.
    It compares the values but does not return any boolean value to confirm. Please explain.


    1. Dear Dwarika Dhish Mishra,

      I write the code using TestNG in Eclipse . TestNG syntax were showing underline or strike but it’s executing in Eclipse.I am not understand why it was happen? please help me on this issue.

      Please see the below code.

      package TestNGtests;

      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.testng.annotations.Test;

      import junit.framework.Assert;

      public class TestNGTestCase_001 {
      public void VerifyTitle() {
      WebDriver driver = new FirefoxDriver();
      String PageTitle = driver.getTitle();
      Assert.assertEquals(PageTitle, “Gmail”);


  3. Hi,
    I am unable to use Assert.assertEquals() function it is always showing ‘Assert’ sting as strike even I imported ‘import junit.framework.Assert;’. Is it because of jar file? if yes then can you please suggest me which jar file is required for this?

    1. have you added Standalone selenium jar? if yes the you can directly write junit tests.

      So please check it carefully what is the problem. or send me the stack trace so that i could help you in resolving your problem

      Thank you!!

      1. Thanks for quick reply there is no problem of jar but how to write fun that give me ‘PASS’ / ‘FAIL’ status that is problem.
        Can I show you sample code?


        1. If you want Pass/Fail status, you can use If-Else loop instead of AssertEquals
          For eg:
          if(“expected String”.equals(“actual string”))
          System.out.println(“String matches “);
          System.out.println(“String does not match “);

  4. Hi,
    I have one class very I want to use this where I am using Assert.assertEquals(expected.trim(), actaul.trim()); by using this method want to know whether my test case is fail or pass? In this method I am giving expected & actual string if both are same then it ‘PASS’ & if both are mismatch then my test case is fail that I want to do. Can you please help me how I can do this?

    1. Why not you are using if else to print pass/fail condition. In that case you need to use some more functions like text.equals(text)

      for both text you need to extract text or result in some string, or in variable.

  5. Pingback: WebDriver’s most popular Commands | Abode QA

  6. Pingback: Handling Multiple Windows in WebDriver | Abode QA

  7. Pingback: Handling JavaScript Alert in WebDriver | Abode QA

  8. Pingback: assertTrue(message,condition) in Selenium WebDriver and its implementation | Abode QA

  9. Hi Deepali,

    For boolean value confirm you need to give condition. But i din’t use the assertequals()
    Eg: String foo = Driver.findElement(By.xpath(“/html/body/form/table/tbody/tr/td/table/tbody/tr[2]/td/div/h4”)).getText();
    System.out.println(“foo text :: “+foo);
    if (foo.equals(“Classify Customer”)) {
    } else {

  10. Hi, Im new to selenium.

    What If I wanted to check every element on the page?

    Would I be right n saying I need to put all the elements in a list, do getText for all and search through one by one?



  11. Hi

    org.testng.Assert is deprecated right ? So should we use any alternative to verify the tests? Pointers would be of great help.Thank you.


  12. In my project I need to launch the application, then I need to verify the content(Text) present on the website.my requirement is ( need to keep the original text in one excel and get the text from the site and compare the expected and actual text).please let me the know process how to automate this requirement.

    1. In this case first read the data from site second thing is to read data from
      Excel sheet using any api either poi or jxl and then call assertequal assertion and put both values if both string would be same it will pass otherwise assert will fail.

          1. i sent you my mailID can u pls send the sample piece of code.
            it will be great helpful to me

    2. hi
      Is there any way to download the files from applications and save it local drive with selenium webdriver.
      if there is a way pls help me in giving a solution.

      Advance thanks

  13. Hi Mistra,

    I need the code in selenium WebDriver Junit. I have a test case working on, which is related to this type of below question.

    Here is my question?

    Note: I m using other link to come into this page and test.

    In this blog you have “Recent Posts” on right side. I want to test whether that field is present in this page coming from other page which consists of recent posts in it or not. Can you please help me out of this.

    I hope you understood my question.


  14. Sir, i am using “assert.equals(expectedvalue, actualvalue);” statement to check some condition. My problem is that i want to store “Pass” in excel file if “assert.equals(expectedvalue, actualvalue);” is success and same way i want to store “Fail” in excel file, if “assert.equals(expectedvalue, actualvalue);” is failed. Could you please help me out. Thanks.!!

    1. Use try and catch block and in case of assertion get true value in try block it will write pass in excel
      Field and if it
      Gets false then it will jump to
      Catch block
      And here in catch block keep your fail status statement for excel

  15. Hi Dwarika , i am a beginner to selenium , i have worked in java development , i tried to run this code as it is , included required jars, i dont where to locate the result and how junit works when i run this class , i am getting an error as
    Error: Main method not found in class com.testng.src.com.testng.Assert_Exp, please define the main method as:
    public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application

    should i right it with main method ? or how does it run ?

  16. How to validate below fields of a webpage by using Selenium Webdriver TestNg
    HTTP Status Code
    Logo link title text
    Page body header (if exists).

  17. I am working with windows application.I have logged into the application by using “Sikuli”

    Query : I want to click on link with given string with “Webdriver” could you please help me on this

  18. Hi
    can you explain how to compare two text file data using assert Equals method in selenium?
    Ex:- abc.txt contains some dta
    xyz.txt contains same data
    how to compare that two files data

  19. Shubham Chauhan

    Hello I have a query in selenium web driver.

    Can any one let me know about how to verify the expected result from the excel sheet, so that i do not have to make changes in the script again & again, only I have to change the test data for the expected result which is dynamic in nature?

    1. Yes you can if you have code to read data from excel sheet.
      In this process, First figure out text from front end and then compare it with the value fetched from excel sheet and it would work as per my understanding.

  20. Assert.assertTrue(enteradmin_email1.getText().contains(p.getVal(“adminemail”)), “Assert Failed as its unable to search text in Logged in Page”);

    I am getting found false in output.

  21. Hi sir,
    I am testing android mobile app.
    My customer form is like
    Name,emails I’d ,phone no and address field.
    I want give validation on name field and check its pass or fail
    For name field- its take only 50 char.
    Its does not take null char
    Its does not take special char.

    Please give me right direction.
    Email [email protected]

  22. On alert pop up, I get message: Please enter username . On next line I get Please enter company name. When I assert it with multiple lines, the assertion fails. How to handle this. If I verify for just one field , I am able to achieve this.

Leave a Reply

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

Scroll to Top
%d bloggers like this: