首页 > 解决方案 > 根据请求的响应跳过 cypress 测试

问题描述

我需要一种基于对 IF/ELSE 条件的评估来跳过赛普拉斯中的测试块的方法。下面是我的代码

it("Website testing",function(){
        cy.request({
            method: 'GET',
            url: "http://127.0.0.1:5000/isIPv6",
            timeout:300000
        }).then(network_result => {
            cy.log(network_result.body)
            if(network_result.body.isIPv6 == false)
            {
               statements
            }
            else
            {
                cy.log("IPv6 device, stopping the test")
                this.skip()
             }
         })
})

上面的代码片段之所以起作用,是因为this.skip()它是一个同步语句,而赛普拉斯为此给出了错误。

我也试过it.skip()throw()但在这种情况下它没有用。我需要一种方法,当执行控制进入 else 块时,我可以跳过测试执行/块,并将测试发送到已跳过/挂起状态。

标签: javascriptautomationautomated-testscypress

解决方案


我看到你有it(title, function() {...}允许访问和修改的模式this,但你可能还需要将它应用于回调.then()

it("Website testing", function() {
  cy.request({
    ...
  }).then(function(network_result) {
    ...
    else {
      ...
      this.skip()
    }
  })
})

刚刚注意到在skip()里面it(),但你应该在开始之前跳过测试it(),所以可能

beforeEach(function() {
  cy.request({
    ...
  }).then(function(network_result) {
    ...
    else {
      ...
      this.skip()
    }
  })
})

it('skipped if the beforeEach calls "this.skip()"', () => {
  ...
})

推荐阅读