Exploring Advanced Interactions of WebDriver

Exclusive offer: get 50% off this eBook here
Selenium WebDriver Practical Guide

Selenium WebDriver Practical Guide — Save 50%

Interactively automate web applications using Selenium WebDriver with this book and ebook

$26.99    $13.50
by Satya Avasarala | January 2014 | Web Development

In this article written by Satya Avasarala, the author of the book Selenium WebDriver Practical Guide, we will go through some advanced ways of performing actions on WebElements.

(For more resources related to this topic, see here.)

Understanding actions, build, and perform

We know how to take some basic actions, such as clicking on a button and typing text into a textbox; however, there are many scenarios where we have to perform multiple actions at the same time. For example, keeping the Shift button pressed and typing text for uppercase letters, and the dragging and dropping mouse actions.

Let's see a simple scenario here. Open the selectable.html file that is attached with this book. You will see tiles of numbers from 1 to 12. If you inspect the elements with Firebug, you will see an ordered list tag (<ol>) and 12 list items (<li>) under it, as shown in the following code:

<ol id="selectable" class="ui-selectable"> <li class="ui-state-default ui-selectee" name="one">1</li> <li class="ui-state-default ui-selectee" name="two">2</li> <li class="ui-state-default ui-selectee" name="three">3</li> <li class="ui-state-default ui-selectee" name="four">4</li> <li class="ui-state-default ui-selectee" name="five">5</li> <li class="ui-state-default ui-selectee" name="six">6</li> <li class="ui-state-default ui-selectee" name="seven">7</li> <li class="ui-state-default ui-selectee" name="eight">8</li> <li class="ui-state-default ui-selectee" name="nine">9</li> <li class="ui-state-default ui-selectee" name="ten">10</li> <li class="ui-state-default ui-selectee" name="eleven">11</li> <li class="ui-state-default ui-selectee" name="twelve">12</li> </ol>

If you click a number, it's background color changes to orange. Try selecting the 1, 3, and 5 numbered tiles. You do that by holding the Ctrl key + 1 numbered tile + 3 numbered tile + 5 numbered tile. So, this involves performing multiple actions, that is, holding the Ctrl key continuously and clicking on 1, 3, and 5 tiles. How do we perform these multiple actions using WebDriver? The following code demonstrates that:

public class ActionBuildPerform {     public static void main(String... args) {       WebDriver driver = new FirefoxDriver();       driver.get("file://C:/selectable.html");       WebElement one = driver.findElement(By.name("one"));       WebElement three = driver.findElement(By.name("three"));      WebElement five = driver.findElement(By.name("five"));       // Add all the actions into the Actions builder.      Actions builder = new Actions(driver);         builder.keyDown( Keys.CONTROL )               .click(one)              .click(three)              .click(five)              .keyUp(Keys.CONTROL);        // Generate the composite action.        Action compositeAction = builder.build();        // Perform the composite action.        compositeAction.perform( );       }    }

Now, if you see the code, line number 9 is where we are getting introduced to a new class named Actions. This Actions class is the one that is used to emulate all the complex user events. Using this, the developer of the test script could combine all the necessary user gestures into one composite action. From line 9 to line 14, we have declared all the actions that are to be executed to achieve the functionality of clicking on the numbers 1, 3, and 5. Once all the actions are grouped together, we build that into a composite action. This is contained on line 16. Action is an interface that has only the perform() method, which executes the composite action. Line 18 is where we are actually executing the action using the perform() method.

So, to make WebDriver perform multiple actions at the same time, you need to follow a three-step process of using the user-facing API of the Actions class to group all the actions, then build the composite action, and then the perform the action. This process can be made into a two-step process as the perform() method internally calls the build() method. So the previous code will look as follows:

public class ActionBuildPerform {     public static void main(String... args) {       WebDriver driver = new FirefoxDriver();       driver.get("file://C:/selectable.html");       WebElement one = driver.findElement(By.name("one"));       WebElement three = driver.findElement(By.name("three"));      WebElement five = driver.findElement(By.name("five"));       // Add all the actions into the Actions builder.     Actions builder = new Actions(driver);         builder.keyDown( Keys.CONTROL )               .click(one)              .click(three)              .click(five)              .keyUp(Keys.CONTROL);        // Perform the action.        builder.perform( );   } }

In the preceding code, we have directly invoked the perform() method on the Actions instance, which internally calls the build() method to create a composite action before executing it.

In the subsequent sections of this article, we will take a closer look at the Actions class. All the actions are basically divided into two categories: mouse-based actions and keyboard-based actions. In the following sections, we will discuss all the actions that are specific to the mouse and keyboard available in the Actions class.

Learning mouse-based interactions

There are around eight different mouse actions that can be performed using the Actions class. We will see each of their syntax and a working example.

The moveByOffset action

The moveByOffset method is used to move the mouse from its current position to another point on the web page. Developers can specify the X distance and Y distance the mouse has to be moved. When the page is loaded, generally the initial position of a mouse would be (0, 0), unless there is an explicit focus declared by the page.

The API syntax for the moveByOffset method is as follows:

public Actions moveByOffset(int xOffSet, int yOffSet)

In the preceding code, xOffSet is the input parameter providing the WebDriver the amount of offset to be moved along the x axis. A positive value is used to move the cursor to the right, and a negative value is used to move the cursor to the left.

yOffSet is the input parameter providing the WebDriver the amount of offset to be moved along the y axis. A positive value is used to move the cursor down along the y axis and a negative value is used to move the cursor toward the top.

When the xOffSet and yOffSet values result in moving the cursor out of the document, a MoveTargetOutOfBoundsException is raised.

Let's see a working example of it. The objective of the following code is to move the cursor on to the number 3 tile on the web page:

 public class MoveByOffSet{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Selectable.html");     WebElement three = driver.findElement(By.name("three"));     System.out.println("X coordinate: "+three.getLocation().getX()+"
  Y coordinate: "+three.getLocation().getY());     Actions builder = new Actions(driver);     builder.moveByOffset(three.getLocation().getX()+1,
three.getLocation().getY()+1);     builder.perform();   }  }

We have added +1 to the coordinates, because if you observe the element in Firebug, we have a style border of 1 px. Border is a CSS-style attribute, which when applied to an element, will add a border of the specified color around the element with the specified amount of thickness. Though the previous code does move your mouse over tile 3, we don't realize it because we are not doing any action there. We will see that when we use this moveByOffset() method in combination with the click method shortly.

The moveByOffset() method may not work in Mac OSX and may raise a JavaScript error when used independently like the previous code.

The click at current location action

The click method is used to simulate the left-click of your mouse at its current point of location. This method doesn't really realize where or on which element it is clicking. It just blindly clicks wherever it is at that point of time. Hence, this method is used in combination with some other action rather than independently, to create a composite action.

The API syntax for the click method is as follows:

public Actions click()  

The click method doesn't really have any context about where it is performing its action; hence, it doesn't take any input parameter.

Let's see a code example of the click method:

public class MoveByOffsetAndClick{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Selectable.html");     WebElement seven = driver.findElement(By.name("seven"));     System.out.println("X coordinate: "+seven.getLocation().getX()+" Y coordinate: "+seven.getLocation().getY());     Actions builder = new Actions(driver);     builder.moveByOffset( seven.getLocation ().getX()+1, seven.getLocation().getY()+1).click();     builder.perform();   } }

Line 8 is where we have used a combination of the moveByOffset() and click() methods to move the cursor from point (0, 0) to the point of tile 7. Because the initial position of the mouse is (0, 0), the X, Y offset provided for the moveByOffset() method is nothing but the location of the tile 7 element. Now, lets try to move the cursor from tile 1 to tile 11 and from there to tile 5 and see how the code looks. Before we get into the code, let's inspect the selectable.html page using Firebug. The following is the style of each tile:

#selectable li {     float: left;     font-size: 4em;     height : 80px;     text-align: center;     width : 100px; } .ui-state-default, .ui-widget-content .ui-state-default, .ui-widget-header .ui-state-default {     background: url("images/ui-bg_glass_75_e6e6e6_1x400.png") repeat-x scroll 50% 50% #E6E6E6;     border : 1px solid #D3D3D3;     color: #555555;     font-weight: normal; }

The three elements with which we are concerned for our offset movement in the preceding style code are: height, width, and the border thickness. Here, the height value is 80px, width value is 100px, and border value is 1px. Use these three factors to calculate the offset to navigate from one tile to the other. Note that the border thickness between any two tiles will result in 2 px; that is, 1 px from each tile. The following is the code that uses the moveByOffset and click() methods to navigate from tile 1 to tile 11, and from there to tile 5:

public class MoveByOffsetAndClick{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Selectable.html");     WebElement one = driver.findElement(By.name("one"));     WebElement eleven = driver.findElement(By.name("eleven"));     WebElement five = driver.findElement(By.name("five"));     int border = 1;     int tileWidth = 100;     int tileHeight = 80;     Actions builder = new Actions(driver);     //Click on One     builder.moveByOffset( one.getLocation ().getX()+border, one.getLocation().getY()+border).click();     builder.build().perform();     // Click on Eleven     builder.moveByOffset( 2*tileWidth+4*border, 2*tileHeight+4*border).click();     builder.build().perform();    //Click on Five     builder.moveByOffset( -2*tileWidth-4*border, -tileHeight-2*border).click();     builder.build().perform();    }  }

Selenium WebDriver Practical Guide Interactively automate web applications using Selenium WebDriver with this book and ebook
Published: January 2014
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

The click on a WebElement action

We have seen how to click a WebElement by calculating the offset to it. This process may not be needed every time, especially when the WebElement has its own identifiers, such as a name or ID. We can use another overloaded version of the click() method to click directly on the WebElement.

The API syntax for clicking on a WebElement is as follows:

public Actions click(WebElement onElement)

The input parameter for this method is an instance of the WebElement on which the click action should be performed. This method, like all the other methods in the Actions class, will return an Actions instance.

Now, let's try to modify the previous code example to use the click(WebElement) method instead of using the moveByOffset() method to move to the location of the WebElement and clicking on it using the click() method:

public class ClickOnWebElement{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Selectable.html");     WebElement one = driver.findElement(By.name("one"));     WebElement eleven = driver.findElement(By.name("eleven"));     WebElement five = driver.findElement(By.name("five"));     Actions builder = new Actions(driver);     //Click on One     builder.click( one);     builder.build().perform();     // Click on Eleven     builder.click( eleven);     builder.build().perform();     //Click on Five     builder.click( five)     builder.build().perform();   } }

Now the moveByOffset() method has been replaced by the click(WebElement) method, and all of a sudden the complex coordinate geometry has been removed from the code. If you're a tester, this is one more good reason to push your developers to provide identifiers for the WebElements.

If you observe the previous code or the moveByOffset and click class code, all the operations of moving the mouse and clicking on one, eleven, and five tiles are built separately and performed separately. This is not how we use our Actions class. You can actually build all these actions together and then perform them. So, the preceding code will turn out to be as follows:

public class ClickOnWebElement{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Selectable.html");     WebElement one = driver.findElement(By.name("one"));     WebElement eleven = driver.findElement(By.name("eleven"));     WebElement five = driver.findElement(By.name("five"));     Actions builder = new Actions(driver);     //Click on One, Eleven and Five     builder.click( one).click(eleven).click(five);     builder.build().perform();   } }

The clickAndHold at current location action

The clickAndHold() method is another method of the Actions class that left-clicks on an element and holds it without releasing the left button of the mouse. This method will be useful when executing operations such as drag-and-drop. This method is one of the variants of the clickAndHold method that the Actions class provides. We will discuss the other variant in the next section.

Now, open the Sortable.html file that came with the book. You can see that the tiles can be moved from one position to the other. Now let's try to move tile 3 to the position of tile 2. The sequence of steps that are involved to do this are:

  1. Move the cursor to the position of tile 3.
  2. Click and hold tile 3.
  3. Move the cursor in this position to the tile 2 location.

Now, let's see how this can be accomplished using the WebDriver's clickAndHold() method:

public class ClickAndHold{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Sortable.html");     Actions builder = new Actions(driver);     //Move tile3 to the position of tile2     builder.moveByOffset( 200, 20)            .clickAndHold()            .moveByOffset(120, 0)            .perform();   } }

Let's analyze the following line of code:

 builder.moveByOffset( 200, 20)         .clickAndHold()         .moveByOffset(120, 0)         .perform();

First we move the cursor to the location of tile 3. Then we click and hold tile 3. Then, we move the cursor by 120px horizontally to the position of tile 2. The last line performs all the preceding actions. Now, execute this in your eclipse and see what happens. If you observe closely, our tile 3 doesn't properly go into the position of tile 2. This is because we are yet to release the left button. We just commanded the WebDriver to click and hold, but not to release. Yes, in a short while, we will discuss the release() method of WebDriver.

The clickAndHold a WebElement action

In the previous section, we have seen the clickAndHold() method, which will click and hold a WebElement at the current position of the cursor. It doesn't care with which element it is dealing with. So, if we want to deal with a particular WebElement on the web page, we have to first move the cursor to the appropriate position and then perform the clickAndHold() action. In order to avoid the hassle of moving the cursor geometrically, WebDriver provides the developers with another variant or overloaded method of the clickAndHold() method that takes the WebElement as input.

The API syntax is as follows:

public Actions clickAndHold(WebElement onElement)

The input parameter for this method is the WebElement that has to be clicked and held. The return type, as in all the other methods of the Actions class, is the Actions instance.

Now, let's refactor the example in the previous section to use this method, as follows:

public class ClickAndHold{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Sortable.html");     WebElement three = driver.findElement(By.name("three"));     Actions builder = new Actions(driver);     //Move tile3 to the position of tile2     builder.clickAndHold( three)            .moveByOffset(120, 0)            .perform();   } }

The only change is that we have removed the action of moving the cursor to the (200, 20) position and provided the WebElement to the clickAndHold() method that will take care of identifying the WebElement.

The release at current location action

Now in the previous example, we have seen how to click and hold an element. The ultimate action that has to be taken on a held WebElement is to release it so that the element can be dropped or released from the mouse. The release() method is the one that can release the left mouse button on a WebElement.

The API syntax for the release method is as follows:

public Actions release()

The preceding method doesn't take any input parameter and returns the Actions class instance.

Now, let's modify the previous code to include release action in it:

public class ClickAndHoldAndRelease{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Sortable.html");     WebElement three = driver.findElement(By.name("three"));     Actions builder = new Actions(driver);     //Move tile3 to the position of tile2     builder.clickAndHold( three)            .moveByOffset(120, 0)            .release()            .perform();   } }

The preceding code will make sure that the mouse is released at the specified location.

The release on another WebElement action

This is an overloaded version of the release() method. Using this, you can actually release the currently held WebElement in the middle of another WebElement. In this way, we don't have to calculate the offset of the target WebElement from the held WebElement.

The API syntax is as follows:

public Actions release(WebElement onElement)

The input parameter for the preceding method is obviously the target WebElement where the held WebElement should be dropped. The return type is the instance of the Actions class.

Let's modify the preceding code example to use this method:

public class ClickAndHoldAndReleaseOnWebElement{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Sortable.html");     WebElement three = driver.findElement(By.name("three"));     WebElement two = driver.findElement(By.name("two"));     Actions builder = new Actions(driver);     //Move tile3 to the position of tile2     builder.clickAndHold( three)            .release(two)            .perform();   } }

Check how simple the preceding code looks. We have removed all the moveByOffset code and added the release() method that takes the WebElement with the name two as the input parameter.

Invoking the release() or release(WebElement) methods without calling the clickAndHold() method will result in an undefined behavior.

The moveToElement action

The moveToElement() method is another method of WebDriver that helps us to move the mouse cursor to a WebElement on the web page.

The API syntax for the moveToElement method is as follows:

public Actions moveToElement(WebElement toElement)

The input parameter for the preceding method is the target WebElement where the mouse should be moved.

Now, go back to The clickAndHold at current location action section of this article and try to modify the code to use this method. The following is the code we have written in the The clickAndHold at current location action section:

public class ClickAndHold{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Sortable.html");     Actions builder = new Actions(driver);     //Move tile3 to the position of tile2     builder.moveByOffset( 200, 20)            .clickAndHold()            .moveByOffset(120, 0)            .perform();   } }

In the preceding code, we will replace the moveByOffset(x, y) method with the moveToElement(WebElement) method:

public class ClickAndHold{   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/Sortable.html");     WebElement three = driver.findElement(By.name("three"));     Actions builder = new Actions(driver);     //Move tile3 to the position of tile2     builder.moveToElement( three)            .clickAndHold()            .moveByOffset(120, 0)            .perform();   } }

In the preceding code, we have moved to tile 3, clicked and held it, and then moved to the location of tile 2 by specifying its offset. If you want, you can add the release() method before the perform() method.

There might be a number of ways to achieve the same task. It is up to the user to choose the appropriate ones that best suit the given circumstances.

Selenium WebDriver Practical Guide Interactively automate web applications using Selenium WebDriver with this book and ebook
Published: January 2014
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

The dragAndDropBy action

There might be many instances where we may have to drag-and-drop components or WebElements of a web page. We can accomplish that by using many of the actions seen until now. But WebDriver has given us a convenient out of the box method to use. Let's see its API syntax.

The API syntax for the dragAndDropBy method is as follows:

public Actions dragAndDropBy(WebElement source, int xOffset,int yOffset)

The WebElement input parameter is the target WebElement to be dragged, the xOffset parameter is the horizontal offset to be moved, and the yOffset parameter is the vertical offset to be moved.

Let's see a code example for it. Open the HTML file, DragMe.html, provided with this book. You can actually drag that rectangle to any location on the web page. Let's see how we can do that using WebDriver. The following is the code example for that:

public class DragMe {   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/DragMe.html");     WebElement dragMe = driver.findElement(By.id("draggable"));     Actions builder = new Actions(driver);     builder.dragAndDropBy( dragMe , 300, 200).perform();   } }

In the preceding code, dragMe is the WebElement that is identified by it's Id, and that is dragged 300px horizontally and 200px vertically.

The dragAndDrop action

The dragAndDrop method is similar to the dragAndDropBy() method. The only difference being that instead of moving the WebElement by an offset, we move it on to a target element.

The API syntax for the dragAndDrop method is as follows:

public Actions dragAndDrop(WebElement source, WebElement target)

The input parameters for the preceding method are the WebElement source and the WebElement target, while the return type is the Actions class.

Let's see a working code example for it. Open the DragAndDrop.html file that is provided with the book. Here we can actually drag the Drag me to my target rectangle to the Drop here rectangle. Try that. Let's see how that can be achieved using WebDriver:

public class DragAndDrop {   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/DragAndDrop.html");     WebElement src = driver.findElement(By.id("draggable"));     WebElement trgt = driver.findElement(By.id("droppable"));     Actions builder = new Actions(driver);     builder.dragAndDrop( src , trgt).perform();   } }

In the preceding code, the source and target WebElements are identified by their IDs, and the dragAndDrop() method is used to drag one to the other.

The doubleClick At current location action

Moving on to another action that can be performed using mouse, doubleClick() is another out of the box method that WebDriver provides to emulate the double-clicking of the mouse. This method, like the click() method, comes in two flavors. One is double-clicking a WebElement, which we will discuss in next section; the second is clicking at the current location of the cursor, which will be discussed here.

The API syntax is as follows:

public Actions doubleClick()

Obviously, the preceding method doesn't take any input parameters, as it just clicks on the current cursor location and returns an Actions class instance.

Let's see how the previous code can be converted to use this method:

public class DoubleClick {   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/DoubleClick.html");     WebElement dblClick= driver.findElement(By.name("dblClick"));     Actions builder = new Actions(driver);     builder.moveToElement( dblClick ).doubleClick().perform();   } }

In the preceding code, we have used the moveToElement(WebElement) method to move the mouse to the location of the button element, and just double-clicked at the current location.

The doubleClick on WebElement action

Now that we have seen a method that double-clicks at the current location, we will discuss another method that WebDriver provides to emulate the double-clicking of a WebElement.

The API syntax for the doubleClick method is as follows:

public Actions doubleClick(WebElement onElement)

The input parameter for the preceding method is the target WebElement that has to be double-clicked and the return type is the Actions class.

Let's see a code example for this. Now, open the DoubleClick.html file, and click (single) on the Click Me button. You shouldn't see anything happening. Now double-click on the button; you should see an alert saying Double Clicked !!. Now, we try to do the same thing using WebDriver. The following is the code to do that:

public class DoubleClick {   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/DoubleClick.html");     WebElement dblClick = driver.findElement(By.name("dblClick"));     Actions builder = new Actions(driver);     builder.doubleClick( dblClick ).perform();   } }

After executing the preceding code, you should see an alert dialog saying that the button has been double-clicked.

The contextClick on WebElement action

The contextClick() method, also known as right-click, is quite common on many web pages these days. The context is nothing but a menu; a list of items is associated to a WebElement based on the current state of the web page. This context menu can be accessed by a right-click of the mouse on the WebElement. WebDriver provides the developer with an option of emulating that action using the contextClick() method. Like many other methods, this method has two variants as well. One is clicking on the current location and the other overloaded method is clicking on the WebElement. Lets discuss the context clicking on WebElement here.

The API syntax for the contextClick method is as follows:

public Actions contextClick(WebElement onElement)

The input parameter is obviously the WebElement that has to be right-clicked, and the return type is the Actions instance.

As we do normally, its time to see a code example. If you open the ContextClick.html file, you can right-click on the text visible on the page and it will display the context menu. Now, clicking any item pops up an alert dialog stating which item has been clicked. Now, let's see how to implement this in WebDriver in the following code:

public class ContextClick {   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/ContextClick.html");     WebElement contextMenu = driver.findElement(By.id("div-context"));     Actions builder = new Actions(driver);     builder.contextClick( contextMenu )        .click(driver.findElement(By.name("Item 4")))            .perform();   } }

In the preceding code, first we have right-clicked using the contextClick() method on the WebElement contextMenu, and then left-clicked on Item 4 from the context menu. This should pop up an alert dialog saying Item 4 Clicked.

The contextClick at current location action

Now that we have seen context click on a WebElement, its time to explore the contextClick() method at the current mouse location.

The API syntax for the contextClick method is as follows:

public Actions contextClick()

As expected, the preceding method doesn't expect any input parameter, and returns the Actions instance. Let's see the necessary modifications needed to the previous example in order to use this method. The following is the code refactored to achieve this:

public class ContextClick {   public static void main(String... args) {     WebDriver driver = new FirefoxDriver();     driver.get("file://C:/ContextClick.html");     WebElement contextMenu = driver.findElement(By.id("div-context"));     Actions builder = new Actions(driver);     builder.moveToElement( contextMenu )             .contextClick()             .click(driver.findElement(By.name("Item 4")))            .perform();   } }

The preceding code first moves the cursor to the div-context WebElement, and then context clicks it.

Learning keyboard-based interactions

Until now, we have seen all the actions that can be taken using a mouse. It's time to look at some of the actions that are specific to the keyboard in the Actions class. Basically, there are three different actions that are available in the Actions class that are specific to the keyboard. They are the keyUp, keyDown, and sendKeys actions, each having two overloaded methods. One method is to execute the action directly on the WebElement, and the other is to just execute the method irrespective of its context.

The keyDown and keyUp actions

The keyDown() method is used to simulate the action of pressing and holding a key. The keys that we are referencing here are Shift, Ctrl, and Alt keys. The keyUp() method is used to release the key that is already pressed using the keyDown() method. The API syntax for the keyDown() method is as follows

public Actions keyDown(Keys theKey) throws IllegalArgumentException

An IllegalArgumentException is thrown when the passed key is not one of the Shift, Ctrl, and Alt keys.

The API syntax for the keyUp method is as follows

public Actions keyUp(Keys theKey)

The keyUp action performed on a key, on which a keyDown action is not already being performed, will result in some unexpected results. So, we have to make sure we perform the keyUp action after a keyDown action is performed.

The sendKeys method

This is used to type in alphanumeric and special character keys into WebElements such as textbox, textarea, and so on. This is different from the WebElement.sendKeys(CharSequence keysToSend) method, as this method expects the WebElements to have the focus before being called. The API syntax for the sendkeys() method is as follows:

public Actions sendKeys(CharSequence keysToSend)

We expect you to implement a couple of test scripts around these keyboard events using the keyUp, keyDown, and sendKeys() methods.

Summary

In this article, we have learned how to use the Actions class to create a set of actions, and build them into a composite action to execute it in one pass using the perform() method. In this way, we can aggregate a series of complex user actions into a single functionality, which can be executed in one pass.

Resources for Article:



About the Author :


Satya Avasarala

Satya Avasarala has rich experience in Java development and automation testing. He is an engineer in computer science. He has used WebDriver for many years now and has created several good automation frameworks. He has worked at various large software enterprises such as Oracle Corp, Yahoo! Inc., VMware Inc., and the REA Group.

In addition, he is also interested in Service Oriented Architectural design and Business Intelligence. He is an Oracle-certified Service Oriented Architecture Infrastructure Implementation Expert and a Business Intelligence Foundation Suite Implementation Specialist.

Books From Packt


Selenium 2 Testing Tools: Beginner’s Guide
Selenium 2 Testing Tools: Beginner’s Guide

Application Testing with Capybara
Application Testing with Capybara

Selenium 1.0 Testing Tools: Beginner’s Guide
Selenium 1.0 Testing Tools: Beginner’s Guide

Web 2.0 Solutions with Oracle WebCenter 11g
Web 2.0 Solutions with Oracle WebCenter 11g

 Instant Selenium Testing Tools Starter
Instant Selenium Testing Tools Starter

Spring Web Flow 2 Web Development
Spring Web Flow 2 Web Development

Selenium Testing Tools Cookbook
Selenium Testing Tools Cookbook

 Spring Web Services 2 Cookbook
Spring Web Services 2 Cookbook


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Z
k
M
W
2
j
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software