The click and hold 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 the following:

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

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

@Test
public void shouldClickAndHold() {

driver.get("http://guidebook.seleniumacademy.com/Sortable.html");

Actions actions = new Actions(driver);

//Move tile3 to the position of tile2
actions.moveByOffset(200, 20)
.clickAndHold()
.moveByOffset(120, 0)
.perform();
}

Let's analyze the following line of code:

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

The tile movement will be similar to the following screenshot:

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, 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. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.12.172