gpt4 book ai didi

html - Protractor 拖放 : Angular vs. Angular with HTML5

转载 作者:可可西里 更新时间:2023-11-01 13:48:16 25 4
gpt4 key购买 nike

我在我的 Protractor 测试中遇到了拖放问题(就像其他一些人一样),但一般情况下不会出现问题 - 仅在使用 HTML5 的角度应用程序中。

为了演示,我使用两个演示拖放功能的网页编写了一个小 Protractor 测试套件。第一个页面(在第一个测试用例中使用)演示了 AngualarJS 中 jQueryUI 拖放功能的实现。这个工作得很好。第二页(在第二个测试用例中使用)使用带有 HTML5 的 Angular 拖放。这个在我的测试中不起作用。

我的下一个尝试是使用这个帖子也给我带来的辅助函数:https://gist.github.com/druska/624501b7209a74040175不幸的是,正如我的第三次测试显示的那样,这对我来说更晚了

谁能告诉我为什么拖放在 HTML 5 中不起作用,我必须做些什么才能让它运行?

提前致谢

阿基

我的系统:

  • Protractor 4.0.0
  • selenium-server-standalone 2.53.1
  • chromedriver 2.22
  • iedriver 2.53.1
  • 壁虎驱动程序 0.9.0

我的测试套件:

describe('Protractor drag-and-drop test', function() {

afterEach(function(){
browser.sleep(5000);
});

it('1st test - jQueryUI drag and drop for AngularJS', function() {
//found here: http://stackoverflow.com/questions/24315571/drag-drop-with-protractor-by-repeater
browser.get('http://codef0rmer.github.io/angular-dragdrop/#!/');
var elem = element(by.css('.ui-draggable'));
var target = element(by.css('.thumbnail'));
browser.sleep(3000);

elem.click();
browser.actions().dragAndDrop(elem, target).perform();
});

it('2nd test - Angular drag & drop with HTML5', function() {
browser.get('http://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/simple');

var elem =element.all(by.xpath("/html/body/div[2]/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[2]/ul/li[1]")).first();
var target = $('ul[dnd-list=list]');

expect(elem.getText()).toEqual("Item B1"); //Item that should be dragged and dropped
expect(target.getText()).toContain("Item A1"); //element sorrounding "ItemA1", "Item A2", "ItemA3"

elem.click();
browser.actions().dragAndDrop(elem, target).perform();
});

it('3rd test - Angular drag & drop with HTML5 with native_js_drag_and_drop_helper', function() {
browser.get('http://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/simple');
var dragAndDropFn = require('./native_js_drag_and_drop_helper.js');

var elem =element.all(by.xpath("/html/body/div[2]/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[2]/ul/li[1]")).first();
var target = $('ul[dnd-list=list]');

expect(elem.getText()).toEqual("Item B1"); //Item that should be dragged and dropped
expect(target.getText()).toContain("Item A1"); //element sorrounding "ItemA1", "Item A2", "ItemA3"

elem.click();
browser.executeScript(dragAndDropFn, target.getWebElement(), elem.getWebElement());
});

xit(' 4th test - Test of native drag and drop helper ', function() {
// test found on https://gist.github.com/druska/624501b7209a74040175 failing with "Angular could not be found on the page http://html5demos.com/drag : retries looking for angular exceeded"
var dragAndDropFn = require('./native_js_drag_and_drop_helper.js');
browser.get("http://html5demos.com/drag");
var field = element.all(by.className('drag-handle')).get(0);
var src = element.all(by.className('box-list-compact-hover')).get(0);
browser.executeScript(dragAndDropFn, field.getWebElement(), src.getWebElement());
}, 120000);
});

我的配置文件:

exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js'],
capabilities: {
//browserName: 'internet explorer'
browserName: 'chrome'
//browserName: 'firefox'
},
};

native_js_drag_and_drop_helper:

module.exports = function simulateDragDrop(sourceNode, destinationNode) {
var EVENT_TYPES = {
DRAG_END: 'dragend',
DRAG_START: 'dragstart',
DROP: 'drop'
}

function createCustomEvent(type) {
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)
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)
}

最佳答案

我遇到了同样的问题,各种 dnd 模拟库似乎不适用于 angular-drag-and-drop-lists。

最后我 fork 了html-dnd简单地添加一个 dragover 事件,angular-drag-and-drop-lists 需要一些东西,因为它计算通过 dragover 事件放置的元素的索引。它还粘在一个临时 li 元素中,代码在删除它之前将其用作实际放置点。这就是用户在屏幕上看到的移动列表点。

fork 在 forked html-dnd .我通过 git pull 将其包含在我的 package.json 文件中的依赖项中

"html-dnd": "git://github.com/PloughingAByteField/html-dnd.git"

用于 Protractor

// at the top of the spec
var dragAndDrop = require('html-dnd').code;

<snip>

it('should dragover and drop', function() {
var draggable = browser.findElement(by.id('id1'));
var droppable = browser.findElement(by.id('id2'));
browser.driver.executeScript(dragAndDrop, draggable, droppable);
);

更新:html-dnd 项目已合并到 dragover 事件中,因此您可以使用它来代替我未维护的分支。

关于html - Protractor 拖放 : Angular vs. Angular with HTML5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38916746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com