You can simulate a slow network connection in Playwright using the slowMo
option. This option slows down Playwright operations by the specified amount of milliseconds. It's a great way to see what's happening during test execution.
By default, slowMo
is set to 0, meaning no delay between operations. But you can set it to a higher value to mimic a slower network. For instance, setting slowMo
to 150 or 200 milliseconds can simulate a slow 3G connection.
Here's how you can use the slowMo
option:
import { chromium } from 'playwright';
async function simulateSlow3G() {
const browser = await chromium.launch();
const context = await browser.newContext({
slowMo: 150,
});
const page = await context.newPage();
await browser.close();
}
simulateSlow3G();
In this example, we've created a new context with slowMo
set to 150 milliseconds. This introduces a delay of that duration between each operation on the page.
Remember, simulating network conditions can vary based on server response times and actual network conditions. For more tips on efficient Playwright test scripts, check out this blog post.
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].