条件 UI 操作



我正在编写一个柏树测试,并试图找出一种情况,何时可以根据启动画面是否出现来执行操作。

例如:

it('Test 2',()=>{
if(cy.get('#center-tile-banner-popup'))
{
cy.get('[title="Accept Cookies"]').click();
}
cy.get('.media-object ').click(10, 10);
})

关于如何实现这一目标的任何想法?

你的代码有几件事,

  • cy.get()不会返回布尔值供if()检查,因此您不能在那里使用它。

  • 如果未找到选择器,cy.get()测试失败,因此如果初始屏幕不存在,则不会执行最后一行。

测试选择器而不失败的一种方法是使用在全局Cypress.$()上提供的 JQuery。

下面是一个示例测试来说明,

.HTML

<div class="maybe"></div>
<div class="anotherDiv"></div>

规范

before(() => {
cy.visit('/app/conditional-execution.html')
})
it('conditional execution - element found', () => {
cy.get('div.maybe')  // fails test and stops here if not found
const $element = Cypress.$('div.maybe')  // non-failing check, returns JQuery object
const exists = !!$element.length;        // convert JQuery object to a boolean
console.log('Element found?', exists);
if (exists) {
// Conditional commands
} 
// Commands to execute if found or not
cy.get('div.anotherDiv')  
})
it('conditional execution - element not found', () => {
const $element = Cypress.$('div.maybeNot')  // non-failing check, returns JQuery object
const exists = !!$element.length;           // convert JQuery object to a boolean
console.log('Element found?', exists);
if (exists) {
// Conditional commands
} 
// Commands to execute if found or not
cy.get('div.anotherDiv')  
})

这将是你的语法

if(Cypress.$('#center-tile-banner-popup').length) {
cy.get('[title="Accept Cookies"]').click();
}

最新更新