代码之家  ›  专栏  ›  技术社区  ›  soeik

为什么cypress中的存根不能用于加载页面后调用的路由

  •  1
  • soeik  · 技术社区  · 7 年前

    问题是:

    beforeEach(function () {
      cy.server()
      cy.route('GET', '/favourites?funcName=columnPreset', []).as('columnPresetEmpty')
      cy.visit('#/search')
    })
    

    如果在页面加载时发生http请求,则存根工作正常。 cy.wait('@columnPresetEmpty')

     it('does not work', function () {
       cy.get('[data-test=button-gridSettings]').click()
       cy.wait('@columnPresetEmpty')
     })
    

    同时,在其他测试中,我有几乎类似的功能,只需单击按钮即可执行请求,而无需打开新的模式窗口。这是唯一的区别。

    我做错了什么?

    3 回复  |  直到 7 年前
        1
  •  4
  •   gabberr    7 年前

    问题可能是cypress还不能完全处理 fetch

    cy.visit('#/search', {
        onBeforeLoad: (win) => {
            win.fetch = null
        }
    })
    

    更多信息请阅读: https://github.com/cypress-io/cypress/issues/95#issuecomment-281273126

        2
  •  1
  •   soeik    7 年前

    我找到了导致这种行为的原因。问题不在于模式窗口本身,而是在promise对另一个请求的回调中调用了执行第二个请求的代码。比如:

    fetch('/initData')
      .then(loadView)
    

    并在第二次提取时执行loadView函数。

        3
  •  0
  •   user8745435 user8745435    7 年前

    关于信息,我在我的搜索模式(在Vue应用程序中)上试用过,效果不错。

    我所做的:

    • test-get-in-modal.txt 在应用程序的静态文件夹中

    • http.get('test-get-in-modal.txt') 在模态代码中,因此它仅在模态打开后运行

    • cy.server() , cy.route('GET', 'test-get-in-modal.txt', []).as('testGetInModal') 在一个 before()

    • it() 补充 cy.wait('@testGetInModal') 哪个成功了

    • 改为 cy.route('GET', 'not-the-file-you-are-looking-for.txt'... ,按预期失败


    cy.visit() 前一页