I'm encountering an issue with keyboard focus when running tests in Cypress with the --record flag enabled. If there is no next focusable item when pressing Tab, the focus unexpectedly resets back to the first focusable item. This behaviour only happens when the --record flag is turned on.
Without the --record flag, focus behaves as expected: it moves forward between elements and remove focus when no more focusable items are available.
With the --record flag enabled, if there is no next focusable item, the focus resets to the first focusable element.
Test:
import { Button, Stack } from '@mui/material';
import { mount } from 'cypress/react18';
describe('MUI Button Keyboard Focus Test', () => {
beforeEach(() => {
// Mount MUI buttons directly
mount(
<Stack direction='column' spacing={2}>
<Button>First Button</Button>
<Button>Second Button</Button>
<Button>Third Button</Button>
</Stack>
);
cy.get('button').as('button').first().focus();
});
it('should not reset focus to the first button when pressing Tab on the last button', () => {
cy.get('@button').first().should('be.focused');
// Press Tab and check the focus moves to the second button
cy.realPress('Tab');
cy.get('@button').first().should('not.be.focused');
cy.get('@button').eq(1).should('be.focused');
// Press Tab again and check the focus moves to the third button
cy.realPress('Tab');
cy.get('@button').eq(1).should('not.be.focused');
cy.get('@button').eq(2).should('be.focused');
// Press Tab on the last button and ensure the focus doesn't reset to the first button
cy.realPress('Tab');
// Verify that none of the buttons have focus after pressing Tab on the last button
cy.get('@button').should('not.be.focused');
});
});
Command: cypress run --component --record -e TAGS='not @manual'
import {addCucumberPreprocessorPlugin} from '@badeball/cypress-cucumber-preprocessor';
import {createRollupPlugin} from '@badeball/cypress-cucumber-preprocessor/rollup';
import {devServer} from '@cypress/vite-dev-server';
import {viteCommonjs} from '@originjs/vite-plugin-commonjs';
import react from '@vitejs/plugin-react';
import {defineConfig} from 'cypress';
export default defineConfig({
component: {
specPattern: '**/*.feature',
projectId: '1wb9vw', // For cypress enterprise and recording capabilities.
devServer(devServerConfig) {
return devServer({
...devServerConfig,
framework: 'react',
viteConfig: {
plugins: [
react(),
createRollupPlugin(devServerConfig.cypressConfig),
viteCommonjs(),
],
},
});
},
async setupNodeEvents(on, config) {
// This is required for the preprocessor to be able to generate JSON reports after each run, and more.
await addCucumberPreprocessorPlugin(on, config);
// Make sure to return the config object as it might have been modified by the plugin.
return config;
},
},
retries: {
// Configure retry attempts for `cypress run`.
runMode: 1,
// Configure retry attempts for `cypress open`.
openMode: 0,
},
video: true,
viewportWidth: 1280,
viewportHeight: 1024,
});
Full log and debug output
```
// Put your logs below this line
Timed out retrying after 4000ms: expected '[ , 2 more... ]' not to be 'focused'
```
Please confirm
Environment information
Describe the bug
I'm encountering an issue with keyboard focus when running tests in Cypress with the
--record
flag enabled. If there is no next focusable item when pressing Tab, the focus unexpectedly resets back to the first focusable item. This behaviour only happens when the--record
flag is turned on.--record
flag, focus behaves as expected: it moves forward between elements and remove focus when no more focusable items are available.--record
flag enabled, if there is no next focusable item, the focus resets to the first focusable element.Test:
https://github.com/user-attachments/assets/0851f7c9-1b3e-4c83-a65e-2b97e850fa72
Expected behavior
I should move forward between elements and remove focus when no more focusable items are available.
https://github.com/user-attachments/assets/0d6f7e54-d39c-49f2-9290-a42636e1b583
Setup and Command
Command:
cypress run --component --record -e TAGS='not @manual'
Full log and debug output