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 clicks wherever it is at that point in 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:

@Test
public void shouldMoveByOffSetAndClick() {

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

WebElement seven = driver.findElement(By.name("seven"));
System.out.println("X coordinate: " + seven.getLocation().getX() +
", Y coordinate: " + seven.getLocation().getY());
Actions actions = new Actions(driver);
actions.moveByOffset(seven.getLocation().getX() + 1, seven.

getLocation().getY() + 1).click();
actions.perform()
;
}

In the above example 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 let's 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-widgetheader .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, the width value is 100px, and the 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:

@Test
public void shouldMoveByOffSetAndClickMultiple() {

driver.get("http://guidebook.seleniumacademy.com/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 actions = new Actions(driver);

//Click on One
actions.moveByOffset(one.getLocation().getX() + border, one.getLocation().getY() + border).click();
actions.build().perform();

// Click on Eleven
actions.moveByOffset(2 * tileWidth + 4 * border, 2 * tileHeight + 4 * border).click();
actions.build().perform();

//Click on Five
actions.moveByOffset(-2 * tileWidth - 4 * border, -tileHeight - 2 * border).
click();
actions.build().perform();
}

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

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