From c079e25cf708f366a24c0ac5da53de1f7aa6ebcf Mon Sep 17 00:00:00 2001 From: Mohab Mohie Date: Sun, 29 Sep 2024 03:20:06 +0300 Subject: [PATCH] moving drag and drop to new core (#1753) --- .../com/shaft/gui/element/ElementActions.java | 81 ++++++++++--------- .../shaft/gui/element/internal/Actions.java | 14 +++- .../appium/AndroidDragAndDropTest.java | 16 +++- 3 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/shaft/gui/element/ElementActions.java b/src/main/java/com/shaft/gui/element/ElementActions.java index 2f972004ac1..5cd043d779e 100755 --- a/src/main/java/com/shaft/gui/element/ElementActions.java +++ b/src/main/java/com/shaft/gui/element/ElementActions.java @@ -272,46 +272,47 @@ public ElementActions doubleClick(By elementLocator) { * @return a self-reference to be used to chain actions */ public ElementActions dragAndDrop(By sourceElementLocator, By destinationElementLocator) { - try { - Exception exception = new Exception(); - var elementName = elementActionsHelper.getElementName(driver, sourceElementLocator); - // replaced canFindUniqueElementForInternalUse, with countFoundElements for - // destinationElement to bypass the check for element visibility - // get source element start location - String startLocation = ((WebElement) elementActionsHelper.identifyUniqueElement(driver, sourceElementLocator).get(1)).getLocation().toString(); - // attempt to perform drag and drop - try { - elementActionsHelper.dragAndDropUsingJavascript(driver, sourceElementLocator, destinationElementLocator); - } catch (Exception rootCauseException) { - exception = rootCauseException; - ReportManagerHelper.logDiscrete(rootCauseException); - } - // get source element end location - String endLocation = ((WebElement) elementActionsHelper.identifyUniqueElement(driver, sourceElementLocator).get(1)).getLocation().toString(); - String reportMessage = "Start point: " + startLocation + ", End point: " + endLocation; - if (!endLocation.equals(startLocation)) { - elementActionsHelper.passAction(driver, sourceElementLocator, Thread.currentThread().getStackTrace()[1].getMethodName(), reportMessage, null, elementName); - } else { - try { - elementActionsHelper.dragAndDropUsingActions(driver, sourceElementLocator, destinationElementLocator); - } catch (Exception rootCauseException) { - if (!exception.equals(new Exception())) { - rootCauseException.addSuppressed(exception); - } - elementActionsHelper.failAction(driver, sourceElementLocator, rootCauseException); - } - // get source element end location - endLocation = ((WebElement) elementActionsHelper.identifyUniqueElement(driver, sourceElementLocator).get(1)).getLocation().toString(); - if (!endLocation.equals(startLocation)) { - elementActionsHelper.passAction(driver, sourceElementLocator, Thread.currentThread().getStackTrace()[1].getMethodName(), reportMessage, null, elementName); - } else { - elementActionsHelper.failAction(driver, reportMessage, sourceElementLocator); - } - } - } catch (Throwable throwable) { - // has to be throwable to catch assertion errors in case element was not found - elementActionsHelper.failAction(driver, sourceElementLocator, throwable); - } + new com.shaft.gui.element.internal.Actions(driverFactoryHelper).dragAndDrop(sourceElementLocator, destinationElementLocator); +// try { +// Exception exception = new Exception(); +// var elementName = elementActionsHelper.getElementName(driver, sourceElementLocator); +// // replaced canFindUniqueElementForInternalUse, with countFoundElements for +// // destinationElement to bypass the check for element visibility +// // get source element start location +// String startLocation = ((WebElement) elementActionsHelper.identifyUniqueElement(driver, sourceElementLocator).get(1)).getLocation().toString(); +// // attempt to perform drag and drop +// try { +// elementActionsHelper.dragAndDropUsingJavascript(driver, sourceElementLocator, destinationElementLocator); +// } catch (Exception rootCauseException) { +// exception = rootCauseException; +// ReportManagerHelper.logDiscrete(rootCauseException); +// } +// // get source element end location +// String endLocation = ((WebElement) elementActionsHelper.identifyUniqueElement(driver, sourceElementLocator).get(1)).getLocation().toString(); +// String reportMessage = "Start point: " + startLocation + ", End point: " + endLocation; +// if (!endLocation.equals(startLocation)) { +// elementActionsHelper.passAction(driver, sourceElementLocator, Thread.currentThread().getStackTrace()[1].getMethodName(), reportMessage, null, elementName); +// } else { +// try { +// elementActionsHelper.dragAndDropUsingActions(driver, sourceElementLocator, destinationElementLocator); +// } catch (Exception rootCauseException) { +// if (!exception.equals(new Exception())) { +// rootCauseException.addSuppressed(exception); +// } +// elementActionsHelper.failAction(driver, sourceElementLocator, rootCauseException); +// } +// // get source element end location +// endLocation = ((WebElement) elementActionsHelper.identifyUniqueElement(driver, sourceElementLocator).get(1)).getLocation().toString(); +// if (!endLocation.equals(startLocation)) { +// elementActionsHelper.passAction(driver, sourceElementLocator, Thread.currentThread().getStackTrace()[1].getMethodName(), reportMessage, null, elementName); +// } else { +// elementActionsHelper.failAction(driver, reportMessage, sourceElementLocator); +// } +// } +// } catch (Throwable throwable) { +// // has to be throwable to catch assertion errors in case element was not found +// elementActionsHelper.failAction(driver, sourceElementLocator, throwable); +// } return this; } diff --git a/src/main/java/com/shaft/gui/element/internal/Actions.java b/src/main/java/com/shaft/gui/element/internal/Actions.java index ee432cd1269..d914d208958 100644 --- a/src/main/java/com/shaft/gui/element/internal/Actions.java +++ b/src/main/java/com/shaft/gui/element/internal/Actions.java @@ -34,6 +34,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; +import java.time.Duration; import java.util.ArrayList; import java.util.Calendar; import java.util.List; @@ -74,7 +75,14 @@ public Actions type(@NonNull By locator, @NonNull CharSequence text) { return this; } - private enum ActionType{GET_NAME,CLICK,TYPE} + @Step("Drag and drop") + @Override + public Actions dragAndDrop(@NonNull By sourceElementLocator, @NonNull By destinationElementLocator) { + performAction(ActionType.DRAG_AND_DROP, sourceElementLocator, destinationElementLocator); + return this; + } + + private enum ActionType {GET_NAME, CLICK, TYPE, DRAG_AND_DROP} private String performAction(ActionType action, By locator, Object data) { AtomicReference output = new AtomicReference<>(""); @@ -164,6 +172,10 @@ private String performAction(ActionType action, By locator, Object data) { foundElements.get().getFirst().sendKeys((CharSequence) data); } case GET_NAME -> output.set(foundElements.get().getFirst().getAccessibleName()); + case DRAG_AND_DROP -> new org.openqa.selenium.interactions.Actions(driver) + .dragAndDrop(foundElements.get().getFirst(), + driver.findElement((By) data)) + .pause(Duration.ofMillis(300)).build().perform(); } // take screenshot if not already taken before action diff --git a/src/test/java/testPackage/appium/AndroidDragAndDropTest.java b/src/test/java/testPackage/appium/AndroidDragAndDropTest.java index 7ac67e2676a..3d3a0a7967d 100644 --- a/src/test/java/testPackage/appium/AndroidDragAndDropTest.java +++ b/src/test/java/testPackage/appium/AndroidDragAndDropTest.java @@ -11,6 +11,7 @@ public class AndroidDragAndDropTest { private static final ThreadLocal driver = new ThreadLocal<>(); + private static final By dragAndDropScreen = AppiumBy.accessibilityId("Drag-drop-screen"); @Test public void wizard_scrollInExpandableLists_verticalScrolling_insideScreen() { @@ -18,8 +19,6 @@ public void wizard_scrollInExpandableLists_verticalScrolling_insideScreen() { By draggableRobotEyes = By.xpath("//android.view.ViewGroup[@content-desc='drag-c1']/android.widget.ImageView"); By dropRobotEyes = By.xpath("//android.view.ViewGroup[@content-desc='drop-c1']/android.view.ViewGroup"); - By dragAndDropScreen = AppiumBy.accessibilityId("Drag-drop-screen"); - // WebDriver code -> really fails to drag // driver.get().element().click(dragButton) // .dragAndDrop(draggableRobotEyes, dropRobotEyes) @@ -31,6 +30,17 @@ public void wizard_scrollInExpandableLists_verticalScrolling_insideScreen() { .and().assertThat(dragAndDropScreen).matchesReferenceImage().perform(); } + @Test + public void multipleDragAndDrop() { + driver.get().touch().tap(AppiumBy.accessibilityId("Drag")); + driver.get().element() + .dragAndDrop(AppiumBy.accessibilityId("drag-l2"), AppiumBy.accessibilityId("drop-l2")) + .dragAndDrop(AppiumBy.accessibilityId("drag-r3"), AppiumBy.accessibilityId("drop-r3")) + .dragAndDrop(AppiumBy.accessibilityId("drag-c3"), AppiumBy.accessibilityId("drop-c3")) + .dragAndDrop(AppiumBy.accessibilityId("drag-r1"), AppiumBy.accessibilityId("drop-r1")) + .and().assertThat(dragAndDropScreen).matchesReferenceImage().perform(); + } + @SuppressWarnings("CommentedOutCode") @BeforeMethod public void setup() { @@ -65,7 +75,7 @@ public void setup() { // SHAFT.Properties.browserStack.set().deviceName("Google Pixel 7"); // SHAFT.Properties.browserStack.set().appName("Android-NativeDemoApp-0.4.0.apk"); // SHAFT.Properties.browserStack.set().appRelativeFilePath(""); -// SHAFT.Properties.browserStack.set().appUrl("bs://832ef13a11caa84ad714e0d4f8d9553cd3c5a3ca"); +// SHAFT.Properties.browserStack.set().appUrl("bs://ea453f6afaac69563817f4ae41311e2de98b260d"); driver.set(new SHAFT.GUI.WebDriver()); }