Skip to main content

Accessing form elements in Selenium

Accessing Form Elements
Input Box
Input boxes refer to either of these two types:
  1. Text Fields– text boxes that accept typed values and show them as they are.
  2. 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.

Entering Values in Input Boxes
The sendKeys() method is used to enter values into input boxes.
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.
Radio Button
Toggling a radio button on is done using the click() method.
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.

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
deselectByValue()
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.
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.

Comments

Popular posts from this blog

Rational Functional Tester: Calling RFT scripts from using xml tags

Hello, This will help to call RFT scripts in xml, if anyone using 'ant' this will help them to embed code to invoke RFT scripts. <java classname="com.rational.test.ft.rational_ft" fork="true" maxmemory="1024m">         <classpath>         <fileset dir="C:\Program Files\IBM\SDP\FunctionalTester\bin\">         <include name="rational_ft.jar" />         </fileset>         </classpath>         <jvmarg line="-Drational_ft.install.dir=&quot;C:\Program Files\IBM\SDP\jdk_\jre\bin&quot;" />         <arg line="-rt.bring_up_logviewer false -datastore &quot;D:\RFT-Dev\Project&quot; -playback RFTTestSet5.testcase4.TestScript1" />         <arg line="-args -scriptArg ${scriptValue}" />         </java>

Rational Functional Tester: Creating and Reading Datapool cell value in RFT

Hello, Hope this will help for my friends who is trying to use data pool for dynamic values.If you want to use any run time values in between scripts then you will be looking for the functions to read/write/create datapool cells dynamically. /************ Dp row count *************************/ IDatapoolIterator  ite; ite.dpCurrent().getEquivalenceClass().getRecordCount(); /***************** to set cell value **************/ IDatapoolIterator it; ((DatapoolCell) it.dpCurrent().getCell("RunTimeSheetForContainers")).setCellValue("Y"); /*********** to load CSV files (csv to Datapool)*******************************/ public IDatapoolIterator LoadCSV(String Sname)       {             File fname=getFileName(Sname);             IDatapool dp=DatapoolUtilities. loadCSV (fname, "," , true );             IDatapoolIterator ii=dpFactory().open(dp, null );             ii.dpInitialize(dp);             return

Questions on Selenium Grid

What is Selenium Grid? Selenium Grid is a part of the Selenium Suite that specializes on running multiple tests across different browsers, operating systems, and machines in parallel . Selenium Grid has 2 versions – the older Grid 1 and the newer Grid 2. We will only focus on Grid 2 because Grid 1 is gradually being deprecated by the Selenium Team. Selenium Grid uses a hub-node concept where you only run the test on a single machine called a   hub , but the execution will be done by different machines called   nodes .  When to Use Selenium Grid? You should use Selenium Grid when you want to do either one or both of following : ·          Run your tests against different browsers, operating systems, and machines all at the same time. This will ensure that the application you are testing is fully compatible with a wide range of browser-OS combinations. ·          Save time in execution of your t