【问题标题】:Cypress intercept with not condition没有条件的赛普拉斯拦截
【发布时间】:2021-09-06 09:43:42
【问题描述】:

我在单击元素时会调用以下服务。我需要拦截端点中没有f0000000000 的请求。

https://example.com/f0000000000/path1 - GET
https://example.com/f0000000000/path1 - GET
https://example.com/f0000000000/path1 - GET
https://example.com/f0000000000/path1 - GET
https://example.com/f0000000000/path1 - GET
https://example.com/f0000000000/path1 - GET
https://example.com/f2021090606/path1 - GET

我们如何在 cypress 中实现这一点?我们有类似的选择吗?

 cy.intercept("GET", "**/NOT(f0000000000)/path1*").as('getForecast');
 cy.get('#some').click();
 cy.wait('@getForecast').its('response.statusCode').should('eq', 200)
});

【问题讨论】:

    标签: cypress


    【解决方案1】:

    使用正则表达式

    您将要排除的部分包装在负前瞻组中,

    (?!NOT-THE-TEXT-YOU-ARE-LOOKING-FOR)
    

    但也必须为该位置的任何其他文本添加通配符在组之后,即.*

    ((?!NOT-THE-TEXT-YOU-ARE-LOOKING-FOR).*)
    

    最短

    • 确保任何域,但必须有/path1,而不是f0000000000

      const regex = /((?!f0000000000).*)\/path1/
      cy.intercept(regex, {}).as('intercept')
      cy.wait('@intercept')
      

    更准确

    • 更准确地指定域

      cy.intercept(/^https:\/\/example\.com\/((?!f0000000000).*)\/path1/, {}).as('intercept')
      cy.wait('@intercept')
      

    • 更准确地指定数字

      如果您总是f 前缀并且只想要数字通配符,请将 f 移到排除项之外。

      可选地将.* 替换为[0-9]{10} 以指定恰好10 位与0000000000 不匹配的数字

      cy.intercept(/^https:\/\/example\.com\/f((?!0000000000)[0-9]*)\/path1/, {}).as('intercept')
      cy.wait('@intercept')
      

    带迷你匹配

    只需将NOT(f0000000000) 更改为!(f0000000000)

    const url1 = 'https://example.com/f2021090606/path1x'
    const match1 = Cypress.minimatch(url1, '**/!(f0000000000)/path1*')
    expect(match1).to.eq(true)
    

    相反

    const url2 = 'https://example.com/f0000000000/path1x'
    const match2 = Cypress.minimatch(url2, '**/!(f0000000000)/path1*')
    expect(match2).to.eq(false)
    

    拦截

    cy.intercept('**/!(f0000000000)/path1*', {}).as('intercept')
    
    //or
    
    cy.intercept('**/f!(0000000000)/path1*', {}).as('intercept')
    

    但要小心尾随路径部分

    const url = 'https://example.com/f0000000001/path1/x'
    const match = Cypress.minimatch(url, '**/!(f0000000000)/path1/*')
    expect(match).to.eq(true)
    

    【讨论】:

      【解决方案2】:

      您可以为此创建一个正则表达式:

      cy.intercept(/^(?!.*f0000000000\/path1).*$/gm).as('getForecast')
      

      这将拦截没有f0000000000 的url。这是一个非常基本的正则表达式,您可以随时根据需要对其进行增强。

      您也可以查看Intercept Cypress Recipe

      【讨论】:

        猜你喜欢
        • 2021-10-09
        • 1970-01-01
        • 1970-01-01
        • 2021-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        相关资源
        最近更新 更多