无法使用Cypress加载特定URL

我无法使用Cypress加载以下URL。获取超时错误。我已经将页面加载时间设置为2分钟,仍然存在相同的问题。通用URL,例如。(https://www.google.co.nz/)工作正常。

it(‘First Test’, () => {
  cy.visit(‘https://shop.countdown.co.nz/‘)
})

解决方案

以下是可以改进的方法,但不是最好的方法...

Countdown站点不喜欢在IFRAME中运行,但它可以在子窗口中测试,请参阅此处的自定义命令Cypress using child window

Cypress.Commands.add('openWindow', (url, features) => {
  const w = Cypress.config('viewportWidth')
  const h = Cypress.config('viewportHeight')
  if (!features) {
    features = `width=${w}, height=${h}`
  }
  console.log('openWindow %s "%s"', url, features)

  return new Promise(resolve => {
    if (window.top.aut) {
      console.log('window exists already')
      window.top.aut.close()
    }
    // https://developer.mozilla.org/en-US/docs/Web/API/Window/open
    window.top.aut = window.top.open(url, 'aut', features)

    // letting page enough time to load and set "document.domain = localhost"
    // so we can access it
    setTimeout(() => {
      cy.state('document', window.top.aut.document)
      cy.state('window', window.top.aut)
      resolve()
    }, 10000)
  })
})

可以像这样测试

cy.openWindow('https://shop.countdown.co.nz/').then(() => {
  cy.contains('Recipes').click()
  cy.contains('Saved Recipes', {timeout:10000})  // if this is there, have navigated
})

我将setTimeout()in自定义命令改为10秒,导致此站点有点拖拉。

配置:

// cypress.json
{
  "baseUrl": "https://shop.countdown.co.nz/",
  "chromeWebSecurity": false,
  "defaultCommandTimeout": 20000       // see below for better way
}


命令超时错误

使用Gleb的子窗口命令时,出现我无法跟踪其来源的超时错误。

为避免这种情况,我在配置中设置了"defaultCommandTimeout": 20000,但由于它只在openWindow调用时才需要,因此最好删除全局设置而改用它

cy.then({timeout:20000}, () => {
  cy.openWindow('https://shop.countdown.co.nz/', {}).then(() => {
    cy.contains('Recipes').click()
    cy.contains('Saved Recipes', {timeout:10000})  // if this is there, have navigated
  })
})

若要检查长命令超时是否仅适用一次,请中断其中一个内部测试命令并检查它是否在标准4000毫秒内超时。

cy.then({timeout:20000}, () => {
  cy.openWindow('https://shop.countdown.co.nz/', {}).then(() => {
    cy.contains('Will not find this').click()  // Timed out retrying after 4000ms

相关文章