Thursday, 28 April 2016

Accessing Forms using Selenium WebDriver



In this tutorial , we will learn how to access forms and its  elements using Webdriver


Links

Links also are accessed by using the click() method.
Consider the below link found in Mercury Tours' homepage.
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.

Drop-Down Box

Before we can control drop-down boxes, we must do following two things :
  1. Import the package org.openqa.selenium.support.ui.Select
  2. 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.  
Step 1
Import the "Select" package.
Step 2
Declare the drop-down element as an instance of the Select class. In the example below, we named this instance as "drpCountry".
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".

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.
The code below will select the first two options using the selectByVisibleText() method.

Select Methods

The following are the most common methods used on drop-down elements.
Method
Description
selectByVisibleText() and
deselectByVisibleText()
Example:
  • Selects/deselects the option that displays the text matching the parameter.
  • Parameter: The exactly displayed text of a particular option
selectByValue() and
deselectByValue()
Example:
  • Selects/deselects the option whose "value" attribute matches the specified parameter.
  • Parameter: value of the "value" attribute
  • Remember that not all drop-down options have the same text and "value", like in the example below.

selectByIndex() and
deselectByIndex()
Example:

  • Selects/deselects the option at the given index.
  • Parameter: the index of the option to be selected.
isMultiple()
Example:

  • Returns TRUE if the drop-down element allows multiple selections at a time; FALSE if otherwise.
  • Needs parameters needed
deselectAll()
Example:

  • Clears all selected entries. This is only valid when the drop-down element supports multiple selections.
  • No parameters needed

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.
When submit() is used, WebDriver will look up the DOM to know which form the element belongs to, and then trigger its submit function.

3 comments:

  1. My friend Suggest me this blog and I can say this is the best blog to get the basic knowledge.Thank you so much for this Selenium Training in Chennai blog

    ReplyDelete
  2. Best blog.Got to learn new things.Thanks for this Blog SAP HR Training in Chennai

    ReplyDelete
  3. Great place to learn about the new things and got great knowledgeSAP MM Training in Chennai

    ReplyDelete