Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
Can a variable be passed to Playwright JS config in global-setup.js file?

JS newbie here so please excuse any misconceptions I may have about JS or Playwright...

I have config.js file setup with projects and for each project I define custom variables (like login creds, etc.). How can I access these values in the global-setup.js file when I use the --project=ProjectName when I run the test?

I can use testInfo in tests to access these values, but can I access it in global-setup.js, since (as I understand it) is adds to the config object?

I am trying to use one global-setup file for login in with credentials from either one or another project, which are defined in the config.js file.

So I would run this: npx playwright test testFile.spec.js --project=FirstProject

The playwright.config.js file would have this setup:

  {
    name: "FirstProject",
    username: "blahblah",
    password: "blahblah",
  },

  {
    name: "SecondProject",
    username: "blahblah1",
    password: "blahblah1",
  }
]

And in global-setup.js I would like to check which project is being used in order to provide the correct credentials -- something like this:

module.exports = async config => {
const username = config.projects[${projectName}].username;
const password = config.projects[${projectName}].password;

Is this possible?

Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The playwright documentation suggests you create a fixture. For instance, a file called my-test.js at the level of playwright.config.js:

// my-test.js
const base = require('@playwright/test');

exports.test = base.test.extend({
  // Define an option and provide a default value.
  // We can later override it in the config.
  person: [{username: 'john', password: 'john123'}, { option: true }],
});

Then inside playwright.config.js:

/**
 * @see https://playwright.dev/docs/test-configuration
 * @type {import('@playwright/test').PlaywrightTestConfig<{ person: { 
username: string; password: string } }>}
 */
const config = {
  projects: [
    {
      name: 'FirstProject',
      use: { person: {username: 'blahblah', password: 'blahblah'} },
    },
    {
      name: "SecondProject",
      use: { person: {username: 'blahblah1', password: 'blahblah1'} },
    }
  ]
}

Finally, you can use the fixture in the spec file:

// testFile.spec.js
const { test } = require('../my-test');
const { expect } = require('@playwright/test');

test('test 1', async ({ page, person }) => {
  await page.goto('https://example.com/');
  await page.fill('input[name="login"]', person.username);
  await page.fill('input[name="password"]', person.password);
});

In order to execute the test, use the cli:

npx playwright test testFile.spec.js --project=FirstProject

I tried the code and it worked for me. More information:

  • Playwright docs: https://playwright.dev/docs/test-parameterize#parameterized-projects

  • https://playwright.dev/docs/test-advanced#projects

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda