30. Using sendKeys( ) predefined method for entering text






sendKeys( ) is a predefined method of Selenium 'WebDriver' Class which is used to type the provided text in to text box and text area fields. 

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-Project4' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package4' under the newly created project.
4. Create a Java Class file say 'Class4' 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 entering the text 'www.Selenium143.blogspot.com' into the text box field as explained in our above Test Description by following the below steps:

4. Using FireBug options, Inspect the above text box without label and and identify its locator after observing the below code in the below screenshot:


5. After observing the above inspected html code, its very clear that we've to identify this text box with ID Locator i.e. id=q in this example

6. Also we've to use the above ID locator in the Selenium WebDriver Command which is used to enter text into this text box. _driver.findElement(By.id("ID LOCATOR of Text Box Field")).sendKeys("Text To entered");  is the syntax we've to use for entering the text into the specified locator.

Lets understand the _driver.findElement(By.id("ID LOCATOR")).sendKeys();  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( ) - is the WebDriver command for entering the specified text into the text element for which the locator is specified.


7. So after understanding the things in step5 and step6, its very clear that we've to write _driver.findElement(By.id("q")).sendKeys("www.Selenium143.blogspot.com");  to enter the text "www.Selenium143.blogspot.com" into the identified text box as shown below:



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



9. After the test has run completely, observe that the text 'www.Selenium143.blogspot.com' got entered into 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 click( ) predefined method for selecting a radio button will be explained in the next post.







Followers

Labels