Actions in Selenium

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Actions in Selenium

Selenium has been widely used in automating the web applications as it stands out from
other testing tools by providing numerous ways to test the application. No matter how
complex the User Interface is, Selenium provides multiple options to automate it thereby
reducing or completely wiping away the manual effort. When it comes to test windows-
based applications or any standalone applications we have different tools like QTP/UFT
which perform the user actions in the window handles. But how do we automate the web
applications where there is a need to interact with the browser through mouse or
keyboard? Its simple. We can again use Selenium for performing such interactions with the
browser through our automation script. So, the next question would be how do we interact
with the help of Selenium? The answer is Actions class. Yes, the Actions class provides
multiple methods which can be used to perform a single action or series of actions in the
browser.
I will take you through the Actions class and its implementations in this article. Before we
dive deep into this concept let us understand the basics.

What is Actions class in Selenium?


The interactions done through mouse and the keyboard can be automated by using the
Actions class in Selenium. The Actions class as the name suggests contains a collection of
actions that can be performed in the web application.
The actions that can be performed in a browser can be broadly classified into two categories
namely
1.Mouse actions
2.Keyboard actions

Mouse actions
The various mouse actions that are provided by the Actions class are

 click() – clicks at the current location


 doubleClick() – performs a Double click at the current mouse location
 contextClick() – performs a Right click at the current mouse location
 dragAndDrop(WebElement source, WebElement target) – drags an element from the
source location and drops in target location
 moveToElement(WebElement target) – moves to the target element

Keyboard actions
The various keyboard actions that are provided by the Actions class are
 keyUp(WebElement target, java.lang.CharSequence key) - Performs a key release
after focusing on the target element
 keyDown(WebElement target, java.lang.CharSequence key) - Performs a key press
after focusing on the target element
 sendKeys(WebElement target, java.lang.CharSequence... keys) – types the value

Apart from the above methods there are many other methods which can be used based on
our requirements.

How to implement the Actions class?


To implement the Actions class in our automation script, follow the below steps
Step 1: We have to import the package org.openqa.selenium.interactions.Actions

Step 2: To use the methods provided by the Actions class we need to create an object of this
class and pass the WebDriver as an argument.

// instantiate the WebDriver


WebDriver driver = new ChromeDriver();

// create an object of the Actions class


Actions act = new Actions(driver);

Step 3 : The object created can now be used to perform any actions. You could see various
actions that are provided by this class once you create an object.
Let us see the implementation of each method in detail with an example

1.sendKeys
We can use the sendKeys() method to type specific values in the application. Below is a
simple code which is used to search a product in a search engine by passing the product’s
name in the search box.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class sendKeysDemo {

public static void main(String[] args) {

//specify the driver location


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");

//instantiate the driver


WebDriver driver = new ChromeDriver();

//specify the URL of the webpage


driver.get("https://www.google.com/");

//maximise the window


driver.manage().window().maximize();

//specify the locator of the search box


WebElement element =
driver.findElement(By.xpath("//*[@id=\"tsf\"]/div[2]/div[1]/div[1]/div/div[2]/in
put"));

//create an object for the Actions class and pass the driver
arguement
Actions action = new Actions(driver);

//pass the product name that has to be searched in the website


action.sendKeys(element, "iphone").build().perform();

driver.quit();
}
}

Now you would wonder why do we add build()and perform() at the end?
The build() method generates a composite action containing all actions which are ready to
be performed. The perform() method is used to perform the series of actions which are
defined.

2.Mouse click
We can perform a mouse click either on a specific element or at the current mouse location.
Below is a simple code which is used to search a product in a shopping website and click the
search icon
import static org.testng.Assert.assertEquals;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class click {

public static void main(String[] args) {

//specify the driver location


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");

//instantiate the driver


WebDriver driver = new ChromeDriver();

//specify the URL of the webpage


driver.get("https://www.amazon.in/");

//maximise the window


driver.manage().window().maximize();

//create an object for the Actions class and pass the driver
arguement
Actions action = new Actions(driver);

//specify the locator of the search box in which the product has
to be typed
WebElement elementToType =
driver.findElement(By.id("twotabsearchtextbox"));

//pass the value of the product


action.sendKeys(elementToType, "iphone").build().perform();

//specify the locator of the search button


WebElement elementToClick = driver.findElement(By.className("nav-
input"));

//perform a mouse click on the search button


action.click(elementToClick).build().perform();

//verify the title of the website after searching the product


assertEquals(driver.getTitle(), "Amazon.in : iphone");

driver.quit();
}

3.ContextClick and doubleClick


In case if there is a requirement to click a button twice or right click we can use the
doubleClick() and contextClick() methods respectively.
Below is s simple code to used to perform both these actions
import static org.testng.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class clickDemo {


public static void main(String[] args) {

// specify the driver location


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");
// instantiate the driver
WebDriver driver = new ChromeDriver();
//specify the URL of the website
driver.get("https://www.amazon.in/");

//maximise the window


driver.manage().window().maximize();

WebElement element = driver.findElement(By.xpath("//*[@id=\"nav-


xshop\"]/a[1]"));

Actions action = new Actions(driver);


action.doubleClick(element).build().perform();

assertEquals(driver.getTitle(), "Mobile Phones: Buy New Mobiles Online at


Best Prices in India | Buy Cell Phones Online - Amazon.in");

driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
action.contextClick().build().perform();

driver.quit();
}
}

4.moveToElement
This method is used to move to a specific target element in the web page. Sometimes there
may be some sub options or submenus which would be visible only when the mouse cursor
is moved over the main menu. In such cases we can use the moveToElement() method. We
can also provide the x coordinate and the y coordinate as parameters in this method in
addition to the target element.
Let us try to automate the below scenario.
Navigate to https://www.lambdatest.com/ website. Mouse hover the Resources tab and click Blog
sub menu to read the articles posted in the blog.
import static org.testng.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class MoveTest {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

//specify the LambdaTest URL


driver.get("https://www.lambdatest.com/");

assertEquals(driver.getTitle(), "Most Powerful Cross Browser


Testing Tool Online | LambdaTest");
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

//specify the locator of the Resources menu


WebElement element =
driver.findElement(By.xpath("//*[@id=\"navbarSupportedContent\"]/ul/li[4]/a"));

Actions act = new Actions(driver);


//mouse hover the Resources element
act.moveToElement(element).build().perform();

//specify the locator for the element Blog and click


driver.findElement(By.linkText("Blog")).click();
assertEquals(driver.getCurrentUrl(),
"https://www.lambdatest.com/blog/");

//verify the page title after navigating to the Blog section

assertEquals(driver.getTitle(), "LambdaTest | A Cross Browser


Testing Blog");

driver.close();
}

5.dragAndDrop
The dragAndDrop(WebElement source,WebElement target) method is used to drag an
element from source and drop it into the target location. There are two ways to perform
this action.
1.Use the dragAndDrop(WebElement source,WebElement target) method
2.Use the below series of actions
clickAndHold(WebElement source)  moveToElement(WebElement target)  release
This is the way we manually drag and drop a file or an image from a source to destination.
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class ActionsTest {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://www.w3schools.com/html/html5_draganddrop.asp");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

Actions action = new Actions(driver);

WebElement source =
driver.findElement(By.xpath("//*[@id=\"drag1\"]"));
WebElement destination =
driver.findElement(By.xpath("//*[@id=\"div2\"]"));
action.clickAndHold(source).moveToElement(destination).release().build().perform
();

driver.quit();
}

6.sendKeys
The sendKeys() method that we have seen earlier was primarily used to send the text. In this
section we shall how can we send other Keys like CTRL,ALT,SHIFT etc.
While we are searching for some products in shopping website, we would type the product
name and press Enter from the keyboard. This action would be same as the one by clicking
the search button.
Below is the code for performing a search operation only using the keyboard actions
import static org.testng.Assert.assertEquals;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class enterDemo {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.amazon.in/");
driver.manage().window().maximize();

Actions action = new Actions(driver);

//specify the locator of the search box


WebElement elementToType =
driver.findElement(By.id("twotabsearchtextbox"));

//pass the name of the product


action.sendKeys(elementToType, "iphone").build().perform();

//pass the Enter value through sendKeys


action.sendKeys(Keys.ENTER).build().perform();
assertEquals(driver.getTitle(), "Amazon.in : iphone");
driver.close();
}

7.KeyUp / KeyDown
The keyUp and keyDown methods are used for imitating the keyboard actions of pressing
and releasing the keys. These methods serve multiple methods like converting the texts into
uppercase or lowercase, copy text from source and paste in destination location, scrolling
up and down the web page, multiple value selection etc.
Let us see sample code for each case.

Converting texts to uppercase


When we want to enter the text manually, we press down the SHIFT key and enter the text
simultaneously without releasing the SHIFT key. The same logic can be applied to our code
like below.
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class keysDemo {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();

WebElement element =
driver.findElement(By.xpath("//*[@id=\"tsf\"]/div[2]/div[1]/div[1]/div/div[2]/in
put"));

Actions action = new Actions(driver);

//holds the SHIFT key and converts the text to uppercase

action.keyDown(element,Keys.SHIFT).sendKeys("lambdatest").build().perform();
driver.quit();

}
}

When the above code is executed, the result would be as seen in the below image.

Scroll up and down the page


You can scroll to the top or the bottom of the page using the Actions class.
import static org.testng.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Scroll {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://www.lambdatest.com/");

assertEquals(driver.getTitle(), "Most Powerful Cross Browser


Testing Tool Online | LambdaTest");
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
Actions act = new Actions(driver);
// Scroll Down using Actions class
act.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Scroll Up using Actions class
act.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).perform();

driver.close();
}

I have intentionally added some wait time in the above code so that you can see how the
page scrolls down and again to the top of the page.

Copy and paste


Copying some text from source and pasting them in target location can also be done with
the help of action class. The logic is same as how we copy and paste texts manually. Below is
the sample code for copying the text and pasting them in another location.

import static org.testng.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class copyPaste {


public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://www.google.com/account/about/");

driver.manage().window().maximize();
WebElement element = driver.findElement(By.xpath("//*[text() = 'Create an
account']"));

element.click();
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);

WebElement firstName = driver.findElement(By.id("firstName"));


WebElement userName = driver.findElement(By.id("username"));

firstName.sendKeys("shalini");

Actions action = new Actions(driver);

action.keyDown( Keys.CONTROL ).sendKeys( "a" ).keyUp( Keys.CONTROL ).build().perform


();

action.keyDown( Keys.CONTROL ).sendKeys( "c" ).keyUp( Keys.CONTROL ).build().perform


();

userName.click();

action.keyDown( Keys.CONTROL ).sendKeys( "v" ).keyUp( Keys.CONTROL ).build().perform


();
driver.close();
}
}

Output

Refresh the page


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class refresh {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://www.amazon.in");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Actions action = new Actions(driver);

action.keyDown(Keys.CONTROL).sendKeys(Keys.F5).build().perform();

driver.quit();
}

Wrapping Up...!!!
To sum it up, we have seen the Actions class and the different methods which are used to
interact with the browser. We have seen examples for performing different actions like
clicking the mouse, typing text, dragging and dropping, move to specific element, double
click, right click, copy and paste content, refreshing a page, converting text to uppercase or
lowercase and so much. Now we would have understood the importance of actions class
and how it could be used in different context.
I hope you have gained knowledge in using Actions in automation script through this article.
After reading this article try to implement them to make your automation scripts effective.
Let me know your feedback on this article in the comment section below. Feel free to share
any other scenarios where you have implemented Actions class effectively. Do share this
with your friends and colleagues so that it would also help me in gaining knowledge in this
topic. Stay tuned until then Happy Testing…!!!

You might also like