最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to load a specific URL with Cypress - Stack Overflow

programmeradmin1浏览0评论

I’m unable to load the following URL with Cypress. Getting timeout error. I have set the page load time to 2 mins, still same issue. General URLs eg. / works fine.

it('First Test', () => {
  cy.visit('/')
})

I’m unable to load the following URL with Cypress. Getting timeout error. I have set the page load time to 2 mins, still same issue. General URLs eg. https://www.google.co.nz/ works fine.

it('First Test', () => {
  cy.visit('https://shop.countdown.co.nz/')
})
Share Improve this question edited Jun 9, 2023 at 6:02 Lisa La 552 silver badges11 bronze badges asked Feb 24, 2022 at 10:37 HarryHarry 171 silver badge3 bronze badges 3
  • 3 Unrelated I guess, but only use regular quotes in code (' or ") – user5734311 Commented Feb 24, 2022 at 10:39
  • 1 Can you edit your post to make a minimal reproducible example ? – Elikill58 Commented Feb 24, 2022 at 10:39
  • @Elikill58 They can't, it's node code – user5734311 Commented Feb 24, 2022 at 10:40
Add a ment  | 

3 Answers 3

Reset to default 9

Here's a way, not the best, could be improved...

The Countdown site has an aversion to being run in an iframe, but it can be tested in a child window, see custom mand here 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/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)
  })
})

Can test with that like this

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

I bumped the setTimeout() in custom mand to 10 seconds, cause this site drags it's feet a bit.

Configuration:

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


Command timeout error

Using Gleb's child window mand, there's a timeout error that I can't track the source of.

To avoid it I set "defaultCommandTimeout": 20000 in config, but since it's only needed for the openWindow call it's better to remove the global setting and use this instead

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
  })
})

To check if the long mand timeout only applies once, break one of the inner test mands and check that that it times out in the standard 4000 ms.

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

I had a similar issue, so what I observed in my case was that the URL was not getting added to the iframe src property and hence cy.visit() was getting timed out each time.

So, I added the URL manually to the src property of the iframe. Here's my custom mand for reference:

Cypress.Commands.add('goto', url => {
  return new Promise(res => {
    setTimeout(() => {
      const frame = window.top.document.getElementsByClassName('aut-iframe')[0];
      frame.src = url;
      var evt = window.top.document.createEvent('Event');
      evt.initEvent('load', false, false);
      window.dispatchEvent(evt);
      res();
    }, 300);
  });
});

Now use cy.goto('https://yoururl.') and you are good to go.

The quotes are wrong. Try the below code:

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

发布评论

评论列表(0)

  1. 暂无评论