Sure, you can modify browser arguments mid-test with Playwright without closing the browser or leaving the current page. Here's how:
First, create an instance of the Browser
class from the playwright
package. This class represents a single browser instance and provides methods for launching new browser contexts.
const { chromium } = require('playwright');
const browser = await chromium.launch();
const context = await browser.newContext({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
In the above example, we've launched a new context with two arguments (--no-sandbox
and --disable-setuid-sandbox
).
If you want to change these arguments mid-test, create a new context with updated arguments and then create a new page within that context.
const updatedContext = await browser.newContext({
args: ['--disable-gpu', '--lang=en-US'],
});
const page = await updatedContext.newPage();
await page.goto('https://ray.run');
In this example, we've created another instance of BrowserContext
called updatedContext
, passing different set of command-line arguments (--disable-gpu
, --lang=en-US
). Then, we create a new page within this updated context using newPage()
method.
Remember, changing browser arguments mid-test may affect the behavior of the browser and the loaded web pages. Some arguments may require a browser restart to take effect, while others can be modified dynamically. So, consider which arguments can be safely changed during runtime and test accordingly.
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].