Playwright:如何从库传递环境变量到流水线?

huangapple go评论51阅读模式
英文:

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:如何从库传递环境变量到流水线?

我如何将环境变量从库传递到管道,以便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(&#39;textbox&#39;, { name: &#39;myVariable&#39; }).fill(process.env.MyVariable as string);

Pipeline error on my environment variable:
Playwright:如何从库传递环境变量到流水线?

<br>

How do I pass environment variables from the library to the pipeline such that Playwright can read them?

答案1

得分: 1

  1. 我将我的测试文件名从 myFile.spec.ts 更新为 myFile.spec.tsx
  2. 我给我的环境变量加上了前缀 REACT_APP_
await page.getByRole('textbox', { name: 'myVariable' }).fill(process.env.REACT_APP_MY_VARIABLE as string);
  1. 现在我的 Azure DevOps 流水线可以读取 Azure DevOps 库中的变量,测试成功!
英文:

Solution

  1. I updated my test file name from myFile.spec.ts to myFile.spec.tsx.
  2. I prefixed my environment variables with REACT_APP_.
await page.getByRole(&#39;textbox&#39;, { name: &#39;myVariable&#39; }).fill(process.env.REACT_APP_MY_VARIABLE as string);
  1. Now my Azure DevOps pipeline can read the variables in the Azure DevOps library, and the tests succeed!

Playwright:如何从库传递环境变量到流水线?

huangapple
  • 本文由 发表于 2023年2月18日 07:27:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定