然后无法返回值
我对柏树及其整个“链接”的东西感到困惑。我有类似的内容:
记录消息返回正确的消息,但是返回我的价值是未定义的。我在这里想念什么?我实际上并不想要日志消息,但这是了解正在发生的事情的唯一方法。如果我用返回替换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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须返回每个
cy。
链才能检索元素的文本。对于您的情况,它将是:另一种方法,将使用
.contains()
与.alias()
You'll have to return each
cy.
chain to retrieve the text of the element. For your case it will be:Alternatively, a different approach would be use
.contains()
in junction with an.alias()