英文:
Playwright: How to pass environment variables from library to pipeline?
问题
背景
我有一个在Azure DevOps管道中运行Playwright集成测试的React/TypeScript项目。管道从Azure DevOps库中获取环境变量。
问题
我可以在本地成功运行针对我的部署的预生产环境的测试。
然而,在管道中,这些测试失败。它们失败是因为管道无法读取我尝试从库中传递的环境变量。
代码中出现问题的部分:
await page.getByRole('textbox', { name: 'myVariable' }).fill(process.env.MyVariable as string);
我如何将环境变量从库传递到管道,以便Playwright能够读取它们?
英文:
Background
I have a React/TypeScript project running Playwright integration tests in an Azure DevOps pipeline. The pipeline gets environment variables from an Azure DevOps library.
<br>
Problem
I can run the tests locally against my deployed pre-prod environment successfully.
However, the tests fail in the pipeline. They fail because the pipeline cannot read the environment variables I am trying to pass in from the library.
Failing line of code:
await page.getByRole('textbox', { name: 'myVariable' }).fill(process.env.MyVariable as string);
Pipeline error on my environment variable:
<br>
How do I pass environment variables from the library to the pipeline such that Playwright can read them?
答案1
得分: 1
- 我将我的测试文件名从
myFile.spec.ts
更新为myFile.spec.tsx
。 - 我给我的环境变量加上了前缀
REACT_APP_
。
await page.getByRole('textbox', { name: 'myVariable' }).fill(process.env.REACT_APP_MY_VARIABLE as string);
- 现在我的 Azure DevOps 流水线可以读取 Azure DevOps 库中的变量,测试成功!
英文:
Solution
- I updated my test file name from
myFile.spec.ts
tomyFile.spec.tsx
. - I prefixed my environment variables with
REACT_APP_
.
await page.getByRole('textbox', { name: 'myVariable' }).fill(process.env.REACT_APP_MY_VARIABLE as string);
- Now my Azure DevOps pipeline can read the variables in the Azure DevOps library, and the tests succeed!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论