In this tutorial , we will learn how to access forms and its elements using Webdriver
Accessing Form Elements
Input Box
Input boxes refer to either of these two types:
- Text Fields- text boxes that accept typed values and show them as they are.
- Password Fields- text boxes that accept typed values but mask them as a series of special characters (commonly dots and asterisks) to avoid sensitive values to be displayed.
.png)
Entering Values in Input Boxes
The sendKeys() method is used to enter values into input boxes.
.png)
Deleting Values in Input Boxes
The clear() method is used to delete the text in an input box. This method does not need any parameter. The code snippet below will clear out the text "tutorial" in the User Name text box.
.png)
Radio Button
Toggling a radio button on is done using the click() method.
.png)
Check Box
Toggling a check box on/off is also done using the click() method.
The code below will click on Facebook's "Keep me logged in" check box twice and then output the result as TRUE when it is toggled on, and FALSE if it is toggled off.
.png)
.png)
Links
Links also are accessed by using the click() method.
Consider the below link found in Mercury Tours' homepage.
.png)
You can access this link using linkText() or partialLinkText() together with click(). Either of the two lines below will be able to access the "Register here" link shown above.
.png)
.png)
Drop-Down Box
Before we can control drop-down boxes, we must do following two things :
- Import the package org.openqa.selenium.support.ui.Select
- Instantiate the drop-down box as a "Select" object in WebDriver
As an example, go to Mercury Tours' Registration page (http://newtours.demoaut.com/mercuryregister.php) and notice the "Country" drop-down box there.
.png)
Step 1
Import the "Select" package.
.png)
Step 2
Declare the drop-down element as an instance of the Select class. In the example below, we named this instance as "drpCountry".
.png)
Step 3
We can now start controlling "drpCountry" by using any of the available Select methods. The sample code below will select the option "ANTARCTICA".
.png)
Selecting Items in a Multiple SELECT element
We can also use the selectByVisibleText() method in selecting multiple options in a multi SELECT element. As an example, we will take http://jsbin.com/osebed/2 as the base URL. It contains a drop-down box that allows multiple selections at a time.
.png)
The code below will select the first two options using the selectByVisibleText() method.
.png)
Select Methods
The following are the most common methods used on drop-down elements.
Method
|
Description
|
selectByVisibleText() and
deselectByVisibleText()
Example:
![]() |
|
selectByValue() and
deselectByValue()
Example:
![]() |
![]() |
selectByIndex() and
deselectByIndex()
Example:
![]() |
|
isMultiple()
Example:
![]() |
|
deselectAll()
Example:
![]() |
|
Submitting a Form
The submit() method is used to submit a form. This is an alternative to clicking the form's submit button.
You can use submit() on any element within the form, not just on the submit button itself.
.png)
When submit() is used, WebDriver will look up the DOM to know which form the element belongs to, and then trigger its submit function.
Summary
- The table below summarizes the commands to access each type of element discussed above.
Element
|
Command
|
Description
|
Input Box
|
sendKeys()
|
used to enter values onto text boxes
|
clear()
|
used to clear text boxes of its current value
| |
Check Box,
Radio Button,
|
click()
|
used to toggle the element on/off
|
Links
|
click()
|
used to click on the link and wait for page load to complete before proceeding to the next command.
|
Drop-Down Box
|
selectByVisibleText()/
deselectByVisibleText()
|
selects/deselects an option by its displayed text
|
selectByValue()/
deselectByValue()
|
selects/deselects an option by the value of its "value" attribute
| |
selectByIndex()/
deselectByIndex()
|
selects/deselects an option by its index
| |
isMultiple()
|
returns TRUE if the drop-down element allows multiple selection at a time; FALSE if otherwise
| |
deselectAll()
|
deselects all previously selected options
| |
Submit Button
|
submit()
|
- WebDriver allows selection of more than one option in a multiple SELECT element.
- To control drop-down boxes, you must first import the org.openqa.selenium.support.ui.Select package and then create a Select instance.
- You can use the submit() method on any element within the form. WebDriver will automatically trigger the submit function of the form where that element belongs to.
.png)
.png)
.png)
.png)
.png)
.png)
No comments:
Post a Comment