Tuesday 4 August 2015

How to handle mouse actions in selenium webdrver usnig java

Selenium Webdriver mouse actions as run as follow.
void click(WebElement onElement)/void click(Coordinates where)
void contextClick(WebElement onElement)/void contextClick(Coordinates where)
void doubleClick(WebElement onElement)/void doubleClick(Coordinates where)
void mouseDown(WebElement onElement)/void mouseDown(Coordinates where)
void mouseUp(WebElement onElement)/void mouseUp(Coordinates where)
void mouseMove(WebElement toElement)/void mouseMove(Coordinates where)
void mouseMove(WebElement toElement, long xOffset, long yOffset)

Method Name: click
Syntax: click(WebElement onElement), click(Coordinates where)
Description: Similar to the existing click() method. It can also perform a click based on coordinates.
Example:
 Actions actions = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu"));
 actions.click(mainmenu).perform();
or
WebElement mainmenu=drive.findElement(By.id("main_menu"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.click(locat.getCoordinates());

Method Name: contextClick
Syntax: contextClick(WebElement onElement), contextClick(Coordinates where)
Description:  Performs a context click/right-click on an element or based on the coordinates
Example:
 Actions actions = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu"));
actions.contextClick(mainmenu).perform();
action.contextClick(mainmenu).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).perform();
or
WebElement mainmenu=drive.findElement(By.id("main_menu"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.contextClick(locat.getCoordinates());

Method Name: doubleClick
Syntax: doubleClick(WebElement onElement),doubleClick(Coordinates where)
Description:  Performs a double-click on the webelement or based on the coordinates. If left empty, it performs double-click on the current location
Example:
Actions action = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu"));
action.doubleClick(mainmenu).perform();
or
WebElement mainmenu=drive.findElement(By.id("main_menu"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.doubleClick(locat.getCoordinates());

Method Name: mouseDown
Syntax: mouseDown(WebElement onElement), mouseDown(Coordinates where)
Description: Performs a mouse-down action on an element or based on coordinates.
Example:
Actions action = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu"));
action.mouseDown(mainmenu).perform();
or
WebElement mainmenu=drive.findElement(By.id("sub_menu"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(locat.getCoordinates());

Method Name: mouseUp
Syntax: mouseUp(WebElement onElement),mouseUp(Coordinates where)
Description: Releases the mouse usually followed by mouse-down and acts based on co-ordinates.
Example:
Actions action = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu2"));
action.mouseUp(mainmenu).perform();
or
WebElement mainmenu=drive.findElement(By.id("sub_menu2"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(locat.getCoordinates());

Method Name: mouseMove
Syntax: mouseMove (WebElement onElement),mouseMove (Coordinates where)
Description: Performs a mouse-move action on an element or based on coordinates.
Example:
Actions action = new Actions(drive);
WebElement mainmenu=drive.findElement(By.id("main_menu"));
action.mouseMove(mainmenu).perform();
or
WebElement mainmenu=drive.findElement(By.id("sub_menu"));
Locatable locat = (Locatable) mainmenu;
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(locat.getCoordinates());

No comments:

Post a Comment