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.
Asked
Active
Viewed 2.5k times
22
-
4If you downvote this will you please explain why? – Andy Feb 24 '15 at 20:35
3 Answers
39
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();
});

Andy
- 7,885
- 5
- 55
- 61
4
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();

PurpleOrange
- 86
- 3
0
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);
});

mkaran
- 2,528
- 20
- 23

kkashyap1707
- 494
- 2
- 8
- 16