Kumar
Kumar

Reputation: 375

Does Playwright offer custom args in the command line?

I want to create custom args in the commandline so that my same specs, when the process.argv matches the custom args, does slightly different things. But I couldn't see that option in the docs. Did I miss it or is this not allowed in Playwright?

Upvotes: 8

Views: 5884

Answers (4)

M András
M András

Reputation: 169

Put into conftest.py

def pytest_addoption(parser):
    parser.addoption('--headed', action='store_true')

In case, you create a Browser, can use

if "--headed" in str(sys.argv):
   Browser.browser_parameters["headless"] = False

Upvotes: 0

Drashti Patel
Drashti Patel

Reputation: 85

Playwright does not support passing custom command-line arguments directly. However, there is an open feature request for this functionality.

In my case, I needed to run two different scripts and wanted to pass a flag to determine whether to use a browser context or a new browser instance. To achieve this, I used the cross-env package to pass custom arguments in the script like this:

"test:nocontext": "cross-env USE_PERSISTENT_CONTEXT=false npx playwright test"
"test:context": "cross-env USE_PERSISTENT_CONTEXT=true npx playwright test"

Then, in the test, I fetched the value like this:

test.beforeEach('Open browser', async ({ browser }) => {
const usePersistentContext = process.env.USE_PERSISTENT_CONTEXT;
    if (usePersistentContext) {
        const browser = await chromium.launchPersistentContext('');
        const pages = browser.pages();
        page = pages[0];
    } else {
        const context = await browser.newContext();
        page = await context.newPage();
    }
});

This might not be exactly what you're looking for, but I thought sharing this alternative could be helpful to others :)

Upvotes: 2

kanji
kanji

Reputation: 839

It seems not possible at this moment. I tried to add my option after --, which terminates original options list, but then those are considered to be arguments to filter files.

Now, I use environmental variables instead. You can define them in .env file and use dotenv package to load it. Then, they will be available through process.env.

import * as dotenv from "dotenv"

export default async function globalSetup() {
    const output = dotenv.config()
    console.log(output.parsed)
    console.log(process.env.MY_ENV_VAR)
    ...
}

Upvotes: 3

Nico Mee
Nico Mee

Reputation: 1102

It's not clear exactly what you mean, but playwright does support custom launch arguments for chrome along with a multitude of other launch options.

Upvotes: -3

Related Questions