赛普拉斯:测试元素是否存在 [英] Cypress: test if element exists
问题描述
我是赛普拉斯的新手,我知道之前曾有人问过这个问题,但我仍然被困在这里!这是我写的:
I am new to cypress, I know that this question has been asked before but I am still stuck here! This is the what I have written:
it("User can set certification for multiple users at once",()=>{
cy.get(':nth-child(4) > .RadioButton__StyledRadioButton-sc-1j2qp2u-1').click(); //clicking Non-certified button
const $el=Cypress.$('[class="Typography__StyledTypography-sc-153d8g4-0.jhYDmS.TrainingQueueListstyles__EmptyListMessage-sc-19yfim3-1.ihRiqU"]');
cy.get("body").then($body =>{
if ($body.find($el.length>0)) {
cy.log('in if loop');
cy.log($el.length);
cy.get('.hZDZBR').should('not.exist');
cy.get('input[id="selectAll"]').should('not.exist');
} else {
cy.log('in else loop');
cy.log($el.length);
cy.get('input[id="selectAll"]').click();
cy.get('.hZDZBR').click();
}
})
})
但是当找不到元素 $ el
时(它记录了正确的 $ el.length = 0
),它仍然尝试执行if循环,而应该在else循环中...有关如何解决此问题的任何想法?谢谢!
But when the element $el
is not found (and it logs $el.length=0
which is correct), it still tries to execute the if loop, whereas it should be in else loop...any ideas on how to resolve this?Thanks!
所以我再次更新了我的代码,因为这种情况($ body.find($ el.length> 0))
总是评估为true,正如理查德·马特森(Richard Matsen)正确指出的那样.现在发生的事情是它计算长度为0,因此进入else循环,但是我可以在网页上看到该元素,但长度仍为0.这是什么原因,我该怎么办?
So I updated my code again because this condition ($body.find($el.length>0))
always evaluates to true as rightly pointed out by Richard Matsen. What happens now is that it calculates the length to be 0, and therefore enters else loop, but I can see the element on the web page and still it gets the length as 0..what is the reason and what can I do about it?
it("User can set certification for multiple users at once",()=>{
cy.get(':nth-child(4) > .RadioButton__StyledRadioButton-sc-1j2qp2u-1').click(); //clicking Non-certified button
const $el=Cypress.$('[class="Typography__StyledTypography-sc-153d8g4-0.jhYDmS.TrainingQueueListstyles__EmptyListMessage-sc-19yfim3-1.ihRiqU"]');
cy.get("body").then($body =>{
if ($body.find($el).length>0) {
cy.log('in if loop');
cy.log($body.find($el).length);
cy.get('.hZDZBR').should('not.exist');
cy.get('input[id="selectAll"]').should('not.exist');
} else if($body.find($el).length==0){
cy.log('in else loop');
cy.log($body.find($el).length);
cy.get('input[id="selectAll"]').click();
cy.get('.hZDZBR').click();
}
})
})
推荐答案
如何使用此功能
it("User can set certification for multiple users at once",()=>{
cy.get(':nth-child(4) > .RadioButton__StyledRadioButton-sc-1j2qp2u-1').click(); //clicking Non-certified button
const selector=".Typography__StyledTypography-sc-153d8g4-0.jhYDmS.TrainingQueueListstyles__EmptyListMessage-sc-19yfim3-1.ihRiqU";
cy.get("body").then($body =>{
const $el = $body.find(selector);
if ($el) {
cy.log('in if loop');
cy.get('.hZDZBR').should('not.exist');
cy.get('input[id="selectAll"]').should('not.exist');
} else {
cy.log('in else loop');
cy.get('input[id="selectAll"]').click();
cy.get('.hZDZBR').click();
}
})
})
我所做的一些更改是:
- 使用选择器而不是查询初始元素
- 查询回调中的实际元素
- 如果/其他基于该结果
这篇关于赛普拉斯:测试元素是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!