Skip to content

Commit

Permalink
moving drag and drop to new core (#1753)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohabMohie authored Sep 29, 2024
1 parent ea06f3c commit c079e25
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 44 deletions.
81 changes: 41 additions & 40 deletions src/main/java/com/shaft/gui/element/ElementActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/shaft/gui/element/internal/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> output = new AtomicReference<>("");
Expand Down Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions src/test/java/testPackage/appium/AndroidDragAndDropTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

public class AndroidDragAndDropTest {
private static final ThreadLocal<SHAFT.GUI.WebDriver> driver = new ThreadLocal<>();
private static final By dragAndDropScreen = AppiumBy.accessibilityId("Drag-drop-screen");

@Test
public void wizard_scrollInExpandableLists_verticalScrolling_insideScreen() {
By dragButton = By.xpath("//android.widget.Button[@content-desc='Drag']");
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)
Expand All @@ -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() {
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit c079e25

Please sign in to comment.