34. Using getText( ) to retrieve any elements Text






getText( ) is a Selenium WebDrivers predefined method which is used for retrieving the specified elements Text.

Test Description:

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




Lets Implement This:

Pre-requisites:

1. Create a new Java Project say 'WebDriver-Project8' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package8' under the newly created project.
4. Create a Java Class file say 'Class8' 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 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 retrieving the Heading Elements text, storing  the retrieved text in a string variable and pasting the retrieved text into the text box field as explained in the Test Description above by following the below steps:

4. Using FireBug options, Inspect the Heading Text 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 Heading text element using Class locator i.e. using class=mainheading  in this case.

6. Also we've to use the above Class locator in the Selenium WebDriver Command which is used to retrieve the text in the specified Heading text element. _driver.findElement(By.className("Class LOCATOR")).getText( );  is the syntax we've to use for retrieve the text from the specified locator.

Lets understand the _driver.findElement(By.className("Class Locator")).getText( );   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
  • By.className("Class LOCATOR") By.className informs the findElement( ) to find the elements using Class LOCATOR
  • Locator - is the actual locator we want to find (i.e. Heading Text  in this case )
  • getText( ) - is the WebDriver command which can also be used for retrieve the text from the specified locator

The result of this statement is a string text. In order to store the retrieved text, we have to create a String object and assign the result of the above statement to the retrieved string text.

String heading_text = _driver.findElement(By.className("Class Locator").getText( );

7. So after understanding the things in step5 and step6, its very clear that we've to write String heading_text_driver.findElement(By.className("mainheading")).getText( );  to retrieve the  text from the specified heading text element locator as shown below: 


8. Now lets follow the below steps to type the retrieved text into the Text Box field.

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



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

11. Also we've to use the above ID locator in the Selenium WebDriver Command which is used to enter the specified text into the Text Box field. _driver.findElement(By.id("ID LOCATOR")).sendKeys(String Object);  is the syntax we've to use for entering the String value stored in the String Object into the Text Box field.

Lets understand the _driver.findElement(By.id("ID LOCATOR")).sendKeys(String Object);   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
  • By.id("ID LOCATOR") By.id 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 )
  • sendKeys(String Object) - is the WebDriver command which can also be used for entering the String text stored in the specified String Object into any specified text field.

12. So after understanding the things in step10 and step11, its very clear that we've to write _driver.findElement(By.id("q")).sendKeys(heading_text);  to entering the String text value stored in the Specified String object heading_text into the Text Box field  as shown below and save:



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



14. After the test has run completely, observe that the heading text got displayed in the text box field as shown below:



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 getPageSource( ) predefined method to retrieve the source code of the page will be explained in the next post.


Followers

Labels