Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate a drag and drop action in protractor?

I have a double slider and I'd like to test that it's operable and return's the right data. The slider has a min and a max handler, it also has some "breakpoints that I can hook to. "

What I want to simulate is

  • a touchStart of the ".handler-max" element
  • a move of the thumb over the element with class ".step-3"
  • a touchEnd of the ".handler-max" element

while I found how to trigger a touchStart and a touchEnd event. I'm clueless on how to simulate the move of the thumb

browser.executeScript('angular.element(arguments[0]).triggerHandler("touchstart");', filterHandler);
// <--- move event????
browser.executeScript('angular.element(arguments[0]).triggerHandler("touchend");', filterHandler);

P.S. The scope of this question is an integration test that tests if what happens to the application when a user interact's with a double slider directive is the desirable result.

like image 675
Alexandros Spyropoulos Avatar asked Sep 04 '14 11:09

Alexandros Spyropoulos


People also ask

How do you drag and drop elements in protractor?

The dragAndDrop () action of the mouse event drags the source element to the target element via mouse actions in Selenium Protractor.

Is protractor going to be deprecated?

Protractor Will Be Deprecated Some of the reasons behind these deprecation plans are: State of Protractor. Protractor is dependent on selenium-webdriver , and is not able to upgrade to the new version without introducing a huge breaking change, and forcing users to do a migration for all their tests.


3 Answers

This is quite straightforward nowadays:

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

Where dragAndDrop behind the scenes is mouseDown + mouseMove + mouseUp:

/**
 * Convenience function for performing a "drag and drop" manuever. The target
 * element may be moved to the location of another element, or by an offset (in
 * pixels).
 * @param {!webdriver.WebElement} element The element to drag.
 * @param {(!webdriver.WebElement|{x: number, y: number})} location The
 *     location to drag to, either as another WebElement or an offset in pixels.
 * @return {!webdriver.ActionSequence} A self reference.
 */
webdriver.ActionSequence.prototype.dragAndDrop = function(element, location) {
  return this.mouseDown(element).mouseMove(location).mouseUp();
};
like image 169
alecxe Avatar answered Nov 03 '22 00:11

alecxe


elem = Element you want to move;

target = Element where you want to drop elem;

For WebdriverJS:-

browser.driver.actions().dragAndDrop(elem,target).mouseUp().perform();

For Protractor:-

browser.actions().dragAndDrop(elem,target).mouseUp().perform();
like image 36
user3800138 Avatar answered Nov 03 '22 00:11

user3800138


        var plot0 = ptor.element(protractor.By.id(''));

        ptor.driver.actions()

        .dragAndDrop(plot0, {x: 200, y: 100})

        .mouseMove(plot0, {x: 200, y: 100}) // 200px from left, 200 px from top of plot0

        .mouseDown()

        .mouseMove({x: 600, y: 0}) // 600px to the right of current location

        .perform();

This works for me guys. My scenario is drag a pop up dialog box which does not have a target element.

like image 34
randomguy Avatar answered Nov 03 '22 00:11

randomguy