在within()函数中返回值
<p>1.<strong>解决在within()函数中无法返回值的问题</strong></p>
<ul>
<li>直接return获取不到值</li>
</ul>
<p>如函数定义如下:</p>
<pre><code class="language-javascript">function_test(){
cy.get('XXXXX').within(()=>{
if(Cypress.$('tr:contains(qa-cy)').length>0){
cy.log('元素存在');
deploymentListPage.deleteDeployment(appName);
}else
{
cy.log('元素不存在');
return false;
}
})
}
//在调用函数时,return的值为空;
let flag;
flag = listPage.function_test();
cy.log(flag); // 输出的值为空
</code></pre>
<ul>
<li>使用变量和别名,返回对象
<pre><code class="language-javascript">function_test(){
cy.get('XXX').within(()=>{
if(Cypress.$('tr:contains(qa-cy)').length>0){
cy.log('元素存在');
deploymentListPage.deleteDeployment(appName);
}else
{
cy.log('元素不存在');
//封装成名为flag的对象
return cy.wrap('false').as('flag');
}
})
}
//在调用函数时
cy.get('@flag').then((el)=>{
cy.log('el: '+el); // 输出的值为 el: flase
})</code></pre>
<p>参考:
<a href="https://docs.cypress.io/api/commands/wrap#Syntax">https://docs.cypress.io/api/commands/wrap#Syntax</a>
<a href="https://docs.cypress.io/guides/core-concepts/variables-and-aliases#Sharing-Context">https://docs.cypress.io/guides/core-concepts/variables-and-aliases#Sharing-Context</a></p></li>
</ul>