Tab key press doesn't consider focusable elements inside shadow dom
What is your Test Scenario?
Pressing tab key to focus an element present inside shadow dom of custom element.
What is the Current behavior?
Pressing tab key in the test code skips the focusable elements present inside shadow dom. Not focusing right element. Click events on elements present inside shadow dom works fine though. This might be because document.querySelectorAll(*) will not consider elements inside shadow dom https://github.com/DevExpress/testcafe/blob/master/src/client/core/utils/dom.js#L163
What is the Expected behavior?
Emulating keypress on 'Tab' key should consider elements present inside shadow dom.
What is your web application and your TestCafe test code?
Your website URL (or attach your complete example):
Rendered html dom
<body>
<button class=''regular-button">Regular Button</button>
<custom-button>
#shadow-root
<button class="custom-button">Custom Button</button>
</custom-button>
</body>
Your complete test code (or attach your test files):
test('focusing correct element on tabbing', async t => {
await t
.click(Selector('.regular-button'))
.pressKey('tab')
.expect(Selector(() => {
// @ts-ignore
let activeElement = document.activeElement || document.body;
while(activeElement && activeElement.shadowRoot) {
const currentElement = activeElement.shadowRoot.activeElement;
if (!currentElement) {
break;
}
activeElement = currentElement;
}
return activeElement;
}).hasClass('custom-button')).ok();
});
Your complete configuration file (if any):
Your complete test report:
Screenshots:
Steps to Reproduce:
- Go to my website ...
- Execute this command...
- See the error...
Your Environment details:
- testcafe version: 1.8.4
- node.js version: 12.13.0
- command-line arguments:
- browser name and version: Chrome 80
- platform and version: macOS 10.14.6
- other:
Thank you for reporting a bug. I've reproduced it for the following test:
import { Selector } from 'testcafe';
fixture `New Fixture`
.page `https://x5ipy.csb.app/`;
test('New Test', async t => {
await t
.wait(5000)
.click(Selector('button').withText('Regular Button'))
.pressKey('tab')
.debug();
});
We will research the issue and update this thread once we have any results.
Perhaps related: I am managing tab and shift+tab key presses inside an element using a React component. I evidently need some way in TestCafe of sending pressKey to a particular element.
We are migrating some puppeteer tests to TestCafe. Puppeteer's await page.keyboard.press('Tab') seems to be doing something that TestCafe's await t.pressKey('tab') isn't.
I am working if the issue is that keyboard events are sent to the document, and hence are only captured (not bubbled), and that the event needs instead to be sent to the document's activeElement?
I think I need the ability to aim t.pressKey at an element for proper bubbling?
The t.pressKey action should work as an end-user action without defining a target element. We'll research why it doesn't work with the shadow dom.
As a workaround, you can try to dispatch the keypress event for a particular element using ClientFunction.
To the team:
I researched the issue. Its cause lies in the getFocusableElements function: https://github.com/DevExpress/testcafe/blob/784414652f49dad5bb665f0f4e2d34d646b49652/src/client/core/utils/dom.js#L160
This function does not search for elements inside the shadow dom.
During the research, I added the target shadow dom element to the returned value of the getFocusableElements function. After that, the test started to work as expected.
Here is the branch with my research and test: https://github.com/AlexKamaev/testcafe/tree/gh-4988
I've run into this issue as well. Are there any updates?
No updates yet. Once we get any results, we will post them in this thread.