比较两个值并在 Cypress 中做出决定

标签 compare ui-automation cypress

因此,我需要比较页面上的两个值,并根据结果执行一些操作。

//First Block
cy.get('selctor1').invoke('text').then(somevalue => {
    cy.get('selector2').should('have.text', somevalue).then(() => {
        #Do Something when equal
    })
})

//Second Block
cy.get('selctor1').invoke('text').then(somevalue => {
    cy.get('selector2').should('not.have.text', somevalue).then(() => {
        #Do Something when not equal
    })
})

因此,对于两个值相等的积极情况,一切正常。但是对于两个值不相等的情况,它只检查第一个块并失败。当值不相等而不是第一个块时,我应该怎么做才能执行第二个块?

最佳答案

抱歉第一次没说清楚。这是我编辑的答案:

然后 vs 应该 :

尽量避免 then在可能的情况。 then不可重复,并且会引入意外行为。
而且还会should引入意外行为。

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)
        });
    })
})

在本例中,您会看到两次相同的代码,但第一个块使用 should而第二个块使用 then .断言必须失败,但在第一个块中,断言被重复。打开 DEV 控制台以查看第一个块的多次重试,但第二个块中没有重试。

这就是我所说的“意外”行为。比方说,你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 (然后 ) the UI acton must be executed very fast and before the然后 is executed so that the期望`不会失败。

should在这种情况下,在断言最终失败之前,您还有 4 秒(如果`defaultCommandTimeout 未被覆盖)。

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");
            })
    })
})

你会期待什么?绿色测试?不,此测试失败:

enter image description here

为什么会这样?因为 get引入了一个隐式断言“应该存在”(参见: https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Default-Assertions )。
Should with callback 跳过默认断言(参见:https://docs.cypress.io/api/commands/should.html#Notes)。我认为他们通过按标志切换来跳过它。这可能会再次反转标志,从而强制 cypress 检查“asdad”是否确实存在,即使我们使用 should not exist .

这个东西有问题:https://github.com/cypress-io/cypress/issues/5963

不知道为什么cy.log有你在案例中提到的行为。所以要么你使用 then如果您想使用 cy然后回调中的命令或避免使用 cy命令和使用 should带有显式断言 ( expect )。也许在那个问题解决后,cy.log也可以使用。

旧答案 :
    cy.get('selctor1').invoke('text').should(someValue => {
        const $el = Cypress.$('selector2');

        if ($el.text() ==== someValue) {
            // positive
            expect()....
        } else {
            // negative
            expect()....
        }
    })

您可以使用 should带有回调。只要达到超时或没有断言失败,就会执行此回调(以及之前的 invoke 命令)。
您始终可以使用原始 jQuery 对象进行处理。这取决于您是否需要 Cypress 在 get() 期间执行的所有检查。 .

如果您需要进一步的帮助,请告诉我。

关于比较两个值并在 Cypress 中做出决定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59317965/

相关文章:

go - go中如何比较两个模板,例子得到了意想不到的结果

angular - _angular_core__WEBPACK_IMPORTED_MODULE_1__ ["ɵvid"] 不是一个函数

java - Hibernate 5 无法比较字符串和 int

android - UI Automator Viewer 的 GUI 损坏

java - Selenium Java - 如何选择没有 ID 的下拉元素

Android UIAutomation Instrumentation NoSuchMethod异常

javascript - 无法使用 Cypress 获取 vuex 状态

typescript - Cypress 在 Jest 断言中导致类型错误

c# - 在 C# 中比较二进制文件

c++ - 如何比较来自用户c++的两个整数的每个数字