Skip to content

Commit

Permalink
Fix Chrome's getLocationInView atom
Browse files Browse the repository at this point in the history
ChromeDriver uses webdriver.chrome.getLocationInView atom to get
location of an element, after scrolling it into view if necessary. This
atom had a couple of bugs:

1. When this atom is passed the <html> element (i.e.,
   document.documentElement), it throws an error since the parent node
   is not an element. This causes ChromeDriver bug
   https://crbug.com/chromedriver/1049.

2. Scrolling isn't working properly.

This commit fixes both bugs, and expands the corresponding test.

Signed-off-by: Alexei Barantsev <barancev@gmail.com>
  • Loading branch information
JohnChen0 authored and barancev committed Oct 29, 2019
1 parent 5d3d2dc commit fbd4dd6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
9 changes: 7 additions & 2 deletions javascript/chrome-driver/atoms.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ webdriver.chrome.scrollIntoView_ = function(elem, region, center) {

offset = goog.style.getClientPosition(elem);
var windowSize = goog.dom.getDomHelper(elem).getViewportSize();
scrollHelper(doc.body, windowSize, offset, region, center);
// Chrome uses either doc.documentElement or doc.body, depending on
// compatibility settings. For reliability, call scrollHelper on both.
// Calling scrollHelper on the wrong object is harmless.
scrollHelper(doc.documentElement, windowSize, offset, region, center);
if (doc.body)
scrollHelper(doc.body, windowSize, offset, region, center);
};


Expand Down Expand Up @@ -171,7 +176,7 @@ webdriver.chrome.getLocationInView = function(elem, center, opt_region) {
if (!region)
region = new goog.math.Rect(0, 0, elem.offsetWidth, elem.offsetHeight);

if (elem != document.documentElement)
if (elem != elem.ownerDocument.documentElement)
webdriver.chrome.scrollIntoView_(elem, region, center);

var elemClientPos = goog.style.getClientPosition(elem);
Expand Down
37 changes: 34 additions & 3 deletions javascript/chrome-driver/test/atoms_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,46 @@
assertEquals(0, computeY('c5', 'e5'));
}

function testGetLocationInView() {
function testGetLocationInViewNoSroll() {
var elem = frames[0].document.getElementById('elem0');
var coord = webdriver.chrome.getLocationInView(
elem, false, new goog.math.Rect(0,0,4,4));
var tolerance = 1;
assertRoughlyEquals(108, coord.x, tolerance);
assertRoughlyEquals(108, coord.y, tolerance);
assertEquals(0, frames[0].document.documentElement.scrollLeft);
assertEquals(0, frames[0].document.documentElement.scrollTop);
}

function testGetLocationInViewScrolling() {
var elem = frames[0].document.getElementById('elem');
var coord = webdriver.chrome.getLocationInView(
elem, false, new goog.math.Rect(0,0,4,4));
var tolerance = 1;
assertRoughlyEquals(280, coord.x, tolerance);
assertRoughlyEquals(280, coord.y, tolerance);
assertEquals(408, frames[0].document.body.scrollLeft);
assertEquals(408, frames[0].document.body.scrollTop);
assertEquals(408, frames[0].document.documentElement.scrollLeft);
assertEquals(408, frames[0].document.documentElement.scrollTop);
}

function testGetLocationInViewDocumentElement() {
var elem = document.documentElement;
var coord = webdriver.chrome.getLocationInView(
elem, false, new goog.math.Rect(0,0,4,4));
assertEquals(0, coord.x);
assertEquals(0, coord.y);
assertEquals(0, document.documentElement.scrollLeft);
assertEquals(0, document.documentElement.scrollTop);
}

function testGetLocationInViewFrameDocumentElement() {
var elem = frames[0].document.documentElement;
var coord = webdriver.chrome.getLocationInView(
elem, false, new goog.math.Rect(0,0,4,4));
assertEquals(0, coord.x);
assertEquals(0, coord.y);
assertEquals(0, frames[0].document.documentElement.scrollLeft);
assertEquals(0, frames[0].document.documentElement.scrollTop);
}

function testGetFirstClientRect() {
Expand Down
4 changes: 3 additions & 1 deletion javascript/chrome-driver/test/location_in_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
</style>
</head>
<body>
<div style='height:500px'></div>
<div style='height:100px'></div>
<div id='elem0' style='margin-left:100px; width:100px; height:100px'></div>
<div style='height:300px'></div>
<div style='margin-left:500px; width:200px; height:200px; overflow:scroll'>
<div style='height:500px'></div>
<div id='elem'></div>
Expand Down

0 comments on commit fbd4dd6

Please sign in to comment.