Sunday 26 January 2014

Advanced User Interactions in Selenium !!

The Advanced User Interactions API is a new, more comprehensive API for describing 
actions a user can perform on a web page. Normally we need to find elements and then 
send actions through them. If we need to perform complex tasks like hold down Control and click, then this may not work.The Advanced User Interactions allows us to build these complex interactions with elements in a really nice API. The API relies on two key interfaces for this to work.
Keyboard
The keyboard interface allows keys to be pressed, held down, and released. 
It also allows for normal typing.

Methods available are:
‹1)void sendKeys(CharSequence... keysToSend): Similar to the existing
sendKeys(...) method.
‹2)void pressKey(Keys keyToPress): Sends a key press only, without releasing it.
Should only be implemented for modifier keys (Control, Alt, and Shift).
‹3)void releaseKey(Keys keyToRelease): Releases a modifier key.

Mouse
The mouse interface allows for mouse clicks, double clicks, context clicks, 
as well as moving the mouse to a specific point or to a specific element on the page.

Methods available are:
‹1)void click(WebElement onElement): Similar to the existing click() method
‹2)void doubleClick(WebElement onElement): Double-clicks an element
‹3)void mouseDown(WebElement onElement): Holds down the left mouse button
on an element
‹4)void mouseUp(WebElement onElement): Releases the mouse button
on an element
‹5)void mouseMove(WebElement toElement): Move (from the current location)
to another element
‹6)void mouseMove(WebElement toElement, long xOffset, long yOffset):
Move (from the current location) to new coordinates 
(X coordinates of toElement+ xOffset, Y coordinates of toElement + yOffset)
‹7) void contextClick(WebElement onElement): Performs a context-click (rightclick)

on an element These methods are useful to know but when working and creating a sequence of it, it is better to use the Actions chain generator and then call perform on that class.

Actions
The Actions class allows us to build a chain of actions that we would like to perform.
This means that we can build up a nice sequence, for example "Press Shift and type and
then release", or if we wanted to work with a select that allows multiple selects, we could
press Shift and then do the necessary clicks.

Time for action – selecting multiple items on a select item
  
1 Open up inteliij and create a new Selenium WebDriver project.

2.             WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://book.theautomatedtester.co.uk/multi-select.html");
Actions builder = new Actions(driver);
//WebElement select = driver.findElement(By.tagName("select"));

WebElement select = driver.findElement(By.id("selectWithMultipleEqualsMultiple"));
System.out.println(select.getText());
List<WebElement> options = select.findElements(By.tagName("option"));
System.out.println(options.size());
Action multipleSelect = builder.keyDown(Keys.SHIFT)
.click(options.get(1))
.click(options.get(2))
.build();
multipleSelect.perform();


No comments:

Post a Comment