×

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

First test case of Selenium-IDE

First Test Case of Selenium-IDE: In this part, we are discussing how to create a basic test case in Selenium-IDE: The entire test script execution process in Selenium -IDE can be divided...

4 minutes read.

Selenium WebDriver Tutorial

Seleniun WebDriver Introduction Selenium WebDriver is the essential tool of Selenium Tool Suite because it can directly communicate the browser without any server.  Selenium WebDriver is a browser automation framework that accepts...

3 minutes read.

Xpath locator in Selenium IDE

X PATH LOCATOR: Xpath is used to identify the element using any attribute or visible test. Syntax for X PATH:- //html tag [@attribute=’value’] e.g. - //div [@class=’label’]                    OR //html tag [text () =’Visible Text’] e.g.-//div [text...

2 minutes read.

Selenium WebDriver - IE [Internet Explorer] browser

Selenium WebDriver - IE [Internet Explorer] browser In this segment, we will understand how to run a test script with the help of the IE (Internet Explorer) browser. It is a standalone...

4 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 WebDriver - Firefox or Gecko (Marionette) browser

Selenium web driver-Firefox or Gecko (Marionette) browser In this tutorial, we are going to learn how to run the Selenium WebDriver test script in the Firefox Browser using the Gecko Driver. Before going...

4 minutes read.

CSS Selector: Attribute in Selenium IDE

CSS Selector: Attribute Locating the attribute of a CSS selector, we would access the Password textbox, which is present in the login form of Facebook. The Password textbox has a type attribute...

1 minute read.

Mouse and Keyboard Controls in Selenium WebDriver

Mouse and Keyboard Controls in Selenium WebDriver In this tutorial, we will learn about how to handle mouse and keyboard event using action class in selenium WebDriver. Action Class in Selenium WebDriver: Action...

13 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.

Selenium WebDriver - Navigation Controls Commands

Navigation Controls Commands in Selenium WebDriver WebDriver is used to hold the browser history and perform browser navigation operations like, back, forward, and refresh. Whenever a WebDriver launches a browser, all...

5 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.

Drawback or Limitations of Selenium

Drawback or Limitations of Selenium Followings are the Drawback or limitations of Selenium:- Drawback /LimitationsDescriptionDesktop applications cannot be automated by Selenium.To test the desktop application, we have tools like Test Complete, Winnium, and...

2 minutes read.

XPath Axes Selenium WebDriver

XPath Axes XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context. These methods are mainly used when the...

14 minutes read.

Id Locator in Selenium IDE

Id Locator Id locator is used to find the web element by the help of id of that element because ID is likely to be unique for each web element. Target Format: Id=id of the...

2 minutes read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

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.

Selenium WebDriver – Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.

Verification Controls Methods in Selenium WebDriver

Verification Controls Methods  The verification controls methods are used to check whether the web element is displayed, enabled, and selected. Some of the most commonly used verification controls are as follow: Verification controls Description Return...

4 minutes read.

Selenium WebDriver- Checkbox handling

Selenium WebDriver- Checkbox handling In this tutorial, we will be learning about handling the checkbox in selenium WebDriver. Let us take one example to give you a better understanding of Checkbox handling,...

4 minutes read.

Selenium WebDriver-CSS Selector: Tag Class and Attribute

Selenium WebDriver-CSS Selector: Tag, Class, and Attribute We will locate the web element with the help of Tag, class, and attribute CSS selector locator in this section of the tutorial. Where Tag,...

5 minutes read.