"Cypress commands do not return their subjects, they yield them" 这个语句是什么意思?
What does this statement mean "Cypress commands do not return their subjects, they yield them"?
我正在了解 Cypress.io 并在他们的官方网站上看到了这个声明 Cypress.io
"Cypress commands do not return their subjects, they yield them."
cypress.io中的命令"yield"和"return"有什么区别?
我认为 yield 的意思是 'pay it forward',而 return = 'pay it back'。
Cypress commands are asynchronous and get queued for execution at a later time. During execution, subjects are yielded from one command to the next, and a lot of helpful Cypress code runs between each command to ensure everything is in order.
它们不会像您期望的那样同步 return 对象,需要一些时间来适应 - 但一旦你掌握了它,你就会掌握它。
这不会像您预期的那样工作,因为 cy.get
是异步的,所以在您调用 .click()
之前不会设置 myButton:
var myButton = cy.get(#myButton);
myButton.click(); //Nope!
Cypress 命令将对象生成给 Chainers,这些对象排队要执行的操作,因此您可以像这样使用它:
cy.get(#myButton).click(); //Yes!
或:
cy.get(#myButton).then(myButton => {
// do stuff with myButton here
});
补充阅读:https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Return-Values
我正在了解 Cypress.io 并在他们的官方网站上看到了这个声明 Cypress.io
"Cypress commands do not return their subjects, they yield them."
cypress.io中的命令"yield"和"return"有什么区别?
我认为 yield 的意思是 'pay it forward',而 return = 'pay it back'。
Cypress commands are asynchronous and get queued for execution at a later time. During execution, subjects are yielded from one command to the next, and a lot of helpful Cypress code runs between each command to ensure everything is in order.
它们不会像您期望的那样同步 return 对象,需要一些时间来适应 - 但一旦你掌握了它,你就会掌握它。
这不会像您预期的那样工作,因为 cy.get
是异步的,所以在您调用 .click()
之前不会设置 myButton:
var myButton = cy.get(#myButton);
myButton.click(); //Nope!
Cypress 命令将对象生成给 Chainers,这些对象排队要执行的操作,因此您可以像这样使用它:
cy.get(#myButton).click(); //Yes!
或:
cy.get(#myButton).then(myButton => {
// do stuff with myButton here
});
补充阅读:https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Return-Values