Lights, camera, action! Yes, today its all about Actions. Oh, I am not talking about those fight sequences you watch in movies but about the keyboard and mouse actions. 😛 Selenium WebDriver provides a user-facing API for performing complex user gestures. We want automation everywhere! So, instead of using keyboard and mouse directly, we can use Actions Class for performing both basic viz., click
, sendKeys
and complex actions such as dragAndDrop
etc.
Some of the available keyboard and mouse methods are,
1. click(WebElement target)
Parameter: target – element to click.
Description: Clicks in the middle of the given target.
2. clickAndHold()
Description: Clicks at the current mouse location and does not release.
3. doubleClick(WebElement target)
Parameter: target – element to click.
Description: Performs a double click at the middle of the given target element. This is equivalent to: Actions.moveToElement(element).doubleClick();
4. dragAndDrop(WebElement source, WebElement target)
Parameters:
Source – element to be dragged i.e. where button down is to be emulated at
Target – element to move to and release the mouse at
Description: performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
5. dragAndDropBy(WebElement source, int xOffset, int yOffset)
Parameters:
source – element to emulate button down at.
xOffset – horizontal move offset.
yOffset – vertical move offset.
Description: performs click-and-hold at the location of the source element, moves on the x-axis by a given xOffset and on the y-axis by the given yOffset, then releases the mouse.
6. keyDown(java.lang.CharSequence modifier_key)
Parameters:
Modifier_key – It can be either Keys.SHIFT
, Keys.ALT
or Keys.CONTROL
. If the provided key is none of those, IllegalArgumentException
is thrown.
Description: Performs a modifier key press and does not release the key. Subsequent interactions may assume that the key is kept pressed. Note that the modifier key is never released implicitly – either keyUp(theKey)
or sendKeys(Keys.NULL)
must be called to release the modifier key.
7. moveByOffset(int xOffset, int yOffset)
Parameters:
xOffset – horizontal offset. A negative value means moving the mouse left.
xOffset – vertical offset. A negative value means moving the mouse up.
Description: Moves the mouse from its current position (or 0,0) by the given xOffset on the x-axis and yOffset on the y-axis.
8. moveToElement(WebElement target)
Parameters:
target – element to move to.
Description: Moves the mouse to the middle of the target element.
9. perform()
Description: performs or executes all the actions.
10. release()
Description: Releases the depressed left mouse button at the current mouse location.
Overall picture
Let us see a test case implementing some of the methods that we just discussed,
Scenario
- Open Chrome browser
- Navigate to the demo site
- Using Actions Class perform the following keyboard and mouse actions,
- Locate and click on the ‘Bicycle’ checkbox
- Locate the text, ‘Message’ and double-click to highlight it
- Type ‘hi there’ in the text box in uppercase
- Drag
click()
from the first position and drop it in the third position in the sortable list
- Verify the output in the Chrome browser
- Verify Eclipse IDE console output screen for any printed messages and JUnit pane for success result
JUnit code for this scenario is,
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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 TestActions {
// Declaring variables
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception {
// System property set up for Chrome driver
System.setProperty("webdriver.chrome.driver", "browser-drivers\\chromedriver.exe");
// Create a new instance for the class ChromeDriver
// that implements WebDriver interface
driver = new ChromeDriver();
// Implicit wait for 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// Assign the URL to be invoked to a String variable
baseUrl = "https://chandanachaitanya.github.io/selenium-practice-site/";
}
@Test
public void testKeyboardAndMouseActions() throws Exception {
// Open baseUrl in Chrome browser window
driver.get(baseUrl);
// Locate 'Bicycle' checkbox using name
WebElement bicyle = driver.findElement(By.name("vehicle1"));
// Locate 'Message' textbox using id
WebElement messageTextBox = driver.findElement(By.id("enterText"));
// Locate 'click()' using id
WebElement element = driver.findElement(By.id("click"));
//Create an instance of the Actions Class
Actions actions = new Actions(driver);
// Perform multiple actions
// Type 'hi there' in uppercase in the text box
actions
.moveToElement(messageTextBox)
.keyDown(Keys.SHIFT)
.sendKeys(messageTextBox, "hi there")
.keyUp(Keys.SHIFT)
.perform();
// Click 'Bicycle' checkbox
actions
.click(bicyle)
.perform();
// Print a message to console
System.out.println("Bicylce checkbox clicked.");
// Double click/highlight the text, 'Message'
actions
.moveToElement(driver.findElement(By.id("labelText")))
.doubleClick()
.perform();
// Print the text of the element that will be dragged and dropped
System.out.println("Element that is dragged : " + element.getText());
// Drag and drop 'click()' using Actions class
actions
.dragAndDropBy(element, 50, 100)
.perform();
} // End of @Test
@After
public void tearDown() throws Exception {
// Close the Chrome browser
driver.close();
System.out.println("Closing the driver");
}
}
Explanation:
Let us decipher some complex actions.
1. Highlight the text, ‘Message’:
actions
.moveToElement(driver.findElement(By.id("labelText")))
.doubleClick()
.perform();
Move the focus to the text, ‘Message’ and double-click on it to highlight. Use perform()
method to execute the actions.
2. Type ‘hi there’ in uppercase in the text box
actions
.moveToElement(text)
.keyDown(Keys.SHIFT)
.sendKeys(text, "hi there")
.keyUp(Keys.SHIFT)
.perform();
First move the focus to the text box, hold the SHIFT
key down, type the text “hi there” so that it gets typed in uppercase and then release the SHIFT
key with the help of keyup
method.
3. Drag and drop ‘click()’
This can be done in two ways,
actions
.dragAndDropBy(element, 50, 100)
.perform();
The simplest way is to use the dragAndDropBy
method where we can specify the element to be dragged along with the xOffset and yOffset.
If you want to take the complex route and look like a ninja,
actions
.clickAndHold(element)
.moveByOffset(50, 100)
.release()
.perform();
Click on the target element, html()
in our case and make sure not to release the click. Move by a given xOffset and yOffset, then release the mouse click so that the element is dropped in the new location. As a result, the element is dragged and dropped.
Execution Result:
Comments are provided for each line of code making the rest of it self-explanatory. Sprinkling some visuals of the demo site output,
Bicycle checkbox is located and checked as expected.
The text Message
is double-clicked, as a result, highlighted. In the text box, ‘hi there’ is keyed in uppercase. From the sortable list, click()
is dragged from its default first position and dropped at the 3rd position.
In JUnit pane, green bar shows that the test case is executed successfully. Also, the console window shows the absence of any errors along with all the printed messages as expected.
Time to bring out the ninja within you. Experiment with as many Action Class methods as possible and have some fun!
All the code discussed above is available in the GitHub repo, ‘WebDriver’ folder. You can star and fork the repository for convenience. Please go through the README.md
file for clear instructions.
Have a great day!