isDisplayed( ) is a Selenium WebDrivers predefined method which is used for verifying whether the specified element is available on the page or not.
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:
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 http://book.theautomatedtester.com 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(By.id("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(By.id("ID Locator")).isDisplayed( ); syntax by breaking it as below:
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(By.id("ID LOCATOR")).isDisplayed( ) );
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: