SAP

Web

Must Learn!

Big Data

Testing

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:

  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.

Selenium WebDriver: Access Form, CheckBox, RadioButton & TextBox

Entering Values in Input Boxes

The sendKeys() method is used to enter values into input boxes.

Selenium WebDriver: Access Form, CheckBox, RadioButton & TextBox

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.

Selenium WebDriver: Access Form, CheckBox, RadioButton & TextBox

Radio Button

Toggling a radio button on is done using the click() method.

Selenium WebDriver: Access Form, CheckBox, RadioButton & TextBox

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.

Selenium WebDriver: Access Form, CheckBox, RadioButton & TextBox

Selenium WebDriver: Access Form, CheckBox, RadioButton & TextBox

 

Links

Links also are accessed by using the click() method.

Consider the below link found in Mercury Tours' homepage.

Selenium WebDriver: Access Form, CheckBox, RadioButton & TextBox

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.

Selenium WebDriver: Access Form, CheckBox, RadioButton & TextBox

Selenium WebDriver: Access Form, CheckBox, RadioButton & TextBox

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.

Selenium WebDriver: Access Form, CheckBox, RadioButton & TextBox

When submit() is used, WebDriver will look up the DOM to know which form the element belongs to, and then trigger its submit function.

Here is the complete code

package newPackage;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test {

	public static void main(String[] args) {

	// declaration and instantiation of objects/variables
	WebDriver driver ;
	System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
	driver = new FirefoxDriver();

	//Create explicit wait.
	WebDriverWait myWait = new WebDriverWait(driver, 10);

	String baseUrl = "http://newtours.demoaut.com/";
	driver.get(baseUrl);

	//wait until text box load.
	myWait.until(ExpectedConditions.visibilityOfElementLocated(By.name("userName")));

	// Enter value into textbox
	driver.findElement(By.name("userName")).sendKeys ("tutorial");

	// Delete value from textbox
	driver.findElement(By.name("userName")).clear();

	// Access Links
	driver.findElement(By.linkText("Register here")).click();
	driver.findElement(By.partialLinkText("SIGN-ON")).click();

	// Enter value into textbox and SUBMIT
	driver.findElement(By.name("userName")).sendKeys ("tutorial");
	driver.findElement(By.name("password")).sendKeys ("tutorial");
	driver.findElement(By.name("password")).submit();

	// Select Radio Button
	driver.findElement(By.cssSelector("input[value=Business]")).click();

	//Select CheckBox
	String baseURL = "http://demo.guru99.com/sele...";
	driver.get(baseURL);

	//Create an Explicit wait.
	WebDriverWait myWait2 = new WebDriverWait(driver, 10);
	myWait2.until(ExpectedConditions.visibilityOfElementLocated(By.id("persist_box")));

	WebElement chkFBPersist = driver.findElement(By.id("persist_box"));
	for (int i=0; i<2; i++) {
		chkFBPersist.click (); 
		System.out.println(chkFBPersist.isSelected());
	}

	driver.quit();
	System.exit(0);
}
}

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.
Submit Button submit()  
  • WebDriver allows selection of more than one option in a multiple SELECT element.
  • 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.
 

 

YOU MIGHT LIKE: