We've already discussed about this command in our earlier post Post # 34 Using getText( ) to retrieve any elements Text. Go through the Post # 34 once and now lets use the assertEquals( ) JUnit method to verify whether retrieved text is equal to the actual text.
Syntax:
assertEquals("Retrieved text is not the same", "omayo", _driver.findElement(By.cssSelector("CSS Path for locating the text element").getText( );
Note:- Here 'omayo' is the text to be retrieved from the text element.
Test Description:
First of all lets understand the Test Description by going through the comments mentioned on the below screenshot.
So we have to locate the heading element's text using CSS path, as use getText( ) to retrieve the text.
Lets Implement This:
Pre-requisites:
1. Create a new Java Project say 'WebDriver-Project76' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package76' under the newly created project.
4. Create a Java Class file say 'Class76' under the newly created package as shown below:
Actual Steps:
1. Write the following code into the newly created Java Class file as shown below and make sure that you resolve all the errors before going to next step:
2. Create a test method 'getTextDemo()' as shown below:
3. Use Selenium WebDriver method getText( ) for retrieving the text from the specified Heading text element and use assertEquals( ) JUnit method for verifying whether the text retrieved using the getText( ) method is equal to the actual text as shown below:
4. Save and Run the 'Class76.java' file by selecting the 'JUnit Test' option and ensure that our Automation Test has passed to confirm that the text of the Heading element is equal to the text that is retrieved using the Selenium WebDriver method getText( ) as shown in the below video:
Watch the below video:
Click here to watch the video.
Download this Project:
Click here to download this project and import into Eclipse IDE on your machine.
Please comment below to feedback or ask questions.
Get XPath Count in Selenium WebDriver will be explained in the next post.