如何创建条件测试?

发布于 2025-02-09 08:03:57 字数 770 浏览 0 评论 0原文

大家好!我正在尝试在柏树中创建有条件的测试,但无法弄清楚如何做。如果DOM中有一个元素,我必须执行一个构造。如果不是,那么应该只有一个通行证。如何实施它?

it("something test", () => {
    cy.get("body").then(($body) => {
      if ($body.find('button[name="button-row-add"]')) {
        cy.get('button[name="button-row-add"]').click();
        cy.get('input[name="newValue"]').type("Cypress Test String");
        cy.get('button[name="submit-dimension"]').click();
        cy.wait(500);
      }
    });

    cy.get(".table-cell").each(($el) => {
      cy.wrap($el).dblclick();
      cy.wrap("[class^=form-cell]").as("form").should("exist");
      cy.wrap("@form").get('textarea[name="value"]').type("Cypress Sell Test");
      cy.wrap("@form").get('button[aria-label="submit-button"]').click();
    });
  })

Good day everyone! I'm trying to create a conditional test in cypress but can't figure out how to do it. I have a construct that must be executed if there is an element in the DOM. If it is not, then there should be just a pass. How to implement it?

it("something test", () => {
    cy.get("body").then(($body) => {
      if ($body.find('button[name="button-row-add"]')) {
        cy.get('button[name="button-row-add"]').click();
        cy.get('input[name="newValue"]').type("Cypress Test String");
        cy.get('button[name="submit-dimension"]').click();
        cy.wait(500);
      }
    });

    cy.get(".table-cell").each(($el) => {
      cy.wrap($el).dblclick();
      cy.wrap("[class^=form-cell]").as("form").should("exist");
      cy.wrap("@form").get('textarea[name="value"]').type("Cypress Sell Test");
      cy.wrap("@form").get('button[aria-label="submit-button"]').click();
    });
  })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥枳 2025-02-16 08:03:57

您可以做这样的事情:

cy.get('body').then(($body) => {
  if ($body.find('selector').length > 0) {
    //Element is present, Do something
  }
})

You can do something like this:

cy.get('body').then(($body) => {
  if ($body.find('selector').length > 0) {
    //Element is present, Do something
  }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文