Skip to content

Commit

Permalink
Fixed focus for click automation (closes #913) (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgiy-abbasov authored and AlexanderMoskovkin committed Oct 26, 2016
1 parent f98ab82 commit cbf6ed6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/client/automation/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export function focusAndSetSelection (element, simulateFocus, caretPos) {
return;
}

var focusWithSilentMode = !simulateFocus;
var focusForMouseEvent = true;
var preventScrolling = false;

if (!isElementFocusable && !isContentEditable) {
var curDocument = domUtils.findDocument(elementForFocus);
var curActiveElement = curDocument.activeElement;
Expand All @@ -76,12 +80,10 @@ export function focusAndSetSelection (element, simulateFocus, caretPos) {
return;
}

elementForFocus = focusableParent || curDocument.body;
elementForFocus = focusableParent || curDocument.body;
preventScrolling = true;
}

var focusWithSilentMode = !simulateFocus;
var focusForMouseEvent = true;

focusBlurSandbox.focus(elementForFocus, () => {
// NOTE: if a different element was focused in the focus event handler, we should not set selection
if (simulateFocus && !isContentEditable && element !== domUtils.getActiveElement()) {
Expand All @@ -97,7 +99,7 @@ export function focusAndSetSelection (element, simulateFocus, caretPos) {
focusBlurSandbox.focus(activeElement, resolve, true, true);
else
resolve();
}, focusWithSilentMode, focusForMouseEvent);
}, focusWithSilentMode, focusForMouseEvent, false, preventScrolling);
});
}

Expand Down
11 changes: 11 additions & 0 deletions test/functional/fixtures/regression/gh-913/pages/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>GH-913</title>
</head>
<body>
<div id="parent" style="width:500px; height:2000px; top:200px; position:absolute; background:grey;" tabindex="0">
<div id="child" style="width:100px; height:100px; top:100px; position:absolute; background:red;"></div>
</div>
</body>
</html>
5 changes: 5 additions & 0 deletions test/functional/fixtures/regression/gh-913/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('[Regression](GH-913)', function () {
it("Shouldn't scroll to the focusable parent while performing click on non-focusable element", function () {
return runTests('testcafe-fixtures/index-test.js', "Shouldn't scroll to target parent while performing click");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect } from 'chai';
import { ClientFunction } from 'testcafe';

fixture `gh-913`
.page `http://localhost:3000/fixtures/regression/gh-913/pages/index.html`;

const getWindowScrollTop = ClientFunction(() => window.pageYOffset);

test("Shouldn't scroll to target parent while performing click", async t => {
const oldWindowScrollValue = await getWindowScrollTop();

await t.click('#child');

const newWindowScrollValue = await getWindowScrollTop();

expect(newWindowScrollValue).eql(oldWindowScrollValue);
});

0 comments on commit cbf6ed6

Please sign in to comment.