Skip to content

Commit caa8d64

Browse files
author
Vasyl Vavrychuk
committed
more precise click via event target search by xpath
1 parent 7881c8d commit caa8d64

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

web/WebDriverJsDemo.html

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,39 @@
4242
}
4343
}
4444

45+
function getXPath(node, path) {
46+
path = path || [];
47+
if(node.parentNode) {
48+
path = getXPath(node.parentNode, path);
49+
}
50+
51+
if(node.previousSibling) {
52+
var count = 1;
53+
var sibling = node.previousSibling
54+
do {
55+
if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {count++;}
56+
sibling = sibling.previousSibling;
57+
} while(sibling);
58+
if(count == 1) {count = null;}
59+
} else if(node.nextSibling) {
60+
var sibling = node.nextSibling;
61+
do {
62+
if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {
63+
var count = 1;
64+
sibling = null;
65+
} else {
66+
var count = null;
67+
sibling = sibling.previousSibling;
68+
}
69+
} while(sibling);
70+
}
71+
72+
if(node.nodeType == 1) {
73+
path.push(node.nodeName.toLowerCase() + (node.id ? "[@id='"+node.id+"']" : count > 0 ? "["+count+"]" : ''));
74+
}
75+
return path;
76+
}
77+
4578
webdriver.WebDriver.prototype.visualizerGetSource = function() {
4679
webdriver.http.Executor.COMMAND_MAP_['visualizerGetSource'] = {
4780
method: 'GET', path: '/session/:sessionId/-cisco-visualizer_source'};
@@ -297,17 +330,14 @@
297330
return false;
298331
}
299332

300-
var hostSize = {width: win.innerWidth, height: win.innerHeight};
301-
302-
self.driver.manage().window().getSize().then(function(targetSize) {
303-
var targetDoc = self.driver.findElement(webdriver.By.xpath('/html'));
304-
var x = Math.floor(event.clientX * targetSize.width / hostSize.width);
305-
var y = Math.floor(event.clientY * targetSize.height / hostSize.height);
306-
return self.driver.actions().
307-
mouseMove(targetDoc, {x: x, y: y}).
308-
click(event.button).
309-
perform();
310-
}).then(function() {
333+
var xpath = getXPath(event.target);
334+
xpath = '/' + xpath.join('/');
335+
var target = self.driver.findElement(webdriver.By.xpath(xpath));
336+
self.driver.actions().
337+
mouseMove(target, {x: event.offsetX, y: event.offsetY}).
338+
click(event.button).
339+
perform().
340+
then(function() {
311341
return self.driver.visualizerShowPoint();
312342
}).then(function() {
313343
// self.updateSource();

0 commit comments

Comments
 (0)