【问题标题】:Break if/else block without exception Cypress无一例外地中断 if/else 块 Cypress
【发布时间】:2021-06-16 12:47:38
【问题描述】:

我需要一种方法在代码 sn-p 下成功运行,但有任何异常

it("Visiting Google",function(){
    var webUrl = 'https://html5test.com/'
    cy.visit(webUrl)
    var iterator = 1
    while(iterator < 5)
    {
        const $select_element = cy.get('h1 > em')
        if($select_element.contains('abcd'))
        {
            cy.log("string found")
        }
        else
        {
            cy.log('string not found')
            break
        }
        iterator += 1
    }
})

我需要这个测试用例来运行 while 循环,直到 while 中的条件失败,或者如果 case 条件失败并且控制进入 else 块

【问题讨论】:

    标签: javascript jquery automation cypress


    【解决方案1】:

    语句$select_element.contains('abcd') 不会像您的代码想要的那样返回真/假。如果没有找到该元素,cypress 将失败并停止。

    您需要做的是从元素中获取值,并对其进行比较。这是使用闭包(回调)的方法

    cy.get('h1 > em').then(($select_element) => {
        if($select_element.text().match('abcd')) {
            cy.log("string found")
        } else ...
    })
    

    但是因为它在回调中,所以它是异步的,并且不会在你拥有的循环中工作。

    【讨论】:

    • 这是来自同一作者的重复问题,请参阅Exit from while loop with if/else in Cypress
    • @phileinSophos - Mikkel 刚刚告诉你为什么因为它在回调中,它是异步的,并且不会在你的循环中工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多