javascript 单击量角器中元素的给定坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28520623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:07:06  来源:igfitidea点击:

Clicking on given coordinates of element in protractor

javascriptprotractormousemoveangularjs-e2e

提问by Bowzer2

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

我想单击canvas元素的特定位置,因此我编写了以下量角器代码:

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

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

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

toRight/toBottom是应该进行点击的像素数,相对于我的画布的左上角。

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

但是,单击似乎并未在给定坐标处执行。我从有关软件质量保证和测试堆栈交换的相关问题中获得了片段。

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

你能确认这个片段有效吗?
你能提出替代方案吗?

回答by Olov

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

我完成了这项工作,将一个表示坐标的对象作为第二个参数传递给mouseMove

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

var clickCanvas = function (toRight, toBottom) { 
    browser.actions()
      .mouseMove(canvas, {x: toRight, y: toBottom})
      .click()
      .perform();
};

回答by Sirk

you missed out .perform()

你错过了 .perform()

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

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

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

我在测试中多次使用它并确认它有效

回答by alecxe

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

在这种情况下,您错过了perform()电话:

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

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

这是在 Protractor/WebDriverJS 中编写 e2e 测试时常见的错误之一。

To prevent these errors from happening, there is a eslint-plugin-protractorplugin to ESLintthat would warn youif perform()was no called on browser.actions()chain.

为了防止发生这些错误,有一个eslint-plugin-protractor插件,ESLint会警告你,如果perform()没有所谓的上browser.actions()链。