Я испробовал различные варианты, но при запуске теста я вижу, что элемент выбран, но он не перемещается в указанную точку отбрасывания, и удержание остается в l oop, элемент не освобождается, пока я не переместлю мышь. URL сайта: http://the-internet.herokuapp.com/drag_and_drop См. Изображение: https://user-images.githubusercontent.com/60186596/72910855-bd174500-3cfe-11ea-8404-3449af5bca8c.png
Вот как я определил функцию в моей последней попытке:
public boolean waitAndDragAndDrop(WebElement from, WebElement to)
{
try {
if(waitForElementVisible(from) && waitForElementVisible(to)) {
Actions act = new Actions(driver);
act.dragAndDrop(from, to).build().perform();
return true;
}
else
return false;
}catch (Exception e) {
System.out.print("Error in the \"class: BasePage\" typeOnElement event:\n" + e);
return false;
}
}
Также попытался выполнить следующее (тот же результат), слегка переместив элемент, а затем выполнив операцию перетаскивания (варианты этой темы также не удалось).
public boolean waitAndDragAndDrop(WebElement from, WebElement to) {
Actions act = new Actions(driver);
act.dragAndDrop(from, to).build().perform();
act.clickAndHold(from).perform();
act.pause(Duration.ofSeconds(1));
act.moveByOffset(10,0).perform();
act.moveToElement(to).perform();
act.moveByOffset(10,0).perform();
act.release();
act.build();
act.perform();
return true;
}
Actions act = new Actions(driver);
act.clickAndHold(from)
.moveByOffset(-10, 0)
.moveToElement(to)
.release()
.perform();
Элементы идентифицируются с помощью идентификатора:
@FindBy(id = "column-a")
private WebElement box_A;
@FindBy(id = "column-b")
private WebElement box_B;
См. Изображение: https://user-images.githubusercontent.com/60186596/72915822-8ba27780-3d06-11ea-93d3-f2603f40b904.png
Я также попытался немного переместить элемент, а затем выполнить операцию перетаскивания (варианты этой проблемы также были проверены не удалось) , Я пытался использовать класс Actions для перемещения элемента, элемент выделен, но он не перемещается; Я также заметил, что при использовании метода «moveToElement» класса Actions мышь не перемещается к элементу, указанному параметром.
new Actions(driver)
.moveToElement(from).perform();
Весь код
new Actions(driver)
.moveToElement(from) //Moves the mouse to an offset from the top-left corner of the element.
.pause(Duration.ofSeconds(1))
.clickAndHold(from) //Clicks (without releasing) in the middle of the given element.
.pause(Duration.ofSeconds(1))
.moveByOffset(1, 0) //Moves the mouse from its current position (or 0,0) by the given offset.
.moveToElement(to)
.moveByOffset(1, 0)
.pause(Duration.ofSeconds(1))
.release().build(); //Releases the depressed left mouse button at the current mouse location.
Затем я подумал об использовании класса Robot для наведения мыши на элемент; но при использовании метода "clickAndHold" он остается в l oop.
Actions act = new Actions(driver);
Robot robot = new Robot();
int xFrom = from.getLocation().x;
int yFrom = from.getLocation().y;
int xTo = to.getLocation().x;
int yTo = to.getLocation().y;
robot.mouseMove(xFrom + 50, yFrom + 150);//Move the mouse over the element
act.clickAndHold(from).moveByOffset(xFrom + 50, yFrom + 150).pause(Duration.ofSeconds(1)).perform(); // :v Stays in loop
act.moveToElement(to).perform(); //Method for performing the actions without calling build() first.
robot.mouseMove(xTo + 50, yTo + 150); //Move the mouse to 'To' element
act.release(to).build(); //RELEASE -> Releases the depressed left mouse button, in the middle of the given element. BUILD -> Generates a composite action containing all actions so far
Заранее спасибо.