×

Operational Controls Methods in Selenium WebDriver

Operational Controls Methods

The Operational controls are used to perform all the actions on the web elements like click on the button, pass the value in the text box, and click on the submit button, etc.

Operational Controls Methods

Some of the most commonly used operational controls are as follow:

Operational Controls Description Syntax Command
sendKeys() It is used to pass data into textbox or text area. sendKeys(String) : void   element.sendKeys("text");  
click() It is used to perform left click operation on link, checkbox, radio button, image element. click() : void element.click();    
clear() It is used to clear or reset existing data from the edit box. clear() :void element.clear();    
submit() It is used to perform click operation only on the submit button. Submit() :void element.submit();

Let us take a sample test script in which will try to cover mostly used operational_controls Commands:

For our testing purpose, we are using one e-commerce website Myntra, to perform operational commands of the web element controls.

In this test, we will automate the following test scenarios:

Steps Action Method Used Input Excepted Result
1. Invoke the Google Chrome Browser. System.setProperty()   The Chrome browser must be opened.
2. Navigate to the Myntra home page. get() www.myntra.com Myntra home page must be displayed.
3. Pass the date using send keys methods in the search box.   sendKeys() Puma shoe page The passed Data should be opened with a particular page.
4. Click on the search key button using the click method.   click()   The Search key button should be clicked.
5. Clear the date on the search box using the clear method.   clear()   The Data should be cleared in the text box.
6. Close the browser close()   The browser should be closed.

Open the Eclipse IDE and the existing test suite new_test, which we have created in WebDriver Installation section of WebDriver tutorial.

Then, right-click on the src folder and create a new Class File from New > Class.

right-click on the src folder
  • Give the Class name as operational_controls and click onthe Finish button.
the Class name as operational_controls

We are creating our test case step by step to give you a complete understanding of Operational_controls Commands in WebDriver.

Step1:

To launch the Google Chrome browser, we need to download the ChromeDriver.exe file and set the system property to the path of ChromeDriver.exe file.

Here is the code for setting the system property for the google chrome:

//set the system property of Google Chrome
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");  

To initialize the Chrome driver using ChromeDriver class:

// create driver object for CHROME browser
WebDriver driver=new ChromeDriver(); 

Step2:

Launch the website using get() method.

//navigate to myntra home page
driver.get("https://www.myntra.com"); 

Step3:

Pass the date using sendkeys() methods in the search box.

// using a send keys method to pass the data into the search box
driver.findElement(By.className("desktop-searchBar")).sendKeys(" puma shoes");
//or can also be written like this:
WebElement wb=driver.findElement(By.className("desktop-searchBar"));
wb.sendKeys("puma shoes");

Step4:

Click on the search key button using the click() method.

//using click method to click on the particular button driver.findElement(By.className("desktop-submit")).click();
 //or written like this
WebElement wb1=driver.findElement(By.className("desktop-submit"));
wb1.click();
// print to check whether the method is working fine or not
System.out.println("passed"); 

Step5:

Clear the date on the search box using the clear() method.

//using the clear method to clear the search box data.
driver.findElement(By.className("desktop-searchBar")).clear();
System.out.println("passed1"); 

Step6:                                                                                 

Finally, we terminate the process and close the browser.

//Close the browser  
driver.close();   

After that, our final test script will look like this:

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class webelement_controls {
public static void main(String[] args) { 
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
 // create driver object for CHROME browser
 WebDriver driver=new ChromeDriver();
 //open the URL 
driver.get("https://www.myntra.com");
// using send keys method to pass the date into the textbox
driver.findElement(By.className("desktop-searchBar")).sendKeys(" puma shoes"); 
//using click method to act on left-click operation
driver.findElement(By.className("desktop-submit")).click();
// print to check whether the method is working fine or not
System.out.println("passed");   
//used clear method to clear the data from the textbox
driver.findElement(By.className("desktop-searchBar")).clear();
System.out.println("passed1");
//close the existing browser
driver.close();
}         
} 
  • To run the test script in Eclipse, right-click on the window and then click Run as > Java application.
right-click on the window and then click Run
  • The test script will be launched in the chrome browser and automate all the test scenarios.

 

 


Related Topics

WebDriver-Name Locator:

WebDriver-Name Locator: The name locator is used to identify any element in UI with the help of the name backend attribute. The Syntax of the name attribute is as below: driver.findElement(By.name(“value of the...

4 minutes read.

Selenium WebDriver-CSS Selector: Tag and Class

Selenium WebDriver-CSS Selector: Tag and Class We will locate the web element with the help of tag and class CSS selector in this section of the tutorials Where Tag and class are...

4 minutes read.

Selenium WebDriver-XPath Locator

Selenium WebDriver-XPath Locator The locators can make the object identification of all the selenium tools. There are eight locators available in selenium web driver, but most of the time we go for...

14 minutes read.

Advantages and Disadvantages of Selenium-IDE

Advantages and Disadvantages of Selenium-IDE Advantages of Selenium IDE: UI based record and playback tool. Easy to understand & developed a test script. Selenium-IDE test script export into web driver code & Selenium–RC It is...

1 minute read.

Selenium-IDE Locators

Selenium-IDE Locators: Before we start learning about Selenium –IDE Locators, firstly we have to learn about UI elements. UI Elements -UI elements are the elements which are displayed on the web application....

1 minute read.

Selenium WebDriver - Browser Controls Commands

Selenium WebDriver - Browser Controls Commands WebDriver includes operations like opening a browser, fetch the title, and closing the browser, etc. Some of the most commonly used Browser controls commands for Selenium WebDriver...

6 minutes read.

Link Text Locator in Selenium IDE

LINK TEXT Link textlocator is used to identify only the weblink element based on the name of the link. Whenever multiple links are there, it will not work; it will work only...

1 minute read.

WebDriver Interface | Class Diagram

WebDriver interface/class diagram: The interface of WebDriver includes the following stages, which is as follows: Search Context Web driver Remote web driver Driver class Search Context  The first interface of the web driver class is search Context,...

2 minutes read.

CSS Selector: Sub String in Selenium IDE

CSS Selector: Sub String CSS Selector Sub-string is used to match a partial string to locate a particular web element. There are three ways to match a substring using a CSS Selector. Match...

2 minutes read.

Selenium-IDE Installation in Chrome

Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Google Chrome browser in your system. However, you can...

1 minute read.

WebDriver-Id Locator

WebDriver-Id Locator: The Id Locator is used to identify any web element in UI with the help of the ID attribute. The First preference is always goes to ID locator while inspecting...

4 minutes read.

Selenium IDE Commands

Selenium –IDE Commands (SELENESE): Selenese commands are the set of commands used in Selenium IDE. Selenese command has two parameters target and value. Selenese command tests the existence of UI elements based on their HTML tags,...

3 minutes read.

Selenium WebDriver-Alert Popup

Selenium WebDriver-Alert Popup  An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser. The Alert popup will be...

6 minutes read.

Selenium WebDriver-CSS Selector: Substring Matching

CSS Selector: Substring Matching Selenium WebDriver In this tutorial, we will learn how to identify the web element using the substring method. A Sub-string contains three methods for identifying the web element...

9 minutes read.

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods: The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc. Some of the most commonly used data-capture controls are...

7 minutes read.

Name Locator in Selenium IDE

NAME LOCATOR: It is used to find the name in the web element. If we have multiple web elements of the same name, then the first matched element would be returned. Open the...

3 minutes read.

Selenium WebDriver-CSS Selector: Tag and ID

Selenium WebDriver-CSS Selector: Tag and ID We will locate the web element with the help of tag and id CSS selector in this section of the tutorials Where Tag and id are...

4 minutes read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes read.

Selenium-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

2 minutes read.

Selenium IDE Tutorial for Beginners

Selenium IDE Introduction Selenium IDE stands for Integrated Development Environment. Selenium-IDE is released in 2006 and developed by SHINYA [JAPAN] THOUGHTWORK Company. Selenium IDE implemented as a Firefox plug-in, and concedes the record, edit, and debug tests....

2 minutes read.