【发布时间】:2017-11-25 19:27:09
【问题描述】:
我正在尝试使用 Protractor Jasmine 在我们的 Angular 应用程序上执行拖放操作。我能够掌握源项目,但随着测试的运行,源元素被选中,但此后没有任何反应;拖放操作不会发生。控制台中没有显示错误。
关于目标容器的一个有趣的事情是,放在这里的项目可以根据用户的意愿调整大小。此外,目标容器中没有明确标记的位置/区域,拖动的项目将被丢弃!但是容器确实有一个 ID;虽然这在这里仍然没有帮助。
代码如下:
let dragAndDrop = require('html-dnd').code;
.
.
.
function dragAndDropListItems(fdIndex: number): void {
let dragElement = element.all(by.repeater('listDefinition in lists')).get(fdIndex); // Select the first repeater corresponding to the first List Item in the list
let dragElementh5 = dragElement.all(by.css('a')).get(0); // Select the first List Item
let printFD = dragElementh5.getText().then((text: string) => {
console.log(text); // Print the innerHTML text from the chosen List Item to the Console
});
let finalDrop = element.all(by.css('[id="dashboardContainerDiv"]')).get(0);
dragElement.click();
browser.actions().dragAndDrop(dragElement, finalDrop).perform();
};
我也尝试过使用基于坐标的 DragNDrop 操作,但在每种情况下都一样。
其他尝试过的选项包括:
//browser.executeScript(dragAndDrop, dragElement, finalDrop); // Perform the drag and drop operation
//browser.driver.actions().dragAndDrop(dragElement, finalDrop).perform();
//browser.actions().dragAndDrop(dragElement, { x: 400, y: 400 }).perform();
// browser.driver.actions().mouseDown(dragElement).mouseMove(finalDrop).mouseUp(finalDrop).perform();
请提出解决此问题的方法。
@FlorentB。我已将代码与您的脚本一起导入。
让 JS_DRAG_DROP = require('./drag-drop.js');
function dragAndDropListItems(fdIndex: number): void {
/*
let source = driver.find_element_by_css_selector("#drag")
target = driver.find_element_by_css_selector("#drop")
driver.execute_async_script(JS_DRAG_DROP, source, target)
# drag and drop an element by offset {x:500, y:200}
source = driver.find_element_by_css_selector("#drag")
driver.execute_async_script(JS_DRAG_DROP, source, None, 500, 200)
# drag and drop an element with a delay of 101ms before the drop
source = driver.find_element_by_css_selector("#drag")
target = driver.find_element_by_css_selector("#drop")
driver.execute_async_script(JS_DRAG_DROP, source, target, 0, 0, 101)
*/
let source = element.all(by.repeater('listDefinition in
lists')).get(fdIndex); // Select the first repeater corresponding to the
first List Item in the list
let dragElementh5 = source.all(by.css('a')).get(0); // Select the first List
Item
let printFD = dragElementh5.getText().then((text: string) => {
console.log(text); // Print the innerHTML text from the chosen List Item
to the Console
});
//browser.driver.switchTo().frame('dashboardContainerDiv');
/*
let finalDropClass = element.all(by.css('[class="dashboard mb10"]')).get(0);
let finalDropCon =
finalDropClass.all(by.css('[id="dashboardContainerDiv"]')).get(0);
let finalDrop =
finalDropCon.all(by.css('[id="dashboardContainerUl"]')).get(0);
*/
let target = element.all(by.css('[id="dashboardContainerDiv"]')).get(0);
//dragElement.click();
browser.executeScript(JS_DRAG_DROP, source, target); // Perform the drag and
drop operation
//browser.actions().dragAndDrop(dragElement, finalDrop).perform();
//browser.driver.actions().dragAndDrop(dragElement, finalDrop).perform();
//browser.actions().dragAndDrop(dragElement, { x: 400, y: 400 }).perform();
// browser.driver.actions().mouseDown(dragElement).mouseMove(finalDrop).mouseUp(finalDrop).perform();
};
【问题讨论】:
-
@Thomas Rollet 感谢您的编辑
-
该页面实现了 Selenium 不支持的 HTML5 拖放。您必须注入一个脚本来模拟这些事件。见gist.github.com/florentbr/60ef7cb8d9b1ae690cafc82aad52da73
-
@FlorentB。谢谢您的建议。您是指这里的“旁边”标签吗?我已经删除了'aside'标签并尝试了dragNdrop(没有添加你建议的脚本)。它不起作用。
-
@FlorentB。我现在还使用您的脚本尝试了 dragNdrop 操作。我收到错误消息:脚本中定义的“源元素不可拖动”。我可以在这里做什么?
标签: angularjs google-chrome typescript drag-and-drop protractor