赛普拉斯:如果找不到任何元素,我可以防止赛普拉斯cy.get失败吗? [英] Cypress: Can I prevent Cypress cy.get from failing if no elements are found?

查看:380
本文介绍了赛普拉斯:如果找不到任何元素,我可以防止赛普拉斯cy.get失败吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cypress cy.get来获取元素,但是如果没有,则测试失败。
我不希望它失败。我希望它继续。测试只是简单地列出其中的项目。

I am using Cypress cy.get to grab elements, but if there are none, my test is failing. I do not want it to fail. I want it to continue. The test is simply to list the items that are there, if any.

const listItemTitle = '[data-cy-component=list-item-title]';
cy.get(listItemTitle).each(($el, index, $list) => {
  cy.wrap($el).then(($span) => {
    const spanText = $span.text();
    cy.log(`index: ` + index + ' ' + spanText);
  });
});

我本以为,如果没有任何元素,该代码仍然可以,但是不是。运行它时,出现以下错误:CypressError:重试超时:预期会找到元素: [data-cy-component = list-item-title],但从未找到。

I would have thought, if there are no elements there - that this code would still be ok, but not so. When I run it, I get this error: CypressError: Timed out retrying: Expected to find element: '[data-cy-component=list-item-title]', but never found it.

在存在元素的地方效果很好。如果未找到任何元素,我要继续&做不同的测试。

It works fine where elements are present. If no elements are found, I want to go on & do a different test.

这是我尝试过的实验:

let count: number = Cypress.$(listItemTitle).length;
cy.log('before:' + count);
cy.get(actionsBarActionsAdd).click();
cy.get(singlePickerSearch).type('Assets' + '{enter}');
cy.get(listItemCheckboxTitle)
  .first()
  .click();
cy.toast({
  type: 'Success',
});
count = Cypress.$(listItemTitle).length;
cy.log('after:' + count);
cy.get(listItemTitle).each(($li, index, $lis) => {
  cy.log('interface no. ' + (index + 1) + ' of ' + $lis.length);
  cy.wrap($li).click();
});

这是结果:

18 LOG        before:0
19 GET       [data-cy-component-key="actions-add"] input
20 CLICK
21 GET       [data-cy-component=single-picker-search] input
22 TYPE      Assets{enter}
23 GET       [data-cy-component="list-item-checkbox-title"]
24 FIRST
25 CLICK
26 GET       .iziToast    toast2
27 ASSERT    expected [ <div.iziToast.iziToast-opening.fadeInUp.iziToast-theme- 
alloy.iziToast-color-green.iziToast-animateInside>, 1 more... ] to have class iziToast-color-green
28 LOG       after:0
29 GET       [data-cy-component=list-item-title]
30 LOG       interface no. 1 of 1

最终显示Cypress。$(listItemTitle).length不计算具有选择器:listItemTitle。

Conclusively shows that Cypress.$(listItemTitle).length does not count number of elements with selector: listItemTitle.

更新:

通过放置cy.wait(1000);在执行完Add(在我的实验中)后-给DOM时间更新-找到了新元素。对于更具体的选择器,不需要等待

By putting a cy.wait(1000); after the Add had been executed (in my experiment) - giving the DOM time to update - the new element was found. With more specific selectors, the wait is not required

推荐答案

您可以通过 Cypress使用jquery。$ 检查是否存在。

You can use jquery via Cypress.$ to check if any exist.

const listItemTitle = '[data-cy-component=list-item-title]';
if (Cypress.$(listItemTitle).length > 0) {
  cy.get(listItemTitle).each(($el, index, $list) => {
    cy.wrap($el).then(($span) => {
    const spanText = $span.text();
    cy.log(`index: ` + index + ' ' + spanText);
    });
  });
}

这篇关于赛普拉斯:如果找不到任何元素,我可以防止赛普拉斯cy.get失败吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆