Clicking on given coordinates of element in protractor
Asked Answered
M

3

12

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?

Motherland answered 14/2, 2015 at 21:39 Comment(2)
Have you looked into browser.executeScript() at all?Libbielibbna
You need to .perform() the action.Alvord
L
22

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();
};
Lecialecithin answered 20/5, 2015 at 13:4 Comment(2)
@Lecialecithin as a consolation you get an up-vote from me ;-) does this work only with canvas elements or with any element?Talithatalk
@Talithatalk Well, it should work with any ElementFinder (look it up in the Protractor API reference if you haven't heared it before), thus should any element work fine.Lecialecithin
G
3

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

Grote answered 20/2, 2015 at 11:37 Comment(0)
V
3

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.

Variorum answered 12/4, 2016 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.