Skip to content

Commit

Permalink
Get activeElement from Shadow DOM (#1061)
Browse files Browse the repository at this point in the history
* Get activeElement from Shadow DOM

* Remove isDomElement check; add a test
  • Loading branch information
AndreyBelym authored and AlexanderMoskovkin committed Mar 17, 2017
1 parent 6fa2485 commit 6ace2b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/client/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,18 @@ export function getActiveElement (currentDocument) {
// https://github.com/DevExpress/testcafe-hammerhead/issues/768
var doc = currentDocument || document;

return isDomElement(doc.activeElement) ? doc.activeElement : doc.body;
var el = isDomElement(doc.activeElement) ? doc.activeElement : doc.body;

while (el && el.shadowRoot) {
var shadowEl = el.shadowRoot.activeElement;

if (!shadowEl)
break;

el = shadowEl;
}

return el;
}

export function getChildVisibleIndex (select, child) {
Expand Down
36 changes: 36 additions & 0 deletions test/client/fixtures/utils/dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,3 +813,39 @@ test('inspect html elements', function () {
ok(domUtils.isTableElement(nativeMethods.createElement.call(document, 'table')), 'table');
ok(domUtils.isTableDataCellElement(nativeMethods.createElement.call(document, 'td')), 'td');
});

if (browserUtils.isChrome) {
test('should return active element inside shadow DOM', function () {
var host = document.createElement('div');
var root = host.createShadowRoot();
var input = document.createElement('input');

document.body.appendChild(host);
root.appendChild(input);

nativeMethods.focus.call(input);

strictEqual(domUtils.getActiveElement(), input);

document.body.removeChild(host);
});

test('should return active element inside nested shadow DOM', function () {
var hostParent = document.createElement('div');
var hostChild = document.createElement('div');
var rootParent = hostParent.createShadowRoot();
var rootChild = hostChild.createShadowRoot();
var input = document.createElement('input');

document.body.appendChild(hostParent);
rootParent.appendChild(hostChild);
rootChild.appendChild(input);

nativeMethods.focus.call(input);

strictEqual(domUtils.getActiveElement(), input);

document.body.removeChild(hostParent);
});
}

0 comments on commit 6ace2b9

Please sign in to comment.