Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking on given coordinates of element in protractor

I want to click on a specific location of my canvas element, so I wrote the following Protractor code:

var canvas = element(by.id("canvas"));

var clickCanvas = function(toRight, toBottom) { 
  browser.actions()
    .mouseMove(canvas, -toRight, -toBottom)
    .click();
}

toRight/toBottom are the numbers of pixels where the click should be made, relative the top left corner of my canvas.

However, the click does not seem to be executed at the given coordinates. I got the snippet from a related question on the Software Quality Assurance & Testing stack exchange.

Can you confirm that this snippet works?
Can you suggest alternatives?

like image 626
Bowzer2 Avatar asked Feb 14 '15 21:02

Bowzer2


3 Answers

I made this work, passing an object representing the coordinate as the second argument of mouseMove:

var canvas = element(by.id("canvas"));

var clickCanvas = function (toRight, toBottom) { 
    browser.actions()
      .mouseMove(canvas, {x: toRight, y: toBottom})
      .click()
      .perform();
};
like image 120
Olov Avatar answered Nov 13 '22 11:11

Olov


you missed out .perform()

browser.actions().mouseMove(canvas, -toRight, -toBottom).click().perform();

I use this a few times in my tests and confirm this works

like image 32
Sirk Avatar answered Nov 13 '22 13:11

Sirk


In this case, you have missed the perform() call:

 browser.actions()
  .mouseMove(canvas, -toRight, -toBottom)
  .click();  // < no .perform() HERE

This is one of the common mistakes when writing e2e tests in Protractor/WebDriverJS.

To prevent these errors from happening, there is a eslint-plugin-protractor plugin to ESLint that would warn you if perform() was no called on browser.actions() chain.

like image 3
alecxe Avatar answered Nov 13 '22 12:11

alecxe