我需要用鼠标移动元素,但由于原生的browser.action()不起作用,我尝试使用以下方法:
module.exports = function simulateDragDrop(sourceNode, destinationNode) {
console.log('simulatingDragDrop');
var EVENT_TYPES = {DRAG_END: 'dragend', DRAG_START: 'dragstart', DROP: 'drop'}
function createCustomEvent(type) {
console.log('createCustomEvent');
var event = new CustomEvent("CustomEvent")
event.initCustomEvent(type, true, true, null)
event.dataTransfer = {
data: {}, setData: function (type, val) {
this.data[type] = val
}, getData: function (type) {
return this.data[type]
}
}
return event
}
function dispatchEvent(node, type, event) {
if (node.dispatchEvent) {
return node.dispatchEvent(event)
}
if (node.fireEvent) {
return node.fireEvent("on" + type, event)
}
}
var event = createCustomEvent(EVENT_TYPES.DRAG_START)
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_START, event)
var dropEvent = createCustomEvent(EVENT_TYPES.DROP)
console.log(event.dataTransfer);
console.log(dropEvent.dataTransfer);
dropEvent.dataTransfer = event.dataTransfer
dispatchEvent(destinationNode, EVENT_TYPES.DROP, dropEvent)
var dragEndEvent = createCustomEvent(EVENT_TYPES.DRAG_END)
dragEndEvent.dataTransfer = event.dataTransfer
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_END, dragEndEvent)
然后在测试中我有:
var dragAndDropFn = require('../../../services/native_js_drag_and_drop_helper');
let elementA = element(by.automationId('ElementA'));
let elementB = element(by.automationId('ElementB'));
browser.executeScript(dragAndDropFn, elementA.getWebElement(), elementB.getWebElement());
它适用于简单的html5测试页面,但是当我在我的应用程序中使用它时它什么都不做:
我的HTML是:
<div _ngcontent-kxy-11="" class="handle" draggable="true" automationid="ElementA">
<i _ngcontent-kxy-11="" class="icon-draghandle"></i>
与ElementB相同。我必须注意到找到了元素,这不是问题所在。你有什么想法吗?