Tuesday, April 17, 2012

AdvancedUserInteractions API





The Advanced User Interactions API is a new, more comprehensive API for describing actions a user can perform on a web page. This includes actions such as drag and drop or clicking multiple elements while holding down the Control key.

How to :
In order to generate a sequence of actions, use the Actions generator to build it.


Actions builder = new Actions(driver);

   builder.keyDown(Keys.CONTROL)
       .click(someElement)
       .click(someOtherElement)
       .keyUp(Keys.CONTROL);
Then get the action:
      Action selectMultiple = builder.build();
And execute it:
   selectMultiple.perform();


Actions

Keyboard

The Keyboard interface has three methods:
  • void sendKeys(CharSequence... keysToSend) - Similar to the existing sendKeys(...) method.
  • void pressKey(Keys keyToPress) - Sends a key press only, without releasing it. Should only be implemented for modifier keys (Control, Alt and Shift).
  • void releaseKey(Keys keyToRelease) - Releases a modifier key.
It is the implementation's responsibility to store the state of modifier keys between calls. The element which will receive those events is the active element.

Mouse

The Mouse interface includes the following methods (This interface will change soon):
  • void click(WebElement onElement) - Similar to the existing click() method.
  • void doubleClick(WebElement onElement) - Double-clicks an element.
  • void mouseDown(WebElement onElement) - Holds down the left mouse button on an element.
  • Action selectMultiple = builder.build();
  • void mouseUp(WebElement onElement) - Releases the mouse button on an element.
  • void mouseMove(WebElement toElement) - Move (from the current location) to another element.
  • 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).
  • void contextClick(WebElement onElement) - Performs a context-click (right click) on an element.

Some Examples :

Mouse over Action : 
           new Actions(driver).moveToElement(element).perform();
Drag and Drop :
           new Actions(driver).dragAndDrop(dragable, dropAt).perform();

will update some more useful Actions here in future.




2 comments:

  1. Have you tried with click muti row by hold shift key?

    I select multi rows by clicking 1st element and holding shift key then click last element.
    Code:
    Actions actions = new Actions(driver);
    Action selectThreeItems = actions
    .click(line1)
    .keyDown(Keys.SHIFT)
    .click(line2)
    .keyUp(Keys.SHIFT)
    .build();

    selectThreeItems.perform();

    Unfortunately, It doesn't work?

    ReplyDelete
  2. For selecting multiple values from MultiSelect Pick list you can use 'Select' class itself.

    try below logic. (It is working for me to select multiple values)

    Select select=new Select(driver.findElement(Select element));
    select.selectByVisibleText(Option1);
    select.selectByVisibleText(Option3);
    select.selectByVisibleText(Option5);

    ReplyDelete

Note: Only a member of this blog may post a comment.