admin管理员组文章数量:1334133
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.
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.
Share Improve this question edited Mar 17, 2022 at 15:43 simi asked Mar 17, 2022 at 14:59 simisimi 1,5554 gold badges12 silver badges14 bronze badges1 Answer
Reset to default 4The 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./');
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-configuration#projects
本文标签: javascriptCan a variable be passed to Playwright JS config in globalsetupjs fileStack Overflow
版权声明:本文标题:javascript - Can a variable be passed to Playwright JS config in global-setup.js file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742349052a2458112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论