簡體   English   中英

賽普拉斯測試掛起自定義命令,但如果在 PO 文件中使用 function 則不會 - 我錯了什么?

[英]Cypress test hangs with custom command but not if using function in PO file - what am I getting wrong?

我對賽普拉斯還是有點陌生,所以請原諒任何菜鳥的無知 - 但是......

我正在使用頁面對象,並嘗試使用我編寫的自定義命令作為斷言的一種包裝器。 此自定義命令將用於多個測試和多個頁面 object 文件,因此我不想在每個頁面 object 文件中復制它的代碼。 但是,如果我將完全相同的代碼放入頁面 object 定義中,它可以正常工作,如果我將它用作自定義命令,則測試掛起(經過的時間只會上升和上升。但沒有任何反應)..:我在做什么:

myPage.myFirstElement.click()
myPage.mySecondElement.type('something')
myPage.want(myPage.ThirdElement, 'to equal', 'Fred') // want is the name of my custom command / function in the PO file

以上工作。 但是,如果我將“想要”移動到自定義命令:

myPage.myFirstElement.click()
myPage.mySecondElement.type('something')
cy.want(myPage.ThirdElement, 'to equal', 'Fred') // want is the name of my custom command / function in the PO file

如上所述,這會掛起。 我嘗試在我的自定義命令定義中設置 prevSubject,但無濟於事。 我也試過使用.then(...),也無濟於事。

我未能正確發現/理解什么?

謝謝:)

//編輯:“想要”的代碼 function 根據要求。 這就像在我的 po 文件中一樣。 當一個自定義命令時,它的內容是相同的,除了聲明當然是 "Cypress.Commands.Add('want', (val1, op, val2) => { " 等等......

    want(val1, op, val2) {

        function getContent(obj, op) {
            if ((op.indexOf('exist') >= 0) || (op.indexOf('visible') >= 0) || (typeof obj != 'object')) {
                return obj
            }
            return obj === null ? obj : obj.attr('value') === undefined ? obj.text() : obj.attr('value');
        }

        function compare(first, second) {
            op = op.split(' ').join('.')
            var cmp1 = getContent(first, op);
            var cmp2 = getContent(second, op);
            var assertFn = (op.indexOf('exist') >= 0) || (op.indexOf('visible') >= 0) ? 'expect' : 'softExpect'
            if (cmp2 === undefined) {
                var chaiAssertion = new Function('a', 'return chai.' + assertFn + '(a).' + op)
                chaiAssertion(cmp1)
                //.then({timeout: Cypress.config('responseTimeout')}, () => { return originalFn(cmp1)})
            } else {
                var chaiAssertion = new Function('a, b', 'return chai.' + assertFn + '(a).' + op + '(b)')
                chaiAssertion(cmp1, cmp2)
            }
        }

        if (typeof (val1) === 'object' && val1 != null) {
            val1.should(($v1) => {
                if (typeof (val2) === 'object' && val2 != null) {
                    val2.should(($v2) => {
                        compare($v1, $v2)
                    })
                }
                else {
                    compare($v1, val2)
                }
            })
        }
        else {
            if (typeof (val2) === 'object' && val2 != null) {
                val2.should(($v2) => {
                    compare(val1, $v2)
                })
            }
            else {
                compare(val1, val2)
            }
        }
    }

如果您創建了自定義命令,這意味着您必須像 cypress 中的任何其他命令一樣使用:cy.[command]

所以它應該與:

cy.want(myPage.ThirdElement, 'to equal', 'Fred') // want is the name of my custom command / function in the PO file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM