用HTML5实现Selenium Java拖拽功能

问题描述 投票:2回答:1

在这里的几个帖子中,有一个针对使用 HTML5 拖放的页面的 selenium 拖放的变通方法。 这个变通方法涉及使用 javascript 模拟拖放,例如 无法在Selenium WebDriver测试中使用javascript执行HTML5拖放。https:/gist.github.comrcorreia2362544。. 这个解决方案在这个页面上很好用。http:/the-internet.herokuapp.comdrag_and_drop。.

一般的方法是阅读这里的javascript文件(https:/gist.github.comrcorreia2362544#file-drag_and_drop_helper-js。)变成一个字符串,下面称为'jsfile'。

然后在Selenium中(使用java),传递源和目标的css选择器,其中#column-a是源的id,#column-b是目标。

 ((JavascriptExecutor) driver).executeScript(jsfile +"$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});");

在那个页面上,它的工作就像一个冠军。

然而,类似的方法在这个页面上似乎不起作用。https:/crossbrowsertesting.github.iodrag-and-drop.html。. 当我运行时,什么都没有发生

 ((JavascriptExecutor) driver).executeScript(jsfile +"$('#draggable').simulateDragDrop({ dropTarget: '#droppable'});");

我有一些页面的行为似乎与第二页类似(例如,没有拖放)。 作为理解这一点的第一步,我想知道为什么这种方法在后一种情况下似乎不起作用。

java html selenium automation
1个回答
0
投票

关于重新测试 https:/crossbrowsertesting.github.iodrag-and-drop.html。看起来,直接使用 Actions 类就能实现拖放功能。 在我测试的特定应用程序中,我使用了一些额外的代码来帮助实现无障碍操作,通过将焦点设置在第一个元素上并按下回车键,然后将焦点设置在目标元素上并再次按下回车键,就可以实现拖放。我很确定这是自定义的事件处理,所以在其他应用程序中可能无法使用。 为了以防万一,我在这里发布了在selenium中这样做的代码。

 public void dndHtml5(String xPathSource, String xPathDestination) {
    clickEnterKeyOnElement(xPathSource);
    clickEnterKeyOnElement(xPathDestination);
}

public void clickEnterKeyOnElement(String xPath) {
    setFocusOnElement(xPath);
    WebElement target=element(xPath);
    target.sendKeys(Keys.ENTER);
}

public void setFocusOnElement(String xPath) {
    WebElement element = element(xPath);
    Actions actions = new Actions(driver);
    actions.moveToElement(element).build().perform();
}

public WebElement element(String xPath){
    return driver.findElementByXPath(xPath);
}
© www.soinside.com 2019 - 2024. All rights reserved.