在AngularJS中使用量角器进行拖放

时间:2017-12-01 12:33:14

标签: angularjs drag-and-drop protractor

我很欣赏作为E2E测试的一部分,已经有很多关于自动拖放的内容。然而,经过多次,数小时的摆弄,我无法获得任何描述的方法......即使用函数,坐标等等。奇怪的是,console.log维持测试已经通过,但屏幕截图清楚地显示没有已经发生了。 Screenshots shows a portion of the application

用户选择纸张并拖动到图像上。当拖动“开始”时,图像上的灰色叠加层将清除,纸张将在房间中呈现。

代码片段显示了我尝试过的一个更简单的想法,我很乐意接受任何帮助!

const JS_HTML5_DND = 'function e(e,t,n,i){var r=a.createEvent("DragEvent");r.initMouseEvent(t,!0,!0,o,0,0,0,c,g,!1,!1,!1,!1,0,null),Object.defineProperty(r,"dataTransfer",{get:function(){return d}}),e.dispatchEvent(r),o.setTimeout(i,n)}var t=arguments[0],n=arguments[1],i=arguments[2]||0,r=arguments[3]||0;if(!t.draggable)throw new Error("Source element is not draggable.");var a=t.ownerDocument,o=a.defaultView,l=t.getBoundingClientRect(),u=n?n.getBoundingClientRect():l,c=l.left+(l.width>>1),g=l.top+(l.height>>1),s=u.left+(u.width>>1)+i,f=u.top+(u.height>>1)+r,d=Object.create(Object.prototype,{_items:{value:{}},effectAllowed:{value:"all",writable:!0},dropEffect:{value:"move",writable:!0},files:{get:function(){return this._items.Files}},types:{get:function(){return Object.keys(this._items)}},setData:{value:function(e,t){this._items[e]=t}},getData:{value:function(e){return this._items[e]}},clearData:{value:function(e){delete this._items[e]}},setDragImage:{value:function(e){}}});if(n=a.elementFromPoint(s,f),!n)throw new Error("The target element is not interactable and need to be scrolled into the view.");u=n.getBoundingClientRect(),e(t,"dragstart",101,function(){var i=n.getBoundingClientRect();c=i.left+s-u.left,g=i.top+f-u.top,e(n,"dragenter",1,function(){e(n,"dragover",101,function(){n=a.elementFromPoint(c,g),e(n,"drop",1,function(){e(t,"dragend",1,callback)})})})})';


describe('Drag and Drop Test', function() {
it('should drag', function () {



    var e1 = element(by.xpath('html/body/webapp-app/div/div/webapp-johnlewis-visualiser/div/div[2]/div/digitalbridge-shortlist/div/div/ul/li[1]/a/img'));
    var e2 = element(by.css('.db-project-designer'));

    element(by.xpath('html/body/webapp-app/div/div/webapp-johnlewis-visualiser/div/div[2]/div/digitalbridge-shortlist/div/div/ul/li[1]/a/img')).click();
    //element(by.xpath('html/body/webapp-app/div/div/webapp-johnlewis-visualiser/div/div[1]/div/div/digitalbridge-project/div/digitalbridge-project-designer/canvas')).click();

    browser.driver.actions().dragAndDrop(e1.getWebElement(),e2.getWebElement()).perform();

    browser.sleep(2000);
});
});

常量显示错误'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz). (W104) - 我在Node_Modules中安装了ES6。

我插入了click行,看看预先选择的项目是否有所不同......它没有!

谢谢

大卫

2 个答案:

答案 0 :(得分:0)

试试这个图书馆https://github.com/SunGard-Labs/sg-protractor-tools

该库还包括简化常见任务的功能,如

滚动到元素 拖放 等待DOM元素变得可见或隐藏

答案 1 :(得分:0)

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

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

    const event = createCustomEvent(EVENT_TYPES.DRAG_START);
    dispatchEvent(sourceNode, EVENT_TYPES.DRAG_START, event);

    const dropEvent = createCustomEvent(EVENT_TYPES.DROP);
    dropEvent.dataTransfer = event.dataTransfer;
    dispatchEvent(destinationNode, EVENT_TYPES.DROP, dropEvent);

    const dragEndEvent = createCustomEvent(EVENT_TYPES.DRAG_END);
    dragEndEvent.dataTransfer = event.dataTransfer;
    dispatchEvent(sourceNode, EVENT_TYPES.DRAG_END, dragEndEvent);
}

您可以通过此代码

来调用它
browser.executeScript(dragAndDrop, element, targetArea);