多环境运行配置
<h1>多环境运行(配置文件+权限)</h1>
<h2>1.配置文件</h2>
<p>使用 cypress.env.json</p>
<p>Cypress 允许针对不同测试环境使用多个配置文件并且在运行时动态指定
从而免除每切换一次环境,就需要更改环境变量值的情况。</p>
<h3>1.1 npm安装</h3>
<pre><code>npm install --save-dev fs-extra
npm install --save-dev fs</code></pre>
<h3>1.2 创建文件夹和文件</h3>
<p>在 cypress安装目录下创建一个 config 文件夹
文件夹下建立两个文件,分别命名为 cypress.beta.json,cypress.ppe.json
分别设置配置信息:
如cypress.beta.json文件</p>
<pre><code class="language-json">{
&quot;baseUrl&quot;: &quot;http://ecp-beta1.eniot.io&quot;,
&quot;viewportWidth&quot;: 1920,
&quot;viewportHeight&quot;: 1024,
&quot;defaultCommandTimeout&quot;: 20000,
&quot;reporter&quot;: &quot;junit&quot;,
&quot;reporterOptions&quot;: {
&quot;mochaFile&quot;: &quot;reports/ecp-fe-test-output-[hash].xml&quot;
},
&quot;video&quot;: false,
&quot;chromeWebSecurity&quot;: false,
&quot;env&quot;: {
&quot;orgId&quot;: 1,
&quot;projectId&quot;: 842,
&quot;namespace&quot;: &quot;qa-cypress&quot;,
&quot;env&quot;: &quot;beta&quot;,
&quot;cluster&quot;: &quot;beta&quot;,
&quot;appName&quot;:&quot;qa-cy-demo01&quot;,
&quot;cloneAppName&quot;:&quot;qa-cy-demo02&quot;
}
}</code></pre>
<h3>1.3 在 cypress 安装目录/plugins/index.js 中更改配置如下</h3>
<pre><code>const fs = require('fs-extra')
const path = require('path')
function getConfigurationByFile(file){
const pathToConfigFile = path.resolve('..','ECP-FE-AUTO/cypress/config',`cypress.${file}.json`)
console.log(pathToConfigFile)
return fs.readJson(pathToConfigFile)
}
module.exports = (on,config) =&gt; {
//指定一个环境变量,如没有指定,则使用 cypress.beta.json
const file = config.env.configFile || 'beta'
return getConfigurationByFile(file)
}</code></pre>
<h3>1.4 启动时选择环境</h3>
<p>npx cypress open --env configFile=ppe</p>
<h2>2.不同环境权限</h2>
<p>针对不同环境中,角色的权限可能不同的情况</p>
<h3>2.1 启动时配置,一个env,用于标识环境</h3>
<pre><code>npx cypress open --env configFile=st1</code></pre>
<h3>2.2 将此环境变量传入PO</h3>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/31ac4e73652988f599030e0ce48b9223" alt="" /></p>
<h3>2.3 PO中根据env的值加载permission文件</h3>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/5ade92837249f87c56167dc152679c9d" alt="" /></p>
<p>各环境permission文件:</p>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/06410273295ed5ba02d3d957d3b61ec9" alt="" /></p>
<h1>Reference</h1>
<p><a href="https://docs.cypress.io/api/plugins/configuration-api#Switch-between-multiple-configuration-files">https://docs.cypress.io/api/plugins/configuration-api#Switch-between-multiple-configuration-files</a></p>
<p>10.0 以上的版本配置:
<a href="https://juejin.cn/post/7174572545402732557">https://juejin.cn/post/7174572545402732557</a></p>
<h3>ps</h3>
<p>path.resolve() 是用来返回相对于当前的工作目录(ecp-fe-auto)的绝对路径的;</p>
<p>这个‘..’ 相当于 cd ../</p>
<p>如:
1、path.resolve('..','ecp-fe-auto/cypress/config',<code>cypress.${file}.json</code>)
<img src="https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/7bd87c6aac2a2e6fcf244276cc9b132c" alt="" /></p>
<p>等价于:
2、path.resolve('cypress/config',<code>cypress.${file}.json</code>)
<img src="https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/71356991d5280a0bd99c410de532992c" alt="" /></p>
<h2>Notes</h2>
<p>如果在本地运行时,当修改了cypress.json配置文件之后,需要重启Test Runner</p>