Skip to content

Commit

Permalink
feat(java): ✨ added mouse actions and added test cases (#116)
Browse files Browse the repository at this point in the history
* feat(java): ✨ add method to verify header and  update documentation

* feat(java): ✨ added mouse actions and added test cases
  • Loading branch information
MakodeAjay authored Jul 26, 2022
1 parent 8e0757a commit a0ec76f
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
package com.github.wasiqb.boyka.actions;

import static com.github.wasiqb.boyka.actions.CommonActions.performElementAction;
import static com.github.wasiqb.boyka.actions.ElementFinder.find;
import static com.github.wasiqb.boyka.enums.WaitStrategy.CLICKABLE;
import static com.github.wasiqb.boyka.sessions.ParallelSession.getSession;
import static org.apache.logging.log4j.LogManager.getLogger;

import com.github.wasiqb.boyka.builders.Locator;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

/**
* Perform Mouse actions on UI elements.
Expand All @@ -32,6 +36,19 @@
public final class MouseActions {
private static final Logger LOGGER = getLogger ();

/**
* LongPress on element
*
* @param locator {@link Locator} of element
*/
public static void clickAndHold (final Locator locator) {
LOGGER.traceEntry ();
LOGGER.info ("Click and hold on element: {}", locator);
new Actions (getSession ().getDriver ()).clickAndHold (find (locator, CLICKABLE))
.perform ();
LOGGER.traceExit ();
}

/**
* Click on element
*
Expand All @@ -44,6 +61,59 @@ public static void clickOn (final Locator locator) {
LOGGER.traceExit ();
}

/**
* DoubleClick on element
*
* @param locator {@link Locator} of element
*/
public static void doubleClickOn (final Locator locator) {
LOGGER.traceEntry ();
LOGGER.info ("Double Click on element: {}", locator);
new Actions (getSession ().getDriver ()).doubleClick (find (locator, CLICKABLE))
.perform ();
LOGGER.traceExit ();
}

/**
* DragAndDrop on element
*
* @param source {@link Locator} of element
* @param destination {@link Locator} of element
*/
public static void dragDropTo (final Locator source, final Locator destination) {
LOGGER.traceEntry ();
LOGGER.info ("Drag and Drop on element: {} , {}", source, destination);
new Actions (getSession ().getDriver ()).dragAndDrop (find (source, CLICKABLE), find (destination, CLICKABLE))
.perform ();
LOGGER.traceExit ();
}

/**
* Hover on element
*
* @param locator {@link Locator} of element
*/
public static void hoverOn (final Locator locator) {
LOGGER.traceEntry ();
LOGGER.info ("Hover on element: {}", locator);
new Actions (getSession ().getDriver ()).moveToElement (find (locator, CLICKABLE))
.perform ();
LOGGER.traceExit ();
}

/**
* RightClick on element
*
* @param locator {@link Locator} of element
*/
public static void rightClickOn (final Locator locator) {
LOGGER.traceEntry ();
LOGGER.info ("Right Click on element: {}", locator);
new Actions (getSession ().getDriver ()).contextClick (find (locator, CLICKABLE))
.perform ();
LOGGER.traceExit ();
}

private MouseActions () {
// Utility class
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.wasiqb.boyka.testng.web;

import static com.github.wasiqb.boyka.actions.DriverActions.navigateTo;
import static com.github.wasiqb.boyka.actions.MouseActions.clickOn;
import static com.github.wasiqb.boyka.actions.MouseActions.dragDropTo;
import static com.github.wasiqb.boyka.actions.MouseActions.hoverOn;
import static com.github.wasiqb.boyka.actions.VerifyDriverActions.verifyBrowserUrl;
import static com.github.wasiqb.boyka.actions.VerifyElementActions.verifyTextOf;
import static com.github.wasiqb.boyka.manager.DriverManager.closeDriver;
import static com.github.wasiqb.boyka.manager.DriverManager.createDriver;
import static com.github.wasiqb.boyka.testng.web.pages.MouseEvent.mouseEvent;

import com.github.wasiqb.boyka.enums.ApplicationType;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestMouseAction {

private static final String URL = "https://the-internet.herokuapp.com/";

/**
* Setup test class by initialising driver.
*
* @param appType Application type
* @param driverKey Driver config key
*/
@BeforeClass (description = "Setup test class")
@Parameters ({ "appType", "driverKey" })
public void setupTestClass (final ApplicationType appType, final String driverKey) {
createDriver (appType, driverKey);
}

/**
* Tear down test class by closing driver.
*/
@AfterClass (description = "Tear down test class")
public void tearDownTestClass () {
closeDriver ();
}

/**
* Test dragAndDrop functionality.
*/
@Test (description = "Test dragAndDrop functionality", priority = 1)
public void testDragAndDrop () {
navigateTo (URL);
verifyBrowserUrl ().startsWith (URL);
clickOn (mouseEvent ().getDragAndDropLink ());
dragDropTo (mouseEvent ().getDraggable (), mouseEvent ().getDroppable ());
verifyTextOf (mouseEvent ().getDragAndDropResult ()).isEqualTo ("B");
}

/**
* Test hover functionality.
*/
@Test (description = "Test hover functionality", priority = 2)
public void testMouseHover () {
navigateTo (URL);
verifyBrowserUrl ().startsWith (URL);
clickOn (mouseEvent ().getHoverLink ());
hoverOn (mouseEvent ().getHoverImage ());
verifyTextOf (mouseEvent ().getHoverResult ()).isEqualTo ("name: user1");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.wasiqb.boyka.testng.web.pages;

import static com.github.wasiqb.boyka.builders.Locator.buildLocator;
import static org.openqa.selenium.By.id;
import static org.openqa.selenium.By.xpath;

import com.github.wasiqb.boyka.builders.Locator;
import lombok.Getter;

@Getter
public class MouseEvent {

public static MouseEvent mouseEvent () {
return new MouseEvent ();
}

private final Locator dragAndDropLink = buildLocator ().web (
xpath ("//div[@id='content']//a[text()='Drag and Drop']"))
.build ();
private final Locator dragAndDropResult = buildLocator ().web (xpath ("//div[@id='column-b']/header"))
.build ();
private final Locator draggable = buildLocator ().web (id ("column-a"))
.build ();
private final Locator droppable = buildLocator ().web (id ("column-b"))
.build ();
private final Locator hoverImage = buildLocator ().web (xpath ("(//div[@class='figure'])[1]"))
.build ();
private final Locator hoverLink = buildLocator ().web (xpath ("//div[@id='content']//a[text()='Hovers']"))
.build ();
private final Locator hoverResult = buildLocator ().web (xpath ("(//div[@class='figure'])[1]//h5"))
.build ();

private MouseEvent () {

}
}
46 changes: 46 additions & 0 deletions website/docs/api/actions/mouse-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,49 @@ import static com.github.wasiqb.boyka.actions.MouseActions.clickOn;
. . .
clickOn (locator);
```

## `doubleClickOn`

This method is used to double click on the given element.

```java
import static com.github.wasiqb.boyka.actions.MouseActions.doubleClickOn;
. . .
doubleClickOn (locator);
```

## `rightClickOn`

This method is used to right click on the given element.

```java
import static com.github.wasiqb.boyka.actions.MouseActions.rightClickOn;
. . .
rightClickOn (locator);
```
## `clickAndHold`

This method is used to click and hold on the given element.

```java
import static com.github.wasiqb.boyka.actions.MouseActions.clickAndHold;
. . .
clickAndHold (locator);
```
## `dragAndDropOn`

This method is used to drag and drop on the given element.

```java
import static com.github.wasiqb.boyka.actions.MouseActions.dragAndDropOn;
. . .
dragAndDropOn (locator);
```
## `hoverOn`

This method is used to hover on the given element.

```java
import static com.github.wasiqb.boyka.actions.MouseActions.hoverOn;
. . .
hoverOn (locator);

0 comments on commit a0ec76f

Please sign in to comment.