Cypress学习文档

沉淀cypress对常用html组件定位的文档


挑选待运行测试用例

<p>[TOC]</p> <h1>1、静态挑选待运行测试用例</h1> <p>是指给测试用例添加关键字如:.only()、.skip()、或者指定 runFlag 且在运行时指定 runFlag 的值</p> <h1>2、动态挑选待运行测试用例</h1> <pre><code>推荐使用插件2,插件1作为参照对比</code></pre> <h4>什么是动态挑选待运行测试用例</h4> <p>指给测试用例添加一个或多个相应描述关键字,在运行时,指定相应的关键字,运行或排斥测试用例</p> <h2>A、插件1 (cypress-select-tests)</h2> <h4>如何动态挑选待运行测试用例</h4> <p>使用 cypress-select-tests 插件 官方:<a href="https://github.com/bahmutov/cypress-select-tests">https://github.com/bahmutov/cypress-select-tests</a></p> <h4>安装插件</h4> <p>注意:目前最高仅支持到<code>cypress@7.4.0</code></p> <pre><code>npm install --save-dev cypress-select-tests</code></pre> <h4>设置插件</h4> <pre><code>const selectTestsWithGrep = require('cypress-select-tests/grep') module.exports = (on, config) =&gt; { on('file:preprocessor', selectTestsWithGrep(config)) }</code></pre> <h4>指定测试用例运行的栗子</h4> <pre><code>context('指定测试用例运行的栗子', function () { it('[smoke] 登录用例1', function () { cy.log('登录成功') }); it('[e2e,smoke] 登录用例1', function () { cy.log('登录成功') }); })</code></pre> <h4>执行以下命令</h4> <pre><code>npx cypress open --env grep=e2e</code></pre> <ul> <li>打开 Cypress 运行器,运行测试用例文件</li> <li>--env grep=e2e 的作用:指定包含 e2e 标签的测试用例运行 <h5>使用该插件的重点</h5> <p>其实就是【写标签,通过各种方式传递环境变量】,以下是通过 CLI 方式传递环境变量的几种写法和对应的作用</p> <pre><code>\# 仅运行带有 works 标签的测试用例 yarn cypress open --env grep = works \# 仅运行文件名中带有 foo 的文件 yarn cypress open --env fgrep = foo \# 仅运行文件名中带有 foo 的文件,且仅运行文件中带有 works 标签的测试用例 yarn cypress open --env fgrep = foo,grep = works \# 仅运行带有 '功能A' 标签的测试用例 yarn cypress open --env grep ='功能A' #仅运行文件名中不带有 foo 的文件 yarn cypress open --env fgrep = foo,invert = true #仅运行不带有 works 标签的测试用例 yarn cypress open --env grep = works,invert = true</code></pre> <h2>B、插件2 (cypress-grep)</h2> <p>使用用例的name和tags挑选用例</p> <h4>安装插件</h4> <pre><code>npm install --save-dev cypress-grep</code></pre> <h4>引入</h4> <p>cypress/support/index.js</p> <pre><code>const registerCypressGrep = require('cypress-grep') registerCypressGrep()</code></pre> <p>cypress/plugins/index.js</p> <pre><code>module.exports = (on, config) =&gt; { require('cypress-grep/src/plugin')(config) return config }</code></pre> <h4>使用</h4> <pre><code># run only the tests with "auth user" in the title $ npx cypress run --env grep="auth user" # run tests with "hello" or "auth user" in their titles # by separating them with ";" character $ npx cypress run --env grep="hello; auth user" # run tests tagged @fast $ npx cypress run --env grepTags=@fast # run only the tests tagged "smoke" # that have "login" in their titles $ npx cypress run --env grep=login,grepTags=smoke # only run the specs that have any tests with "user" in their titles $ npx cypress run --env grep=user,grepFilterSpecs=true # only run the specs that have any tests tagged "@smoke" $ npx cypress run --env grepTags=@smoke,grepFilterSpecs=true # run only tests that do not have any tags # and are not inside suites that have any tags $ npx cypress run --env grepUntagged=true</code></pre> <h4>推荐使用方式</h4> <p>将<code>grepFilterSpecs</code>和<code>grepOmitFiltered</code>两个参数设置再环境变量中,参数含义见<a href="https://github.com/cypress-io/cypress-grep" title="参考">官方文档</a></p> <pre><code>    "env": {       "grepFilterSpecs": true,       "grepOmitFiltered": true }</code></pre> <p>用例中加tag的方式</p> <pre><code> it('删除已经存的statefulset资源',{ tags: ['smoke', 'some-other-tag'] }, () =&gt; { cy.loadPage('statefulset', orgId, projectId, envId, env, clusterId, cluster, appName, appId); statefulsetListPage.checkIfResourceExist(appName); })</code></pre> <h1>参考</h1> <p>[1]. <a href="https://github.com/bahmutov/cypress-select-tests">cypress-select-tests</a> [2]. <a href="https://github.com/cypress-io/cypress-grep">cypress-grep</a></p></li> </ul>

页面列表

ITEM_HTML