Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drag N Drop Java Example - no working example #27

Open
timrsfo opened this issue May 23, 2018 · 4 comments
Open

Drag N Drop Java Example - no working example #27

timrsfo opened this issue May 23, 2018 · 4 comments

Comments

@timrsfo
Copy link

timrsfo commented May 23, 2018

I can't get Drag And Drop Java working. I've searched for hours and only find errors without solutions for MoveToElement and DragAndDrop and/or clickAndHold-MoveToElement-Release.

The following are some snippets of what I've tried:

`
public static final By COLUMN_A = By.id("column-a");
public static final By COLUMN_B = By.id("column-b");

@Test
public void dragAtoBTest() {
    Actions action = new Actions(driver);
    log.info("drag column-a to column-b position");
    WebElement weA = driver.findElement(DragNDropPage.COLUMN_A);
    WebElement weB = driver.findElement(DragNDropPage.COLUMN_B);
    action.dragAndDrop(weA, weB).build().perform();

    log.info("wait for opacity to change back to 1 from 0.4");
    wait.until(ExpectedConditions.attributeToBe(weA, "opacity", "1"));
    assertThat(weA.findElement(page.columnHeader).getText(),  containsString("B"));
}

@Test
public void clickHoldMoveReleaseAtoBTest() {
    Actions action = new Actions(driver);
    log.info("click and hold column-a");
    
    WebElement weA = driver.findElement(DragNDropPage.COLUMN_A);
    WebElement weB = driver.findElement(DragNDropPage.COLUMN_B);
    
    action.clickAndHold(weA)
    .moveByOffset(-1, -1) // To fix issue with drag and drop in Chrome V61.0.3163.79
    .moveToElement(weB, 
            weB.getLocation().getX()+weB.getSize().getWidth()/2, 
            weB.getLocation().getY()+weB.getSize().getHeight()/2)
    .release(weB)
    .build()
    .perform();
    
    
    
//       action.clickAndHold(weA).build().perform();
//        log.info("moveToElement column-b");
//        action.moveToElement(weB).build().perform();

   //        log.info("release on column-b");
   //        action.release().build().perform();
    
    
    
    log.info("wait for opacity to change back to 1 from 0.4");
    wait.until(ExpectedConditions.attributeToBe(weA, "opacity", "1"));
    assertThat(weA.findElement(page.columnHeader).getText(),  containsString("B"));`
@sturman
Copy link
Contributor

sturman commented Jun 3, 2018

Here are the reason and workaround why DnD does not work SeleniumHQ/selenium#1365
Workaround for Ruby is here http://elementalselenium.com/tips/39-drag-and-drop

@timrsfo
Copy link
Author

timrsfo commented Jun 4, 2018

@RomanIsko Thanks for the input, I figured out a java implementation using the dnd.js and your C# code a few weeks back:

/**
 * Solution from elemental-selenium-tips
 * https://github.com/tourdedave/elemental-selenium-tips/blob/master/39-drag-and-drop/csharp/DragAndDrop.cs
 * JSDriver.ExecuteScript(dnd_javascript + "$('#column-a').simulateDragDrop({
 * dropTarget: '#column-b'});");
 * 
 * NOTE: Seems fragile, not sure if this works for XPATH or other src/dst type strings
 * TODO: would be good if it worked with WebElement, or BY
 * 
 * @param driver
 * @param src
 *            - css string for source element
 * @param dst
 *            - css string for destination element
 */
public void dragDrop(WebDriver driver, String src, String dst) {
    String js = String.format("$('%s').simulateDragDrop({ dropTarget: '%s'});",src,dst);
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    jse.executeScript(dragndrop_js + js);
}

see: Drag and Drop in selenium java

@vikramvi
Copy link

@timrsfo you can refer to Java solution vikramvi/Selenium-Java@a1354ca, this is needed for HTML5 drag and drop which can't be done with standard selenium api.

@timrsfo
Copy link
Author

timrsfo commented Jul 20, 2018

@vikramv, thanks for the update. Did you see my solution as shown by the link above Drag and Drop in selenium java?

I took a look at your code, I see you are also using rcorreia/drag_and_drop_helper.js .
I also pushed my solution to my github se-drag-n-drop java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants