Ad

Selenium/Java: Can I Separate A Drag And Drop Action In Two Actions Belonging To Two Separate Classes?

I'm aware this is kind of a weird thing to want to do, but right now I'm building a test project, and I separated the several areas/containers of the application I'm testing in different classes, as a measure to keep organization. Therefore, if I want to drag an element from area A to area B, I've got to separate my D&D action in 2 methods, each belonging to a different class. I tried doing that in two ways:

Way 1:

//Class A
public void dragThumbnail(int startPosition) {
    Actions act = new Actions($(By.cssSelector("body")).getWrappedDriver());
    act.clickAndHold(getThumbnailFromPosition(startPosition)).build().perform();
}
//Class B
public void dropInDivider(int endPosition) {
    Actions act = new Actions($("body").getWrappedDriver());
    act.moveToElement(getDividerFromPosition().get(endPosition)).release().perform();
}

Way 2:

//Class A:
public void dragThumbnail(int startPosition) throws AWTException {
    Robot robot = new Robot();
    int x = (getThumbnailFromPosition(startPosition)).getLocation().getX();
    int y = (getThumbnailFromPosition(startPosition)).getLocation().getY();
    robot.mouseMove(x,y);
    robot.mousePress(InputEvent.BUTTON1_MASK);
}
//Class B
public void dropInDivider(int endposition) throws AWTException {
    Robot robot = new Robot();
    int x = getDividerFromPosition().get(endposition-1).getLocation().getX();
    int y = getDividerFromPosition().get(endposition-1).getLocation().getY();
    robot.mouseMove(x, y);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
}

Evidently, none of them worked, because otherwise I wouldn't be here. I'm not too knowledgeable about Selenium Java yet, is anything wrong with my code, or is this just impossible/unfeasible to do, and I've just got to have everything in the same class?

I appreciate each and every input I might get.

Ad

Answer

I solved this issue as follows:

Step 1: Create a method to move the mouse cursor to points (x1, y1):

private void mouseMove(int destinationLocationX, int destinationLocationY) throws AWTException {
    Robot robot = new Robot();
    Point mousePosition;
    robot.mouseMove(destinationLocationX/2, destinationLocationY/2);
    mousePosition = MouseInfo.getPointerInfo().getLocation();
    while (mousePosition.x != destinationLocationX && mousePosition.y != destinationLocationY) {
        mousePosition = MouseInfo.getPointerInfo().getLocation();
        robot.mouseMove(destinationLocationX, destinationLocationY);
    }
}

Then, I created methods to find the exact center of each element:

private int getElementX(SelenideElement element) {
   return element.getLocation().x + element.getSize().getWidth()/2; }

private int getElementY(SelenideElement element) {
    return element.getLocation().y + element.getSize().getHeight()/2; }

Then, I created a generic drag and drop method, that I then used in several different other methods:

private void dndToCenterOfObject(SelenideElement source, SelenideElement target)
        throws AWTException {
    Robot robot = new Robot();
    robot.setAutoDelay(150);
    int sourceX = getElementX(source);
    int sourceY = getElementY(source);
    int targetX = getElementX(target);
    int targetY = getElementY(target);
    mouseMove(sourceX, sourceY);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    mouseMove(targetX, targetY);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
}

Hope this helps.

Ad
source: stackoverflow.com
Ad