To enable the autoplay option in @playwright/test and reduce flakiness related to expected autoplay behavior, you can use the permissions
property in the contextOptions
when creating a new test context. This allows you to grant the video
and audio
autoplay permissions to the browser context.
Here's an example of how to enable autoplay in a test:
import { test, expect } from '@playwright/test';
test.describe('Autoplay Test', () => {
test.beforeEach(async ({ context }) => {
// Grant autoplay permissions for video and audio
await context.grantPermissions(['video', 'audio']);
});
test('Check if video autoplay is working', async ({ page }) => {
await page.goto('https://ray.run/');
const videoElement = await page.locator('video');
const isPlaying = await videoElement.evaluate(video => !video.paused);
expect(isPlaying).toBe(true);
});
});
In this example, we grant the video
and audio
permissions to the browser context before each test. Then, we navigate to the website and check if the video element is playing as expected.
By granting the necessary permissions, you can ensure that the autoplay behavior works as intended, reducing flakiness in your tests. For more tips on writing efficient @playwright/test scripts, check out Tips for Writing Efficient Playwright Test Scripts.
If you still have questions, please ask a question and I will try to answer it.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].