然后无法返回值

发布于 2025-02-11 17:53:56 字数 1117 浏览 0 评论 0原文

我对柏树及其整个“链接”的东西感到困惑。我有类似的内容:

记录消息返回正确的消息,但是返回我的价值是未定义的。我在这里想念什么?我实际上并不想要日志消息,但这是了解正在发生的事情的唯一方法。如果我用返回替换cy.log,我会变得不确定。

 const getCardUniqueByTitle = (cardSearch: string) => {
   cy.get(".card-title")).each((cardTitle) => {
        if (cardTitle.text().indexOf(cardSearch) >= 0) {
          cy.wrap(cardTitle)
            .parents("find-parents-id-here"))
            .find("some field")
            .find("a field within")
            .invoke("text")
            .then((unique) => {
              cy.log(`The unique for ${cardSearch} is '${unique}'`).then(
                () => {
                  return unique;
                });
            });
        }
      });
    };

所以在我的测试中,我有类似的东西:

describe( "Get sums for enabled cards", () => {
  it("sum two cards values", () => {
    const card1 = getCardUniqueByTitle("foo");
    const card2 = getCardUniqueByTitle("bar");
    const total = String(parseInt(card1) + parseInt(card2));
    cy.get(".subtotal).invoke("text").should('eq', total);
  });
});
      

I am beyond confused by Cypress and its whole "chaining" thing. I have something like the following:

The logging message returns the correct message, but returning my value is undefined. What am I missing here? I don't actually want the log message, but it was the only way to get some insight into what was going on. If I replace the cy.log with a return, I get undefined.

 const getCardUniqueByTitle = (cardSearch: string) => {
   cy.get(".card-title")).each((cardTitle) => {
        if (cardTitle.text().indexOf(cardSearch) >= 0) {
          cy.wrap(cardTitle)
            .parents("find-parents-id-here"))
            .find("some field")
            .find("a field within")
            .invoke("text")
            .then((unique) => {
              cy.log(`The unique for ${cardSearch} is '${unique}'`).then(
                () => {
                  return unique;
                });
            });
        }
      });
    };

So in my test I have something like:

describe( "Get sums for enabled cards", () => {
  it("sum two cards values", () => {
    const card1 = getCardUniqueByTitle("foo");
    const card2 = getCardUniqueByTitle("bar");
    const total = String(parseInt(card1) + parseInt(card2));
    cy.get(".subtotal).invoke("text").should('eq', total);
  });
});
      

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

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

发布评论

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

评论(1

も让我眼熟你 2025-02-18 17:53:56

您必须返回每个cy。链才能检索元素的文本。对于您的情况,它将是:

const getCardUniqueByTitle = (cardSearch: string) => {
   return cy.get(".card-title")).each((cardTitle) => {
        if (cardTitle.text().indexOf(cardSearch) >= 0) {
          return cy.wrap(cardTitle)
            .parents("find-parents-id-here"))
            .find("some field")
            .find("a field within")
            .invoke("text")
        }
   });
};

另一种方法,将使用.contains().alias()

describe( "Get sums for enabled cards", () => {
  it("sum two cards values", () => {
    cy.contains(".card-title", "foo")
      .invoke('text')
      .then(parseInt)
      .as('fooNum')
    cy.contains(".card-title", "bar")
      .invoke('text')
      .then(parseInt)
      .as('barNum')
    cy.get(".subtotal")
      .invoke("text")
      .then(parsInt)
      .as('subtotal')
      .then(function() {
        expect(this.fooNum + this.fooBar).to.eq(this.subtotal)
      }
  })
})

You'll have to return each cy. chain to retrieve the text of the element. For your case it will be:

const getCardUniqueByTitle = (cardSearch: string) => {
   return cy.get(".card-title")).each((cardTitle) => {
        if (cardTitle.text().indexOf(cardSearch) >= 0) {
          return cy.wrap(cardTitle)
            .parents("find-parents-id-here"))
            .find("some field")
            .find("a field within")
            .invoke("text")
        }
   });
};

Alternatively, a different approach would be use .contains() in junction with an .alias()

describe( "Get sums for enabled cards", () => {
  it("sum two cards values", () => {
    cy.contains(".card-title", "foo")
      .invoke('text')
      .then(parseInt)
      .as('fooNum')
    cy.contains(".card-title", "bar")
      .invoke('text')
      .then(parseInt)
      .as('barNum')
    cy.get(".subtotal")
      .invoke("text")
      .then(parsInt)
      .as('subtotal')
      .then(function() {
        expect(this.fooNum + this.fooBar).to.eq(this.subtotal)
      }
  })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文