如何使用 Protractor/Selenium 将鼠标移动到任意点
How to move the mouse to an arbitrary point using Protractor/Selenium
在 Protractor 测试中是否可以将鼠标移动到 screen/relative 上的任意坐标到某个元素?我看到人们建议 Java 用户使用 Robot,但我当然不能在 JavaScript 中使用它。
我自己想出来了...只是深入研究了 Protractor 和 Selenium 文档。这是一些示例代码:
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();
});
我还找到了模拟滑动动作的解决方案。
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();
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);
});
在 Protractor 测试中是否可以将鼠标移动到 screen/relative 上的任意坐标到某个元素?我看到人们建议 Java 用户使用 Robot,但我当然不能在 JavaScript 中使用它。
我自己想出来了...只是深入研究了 Protractor 和 Selenium 文档。这是一些示例代码:
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();
});
我还找到了模拟滑动动作的解决方案。
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();
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);
});