skip to Main Content

I have my config as

Projects :[
{ name: 'setup', testMatch: 'auth.setup.ts' },
    {
      name: 'e2e-uat',
      use: {
        baseURL: 'https://uat.example.com',
        storageState: '.auth/user.json',
      },
      dependencies: ['setup'],
    },
    {
      name: 'e2e-canary',
      use: {
        baseURL: 'https://example.com',
        storageState: '.auth/user.json',
      },
      dependencies: ['setup'],
    },]

but when I run the auth.setup,

await page.goto('/auth/login');

it does not get any baseUrl and hence it becomes an invalid URL.

How can I pass the baseURL from one config to it’s dependency configAs here when I run e2e-uat I want to pass baseURL: 'https://uat.example.com' to project setup

and when I run e2e-canary I want to pass baseURL: 'https://example.com' to project setup

2

Answers


  1. You may access the base URL (from any project) inside your global setup file like below:

    Complete Example:

    //global-setup.ts
    import { chromium, type FullConfig } from '@playwright/test';
    
    async function globalSetup(config: FullConfig) {
      const { baseURL, storageState } = config.projects[0].use;
      const browser = await chromium.launch();
      const page = await browser.newPage();
      await page.goto(baseURL!);
      await page.getByLabel('User Name').fill('user');
      await page.getByLabel('Password').fill('password');
      await page.getByText('Sign in').click();
      await page.context().storageState({ path: storageState as string });
      await browser.close();
    }
    
    export default globalSetup;
    

    Dynamic Reference:

    To pass baseurl dynamically from the command line in Playwright test, you can always pass using the –baseURL option. For example, to run tests against a staging environment, you would run the following command:

    npx playwright test --baseURL=http://staging.example.test/
    

    References:

    Login or Signup to reply.
  2. First solution

    You should create a dedicated setup project for each project,
    and pass the url using the use property inside each of the setups – because a setup project has it’s own tests to execute.

    projects: [
        // e2e-uat
        {
          name: "setup-e2e-uat",
          testMatch: "auth.setup.ts",
          use: { baseURL: "https://uat.example.com" },
        },
        {
          name: "e2e-uat",
          use: {
            baseURL: "https://uat.example.com",
            storageState: ".auth/user.json",
          },
          dependencies: ["setup-e2e-uat"],
        },
    
        // e2e-canary
        {
          name: "setup-e2e-canary",
          testMatch: "auth.setup.ts",
          use: { baseURL: "https://example.com" },
        },
        {
          name: "e2e-canary",
          use: {
            baseURL: "https://example.com",
            storageState: ".auth/user.json",
          },
          dependencies: ["setup-e2e-canary"],
        },
    ]
    

    Second solution

    Using the same setup project for both of the projects,
    is to create a switch-case statement inside the setup test, that checks the project-name that executes current run, and based on the name- pass the wanted URL.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search