313. Using isDisplayed( ) to verify whether an element is available

isDisplayed( ) is a Selenium WebDrivers predefined method which is used for verifying whether the specified element is available on the page or not.

Test Description:

Lets implement the following Test as explained using the comments on the below screenshot:

Lets Implement This:


1. Create a new Java Project say 'WebDriver-Project16' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package16' under the newly created project.
4. Create a Java Class file say 'Class16' 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. The above code will create a Selenium WebDriver object, Launch a new Firefox Browser session, opens the page in the Browser window and finally it maximizes the Browser window as shown below:

3. Now lets write Selenium WebDriver code for verifying whether the text box field is available on the Home page as explained in the Test Description above by following the below steps:

4. Using FireBug options, Inspect the Text Box element and identify the locator after observing the below code in the below screenshot:

5. After seeing the above html code in the screenshot, its very clear that we've to identify the Text Box field element using id locator i.e. using id=q  in this case.

6. Also we've to use the above id locator in the Selenium WebDriver Command which is used verify whether the text box field is present on the page. _driver.findElement("ID LOCATOR")).isDisplayed( );  is the syntax we've to use for verifying whether the element is present on the page.

Lets understand the _driver.findElement("ID Locator")).isDisplayed( );   syntax by breaking it as below:

  • _driver - is the WebDriver object 
  • findElement( )  - is used for locating the elements on which we have to perform the operations
  •"ID LOCATOR") informs the findElement( ) to find the elements using ID LOCATOR
  • Locator - is the actual locator we want to find (i.e. Text Box field in this case )
  • isDisplayed( ) - is the WebDriver command which returns true if the specified element is present on the page, else it will return false.

The result of this statement is a boolean value i.e. True or false. We have to pass this Boolean value returned by this statement to the JUnit's method assertTrue( ), which passes the test when the boolean value is true and fails the when the boolean value returned by the statement is false.

assertTrue( _driver.findElement("ID LOCATOR")).isDisplayed( ) );

7. So after understanding the things in step5 and step6, its very clear that we've to write assertTrue( _driver.findElement("q")).isDisplayed( ) ); for verifying whether the Text Box field is present on the page as shown below: 

8. Run the Test using JUnit Test as shown below:

9. Observe that the Test Got Passed as shown below:

As the test got passed, hence the Text Box field is available on the page.

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.

Using FirefoxDriver for running Selenium WebDriver Tests on Firefox Browser will be explained in the next post.

No comments:

Followers :)

Blog Index