Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Automating Mouse and Keyboard Interaction using Selenium Webdriver

Using Interactions. Action class, we can automate various mouse and keyboard operations using Selenium Webdriver. We can also create a series of operation using Actions class, build the operations and perform the action in series.

Suppose we visit a shopping site, there can be main categories in the header of the Page, on hovering over a main category, sub categories can be displayed, and we can click on the sub categories.
There are three main steps which should be performed.
  •  Identify the series of actions to be performed. This can be an individual action e.g. hovering over a link to a series of event, e.g. pressing up Key in Keyboard, hovering over a link, click on a sub menu.
  • Once we have identified the series of event we need to build the action.
  • Once we have built the action, we need to perform the action.

This can be explained in code below:

//Import following class libraries
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

Together with this, all the other class libraries for object identification needs to be imported, We assume we have set up for selenium in the machine and knows object Identification using Selenium as this is an advance concept in Selenium.If you want to learn more on basic concepts in Selenium, refer to previous posts in Selenium in this blog or visit the Selenium official website for required knowledge.

//define an object act of Actions class
Actions act = new Actions(driver);
// define the main link in the Page.
WebElement ElemElec = driver.findElement(By.linkText("Electronics"));
// define an object mousehover1 of action class. In action mousehover1, define the flow of event. Example in below case, we define series of events, hovering on the main menu, followed by submenu and then clicking the link.
Action mousehover1 = act.moveToElement(ElemElec).moveToElement(driver.findElement(By.linkText("Access"))).click().build();
mousehover1.perform();

This was an example of using action/ actions to identifying an element in the page using mouse operation and performing action on the same similarly we can automate keyboard actions using Interactions class.

Some of the useful methods of Acton/Actions class are:

  • click() or click(WebElement) – clicks on current mouse location or in the middle of the webElement.

  • doubleClick() or doubleClick(WebElement) – clicks on current mouse location or in the middle of the webElement.

  • dragAndDrop(WebElement src, WebElement tgt) - performs click-and-hold at the location of the source element, releases the mouse at  target location.

  • movetoElement(() or movetoElement(WebElement) – hovers at current location or Element identified by the webElement.

  • SendKeys, KeyUp, and KeyDown – Used to perform keyboard operation by sending keys

  • Build() – Once we have defined the sequence of action to be performed, we use build() to build the sequence of operations to be performed.

  • Perform() – Executing an action.


Details of all the methods of the action or actions class are explained in the Google code site for Selenium.
Please refer http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html for further details on the topic.



In the end, this is 100th Post of this blog. I would like to thanks all readers of the blog for supporting the effort.

No comments:

Post a Comment