Javascript 如何使用量角器/硒将鼠标移动到任意点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28705613/
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-08-23 02:16:53  来源:igfitidea点击:

How to move the mouse to an arbitrary point using Protractor/Selenium

javascriptseleniumprotractorpointmousemove

提问by Andy

Is it possible to move the mouse to arbitrary coordinates on the screen/relative to an element in Protractor tests? I see people recommend using Robot for Java users, but of course I can't use that in JavaScript.

是否可以将鼠标移动到屏幕上的任意坐标/相对于量角器测试中的元素?我看到有人建议 Java 用户使用 Robot,但我当然不能在 JavaScript 中使用它。

回答by Andy

I figured it out for myself...just took some deep digging through Protractor and Selenium documentation. Here is some example code:

我自己想通了……只是深入研究了量角器和硒文档。下面是一些示例代码:

  it('should pan plots when you click and drag', function() {
    var plot0 = element(by.css('.plot > canvas'));
    browser.actions()
      .mouseMove(plot0, {x: 100, y: 100}) // 100px from left, 100 px from top of plot0
      .mouseDown()
      .mouseMove({x: 400, y: 0}) // 400px to the right of current location
      .perform();
  });

回答by PurpleOrange

I also found a solution for simulating a swipe action.

我还找到了一个模拟滑动动作的解决方案。

var card = element(by.css('#card'));

browser.actions()
  .mouseMove(card, {x: 100, y: 100})
  .mouseDown()
  .mouseMove({x: 0, y: -400})
  .perform();

browser.sleep(500);

browser.actions()
  .mouseUp()
  .perform();

回答by kkashyap1707

var graphDimensions = {// see [1]
  Width: 0,
  Height: 0
};
company_page.company_Chart_Graph().getAttribute('width')
  .then(function(width) {
    graphDimensions.Width = parseInt(width);
  });

company_page.company_Chart_Graph().getAttribute('height').then(function(height) {
  graphDimensions.Height = parseInt(height);

  console.log('W' + graphDimensions.Width);
  console.log('H' + graphDimensions.Height);

  var plot0 = company_page.company_Chart_Graph();
  browser.actions()
    .mouseMove(plot0, {
      x: 0,
      y: 0
    })
    .mouseDown()
    .mouseMove(plot0, {
      x: graphDimensions.Width,
      y: graphDimensions.Height
    })
    .mouseUp()
    .perform();

  browser.driver.sleep(23000);
  company_page.company_ReportDownload().click();
  browser.driver.sleep(23000);
});

[1]: http://testingalert.com/

[1]:http: //testingalert.com/