In this article, we will learn to handle Mouse Click & Keyboard Event in Selenium Webdriver.
Using Advanced User Interactions API, we can handle special mouse and keyboard events in Selenium Webdriver. These API contains Action classes and Actions that are needed when executing keyboard and mouse events. Action class provides the most commonly used mouse and keyboard events. Selenium has an in-built facility to handle different types of mouse and keyboard events. We use org.openqa.selenium.interactions Actions class to do action events. We use these selenium actions class rather than using Mouse or Keyboard directly.
Methods Available in Selenium Actions Class:
Keyboard Events Using Selenium Actions Class API:
The Keyboard interface has the methods mentioned below:
- sendKeys(keysToSend): It sends a series of keystrokes onto the element
- keyDown(theKey): It does not release the modifier key. Subsequent interactions may assume it as kept pressed.
Parameters:
Modifier key sample for KeyDown = keys.ALT, keys.SHIFT, or keys.CONTROL, etc.
- keyUp(theKey): Releases already pressed modified key
Parameters:
Modifier key sample for KeyUp = keys.ALT, keys.SHIFT, or keys.CONTROL, etc.
Mouse Events Using Selenium Actions Class API:
The Mouse interface has the methods mentioned below:
- click(): It simply clicks on the element
- clickAndHold(): It clicks at the mouse pointed location(without releasing)
- contextClick(): It performs a context-click at the mouse pointed location (Right Click)
- doubleClick(): It performs a double-click at the mouse pointed location
- dragAndDrop(source, target): It performs a click-and-hold action at the location of the source element, and moves to the location of the target element, then it releases the mouse
source- web element to emulate button down at.
target- web element to move to and release the mouse at.
- dragAndDrop(source, x-offset, y-offset): It performs click-and-hold at the source element location of the target element and moves by a given offset, and then it releases the mouse.
source- web element to emulate button down at.
xOffset- offset of horizontal move
yOffset- offset of vertical move
- moveByOffset(x-offset, y-offset): It moves the mouse from its current position by the given offset.
x-Offset- horizontal offset. (negative value = moving the mouse left)
y-Offset- vertical offset. (negative value = moving the mouse up)
- moveToElement(toElement): It moves the mouse to the center of the web element
- release(): It releases the pressed left mouse button at the current mouse location pointer
In the following example, we use the method moveToElement() to mouse-over. Consider the example below:
Go to the amazon.in website and Mouse-hover to Account & Lists and click on Your Account.
Step 1: Import the Action and Actions classes
Step 2: Instantiate an Actions class
Step 3: Instantiate an Action using the Actions object
Step 4: When executing the Action object, use the perform() method.
Below is the whole WebDriver code to mouse-hover to Account & Lists and click on Your Account link.
Let’s create a test case in which we will automate the following scenarios to handle Mouse-Hover:
- Invoke a Google chrome browser.
- Open URL: https://www.amazon.in/
- Mouse-hover to Account & Lists and click on Your Account link.
- Close the browser.
Now, we will create a test case step by step to understand how to handle Mouse-hover in WebDriver.
Step 1: Launch Eclipse IDE.
Step 2: Go to File > New > Click on Java Project.
Step 3: Right-click on the Project folder and click on the New > class.
Give your Class name as “Test_Mousehover” and Select the checkbox “public static void main(String[] args) and click on the “Finish” button.
Step 4: Invoke the Google Chrome browser and set the system property to the path of your chromedriver.exe file.
Here is the sample code to set Chrome driver system property:
// System Property for Chrome Driver
System.setProperty("webdriver.chrome.driver", “ D:\\Drivers\\geckodriver.exe ");
After that, we have to initialize the Chrome driver using ChromeDriver Class. Below is the sample code to initialize Chrome driver using ChromeDriver class.
// Instantiate a ChromeDriver class.
WebDriver driver=new ChromeDriver();
We will get the below code to launch the Google Chrome browser after combining both of the above codes.
System.setProperty("webdriver.chrome.driver", “ D:\\Drivers\\geckodriver.exe ");
WebDriver driver=new ChromeDriver();
After that, we need to navigate to the desired URL.
Below is the sample code to navigate to the desired URL:
// Launch Website driver.navigate().to(“https://www.amazon.in/”);
Here is the complete code for above scenario:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test_Dropdown {
public static void main(String[] args) {
// System Property for Chrome Driver
System.setProperty("webdriver.chrome.driver", “ D:\\Drivers\\geckodriver.exe ");
// Instantiate a ChromeDriver class.
WebDriver driver=new ChromeDriver();
// Launch Website
driver.navigate().to("https://www.amazon.in/");
}
}
Step 5: Now we will try to Mouse-Hover to Accounts & Lists and click on Your Account.
Follow the below steps to locate the drop-down menu on the web page.
- Open URL: https://www.amazon.in/
- Right-click on the Accounts & Lists on the web page and click on Inspect
Step 6: Now we try to locate the “Accounts & Lists” by inspecting its HTML code.
Note the id attribute of “Accounts & Lists”.
Step 7: Now we need to write the code for Account & Lists.
Below is the sample code for Account & Lists:
WebElement menuSignIn = driver.findElement(By.id(“nav-link-accountList”));
Actions act = new Actions (driver);
act.moveToElement(menuSignIn).perform();
Step 8: Now inspect the element “Your Account”.
Now the Xpath of the “Your Account” is
Step 9: Now we need to write the code for Your Account.
Below is the sample code for Your Account
driver.findElement(By.xpath(“//span[text()=’Your Account’]”)).click();
Now, our final test script will look something like:
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 Test_Mousehover {
public static void main(String[] args) {
// TODO Auto-generated method stub
// System Property for Chrome Driver
System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
// Instantiate a ChromeDriver class.
WebDriver driver=new ChromeDriver();
// Launch Website
driver.navigate().to("https://www.amazon.in/");
WebElement menuSignIn = driver.findElement(By.id("nav-link-accountList"));
Actions act = new Actions (driver);
act.moveToElement(menuSignIn).perform();
driver.findElement(By.xpath("//span[text()=Your Account']")).click();
// Close the Browser
driver.close();
}
}
Keyboard Interface Methods
sendKeys(onElement, charSequence): This sendKeys() method is used to send a series of keys to a web element.
releaseKey(): This method releases the key on the keyboard and presskeys
pressKeys(): This method is used to post special keys of the keyboard like “shift”, “ctrl”, “f1”, “tab”, etc.
Example:
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.Action;
import org.openqa.selenium.interactions.Actions;
public class keyboardEvents {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
// Instantiate a ChromeDriver class.
WebDriver driver=new ChromeDriver();
driver.get("http://www.google.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement text = driver.findElement(By.name("q"));
Actions make = new Actions(driver);
Action keyboardEvents = make.keyDown(text, Keys.SHIFT).sendKeys("Selenium Webdriver Java")
.keyUp(text, Keys.SHIFT).doubleClick().contextClick().build();
keyboardEvents.perform();
}
}
From the above example, The keyword “Selenium Webdriver Java” is sent to the google site search box. Thus, it is changed to the UPPERCASE with method keyDown() and the method doubleClick() double clicks on it to highlight the text and method contextClick() double click on the text and thus contextual menu is displayed.
The above example was a demonstration of Advance User Interactions API in Selenium Webdriver and the use of Keyboard and Mouse events on the web similar to user’s interaction.
Conclusion
- Using AdvancedUserInteractions API, we can handle special keyboard and mouse events.
- doubleClick(), keyUp, dragAndDropBy, contextClick & sendKeys are most frequently used Keyword and Mouse Events.
One Response