Actions in Selenium
Actions in Selenium
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.
Mouse actions
The various mouse actions that are provided by the Actions class are
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.
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.
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;
//create an object for the Actions class and pass the driver
arguement
Actions action = new Actions(driver);
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;
//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"));
driver.quit();
}
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;
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;
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
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;
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();
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;
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();
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.
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"));
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.
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;
driver.get("https://www.lambdatest.com/");
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.
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;
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);
firstName.sendKeys("shalini");
userName.click();
Output
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…!!!