Automating textboxes, text areas, and buttons

The textbox and button elements are the most common elements used in any web application. Selenium WebDriver's WebElement interface provides methods to simulate keyboard entry into textboxes or text areas and perform clicks on a button control.

In this recipe, we will explore these methods to automate textbox, text-area, and button elements.

How to do it...

Here we will explore the clear(), sendKeys(), submit() and click() methods of the WebElement interface.

Clearing text from textbox and text-area elements

To clear the existing text value from textbox and text-area elements, we can use the clear() method, as shown in following code example:

// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// Clear the existing text value using clear method
element.clear();

Entering text in textbox and text-area elements

To enter text value in a textbox or a text-area element, we can use the sendKeys() method, as shown in following code example:

// Enter something to search for
element.sendKeys("Selenium testing tools cookbook");

Submitting forms

Normally, HTML data entry forms are created using the <form> element. When user fills the data and saves form, it is submitted on the remote web server to process the data. Here's an example where a Google search form is submitted by calling the submit() method on the search textbox shown on the Google homepage:

// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// Clear the existing text value
element.clear();

// Enter something to search for
element.sendKeys("Selenium testing tools cookbook");

// Now submit the form. WebDriver will find
// the form for us from the element
element.submit();

Performing a click on a button element

To click on a button element, we can use the click() method of the WebElement interface:

// Find the button element by its name
WebElement element = driver.findElement(By.name("btnG"));

// Click on the button
element.click();

How it works...

The clear() method of the WebElement interface works only on a textbox <input> and text area, having no effect on other types of elements. It will clear any previous value from an element.

The sendKeys() method can be used on any element that accepts values by typing on the element. This method simulates typing into an element that sets the given string as the value of that element. The sendKeys() method accepts java.lang.CharSequence or string value.

We can also simulate pressing non-text keys using the sendKeys() method by using the Keys enum. For example, after entering a value in the textbox, we can press the TAB key as shown in following code example:

element.sendKeys("123" + Keys.TAB);

For a complete list of keys visit http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/Keys.html.

The submit() method is applicable on the <form> element or any element that is under the <form> element. Developers can create buttons or links for form submission. When a user clicks on the submission element, the onsubmit event is fired. In Selenium WebDriver, any element that is part of the form, or an element within a <form>, can call this event using the submit() method of the WebElement interface.

The click() method will click on an element. If clicking on an element causes the current page displayed in a browser to change, then the script will wait until the new page is loaded. However, if it causes a new page to be loaded via an event, or by sending a native event, then the method will not wait for it to be loaded. In such cases, we will have to verify that a new page has been loaded.

There are some preconditions for an element to be clicked using the click() method. The element must be visible and it must have a height and width greater then 0. Clicking on an invisible element will result in a ElementNotVisibleException exception.

See also

  • The Checking an element's text recipe
  • The Checking an element's attribute and CSS values recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.134.17