本文介绍了比较两个值并在赛普拉斯中做出决定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 因此我在页面上有两个值需要比较,并根据结果执行一些操作。So I have two values on a page that I need to compare and as per the result perform some actions.//First Blockcy.get('selctor1').invoke('text').then(somevalue => { cy.get('selector2').should('have.text', somevalue).then(() => { #Do Something when equal })})//Second Blockcy.get('selctor1').invoke('text').then(somevalue => { cy.get('selector2').should('not.have.text', somevalue).then(() => { #Do Something when not equal })})因此对于肯定值,当两个值相等时,一切正常。但是,对于两个值不相等的情况,它仅检查第一个块并失败。我应该怎么做才能在值不相等而不是第一个块时执行第二个块?So for the positive case when both values are equal everything works fine. But for the case when two values are not equal, it's only checking the first block and fails. What should I do so that it executes the second block when values are not equal and not the first block?推荐答案对不起第一次清楚。这是我编辑过的答案:Sorry for not being clear the first time. Here is my edited answer: 然后vs应该:尽量避免然后。 然后不可重复,并且会引入意外行为。 但也应该引入意外的行为。Try to avoid then where possible. then is not repeatable and will introduce unexpected behaviour.But also will should introduce unexpeced behaviour. 错误使用然后 :Example for a bad usage of then:describe("asd", () => { it("fails but retries", () =>{ console.log("######### first test") cy.wrap({ fn: () => console.log(new Date())}) .invoke("fn") .should(r => expect(r).to.eq(true)); }) it("fails but retries not", () =>{ console.log("######### next test") cy.wrap({ fn: () => console.log(new Date())}) .invoke("fn") .then(r => { expect(r).to.eq(true) }); })})在此示例中,您看到两次相同的代码,但第一个块使用应该,而第二个块使用然后。断言必须失败,但是在第一个块中,将重复断言。打开DEV COnsole,可以看到第一个块重试了很多,而第二个块没有重试。 In this example you see the same code twice but the first block uses should while the second block uses then. The assertion must fail but in the first block, the assertion is repeated. Open the DEV COnsole to see many retries for the first block but no retry in the second. 我的意思是意外行为。假设您包装了一个动态扩展的对象(也许通过UI动作),并且期望该对象具有属性。在第二个块( then )中,必须非常快速地执行UI acton,并且在执行 then 之前, expect`不会失败。This is what I mean by "unexpected" behaviour. Let's say, you wrap a object that is dynamically extended (maybe by a UI action) and you are expecting a property on this object. In the second block (then) the UI acton must be executed very fast and before thethenis executed so that theexpect` does not fail.在应该的情况下,您有4秒钟(如果`defaultCommandTimeout不会被覆盖)直到断言将最终失败。In the should case, you have 4 seconds (in case of `defaultCommandTimeout is not overwritten) left until the assert will finally fail. 应该严重使用 : Bad usage of should:describe("ad", () => { it("test", () => { cy.visit("https://www.cypress.io/") cy.get("*[aria-label='pricing']") .invoke('text').should(someValue => { cy.get("asdad", {timeout: 5000}).should("not.exist"); }) })})您期望什么?绿色测试?不,该测试失败:What would you expect? A green test? No, this test fails: 为什么这样吗因为 get 引入了隐式断言应该存在(请参阅: https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Default-Assertions )。 Why is this the case? Because get introduces an implicit assert "should exist" (see: https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Default-Assertions ). 应该带回调会跳过默认断言(请参阅: https://docs.cypress.io/api/commands/should.html#Notes )。我认为他们跳过了通过标志切换它。这可能会导致标志再次反转,从而迫使赛普拉斯即使我们使用不应存在 Should with callback skips the default assertion (see: https://docs.cypress.io/api/commands/should.html#Notes ).I think they skip it by toggling it by flag. This could have the effect of reversing the flag again and thus forces cypress to check if "asdad" does exist even though we use should not exist.这个东西有问题: https:// github.com/cypress-io/cypress/issues/5963 我不知道为什么 cy.log 具有您在案例中提到的行为。因此,如果您想在回叫中使用 cy 命令,然后使用 then ,或者避免使用 cy 命令,并在显式断言(期望)中使用应该。也许在该问题解决之后,也可以使用 cy.log 。I do not know why cy.log has the behaviour you mentioned in your case. So either you use then if you want to use cy commands within then callback or you avoid the usage of cy commands and use should with explicit assertions (expect). Maybe after that issue is fixed, cy.log also can be used. 旧答案: cy.get('selctor1').invoke('text').should(someValue => { const $el = Cypress.$('selector2'); if ($el.text() ==== someValue) { // positive expect().... } else { // negative expect().... } })您可以将应该与回调一起使用。只要达到超时或没有断言失败,就会执行此回调(和先前的 invoke 命令)。 您始终可以使用原始jQuery对象进行处理。这取决于您是否需要cypress在 get()期间执行的所有检查。You can use should with a callback. This callback (and the previous invoke command) is executed as long as the timeout is reached or no assertion fails.You always can use the raw jQuery object to work with. This depends on whether or not you need all the checks cypress is executing during a get().请让我知道您是否需要进一步的帮助。Please let me know if you need further assistance. 这篇关于比较两个值并在赛普拉斯中做出决定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 17:50