Cypress is having trouble loading a particular URL

I'm encountering a timeout error while trying to load a specific URL using Cypress. Even after setting the page load time to 2 minutes, the issue persists. Interestingly, general URLs like https://www.google.co.nz/ load without any problems.

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

Answer №1

One possible approach, though not perfect, could use some enhancements...

The Countdown site does not work well within an iframe, but it can still be tested in a child window using a custom command described here

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)

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

You can test it like this:

cy.openWindow('https://shop.countdown.co.nz/').then(() => {
  cy.contains('Recipes').click()
  cy.contains('Saved Recipes', {timeout:10000})  // check for navigation completion
})

I adjusted the setTimeout() in the custom command to 10 seconds, as the site tends to load slowly.

Configuration:

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

https://i.sstatic.net/8khPM.png


Error with Command Timeout

When using Gleb's child window command, there is a timeout error that seems difficult to pinpoint.

To overcome this, I included

"defaultCommandTimeout": 20000
in the configuration, but a more efficient approach would be to remove the global setting and utilize this method instead:

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

To verify that the extended command timeout only applies once, intentionally delay one of the inner test commands and confirm it times out after the standard 4000 ms timeframe.

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

Answer №2

I encountered a similar issue where the URL was not being added to the iframe's src property, causing timeouts with cy.visit().

To address this, I manually set the URL in the src property of the iframe using a custom command:

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

You can now navigate to a specific URL using cy.goto('https://yoururl.com') without any issues.

Answer №3

The quotation marks are incorrect. Please use the following code snippet:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to prevent selecting future dates in Material UI date picker

Is there a way to prevent selecting future dates in the material UI datepicker? I noticed that it doesn't seem to have any prop options like disableFuture or past. For those interested, here's the link to the github repository sandboxlink ...

What is preventing me from altering the array one element at a time?

I am working with an array and a class let questions = [ { questionText: '', answerOptions: [], }, ]; class Questions { constructor(questionText,answerOptions) { this.questionText = questionText; this.answerOptio ...

Changing the background color of a page to match the background color of a button in React, which can be updated at any time

I have a special button called ArbitraryBtn that, when clicked, changes the page's background color to random colors: import React from 'react'; export const changeToArbitraryColor = () => (document.body.style.backgroundColor = ...

Unlocking the secrets of integrating Vuex store with JavaScript/TypeScript modules: A comprehensive guide

I am working on a vue application and I have a query. How can I access the store from javascript/typescript modules files using import/export? For example, if I create an auth-module that exports state, actions, mutations: export const auth = { namesp ...

Run JavaScript code whenever the table is modified

I have a dynamic table that loads data asynchronously, and I am looking for a way to trigger a function every time the content of the table changes - whether it's new data being added or modifications to existing data. Is there a method to achieve th ...

Show data from a Node.js server in its original format within an AngularJS application

Currently, I am using the angular fullstack generator to develop a web application. One issue I am facing is sending file data from the Node.js server to display on the front end. The problem arises because the data is being sent in an unformatted manner, ...

What are the best practices for dynamically handling variables in JavaScript?

I am looking to dynamically work with variables and store their references in a collection. My first question is: How can I save a variable reference (not its value) in a collection? For example: var apple = "A delicious fruit"; var banana = "A yellow f ...

Having trouble properly sending gender value through javascript/ajax functionality

Within my database, the gender_id attribute is configured as an enum with options ('M', 'F') and M serves as the default selection. Gender Selection Form <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12"> <label>Gend ...

Retrieve data from a variable that is located within a function that is also

<script> const tally ={ total: 0, increase: function(){ total++; console.log(total); } } const selectBtn = document.getElementsByTagName('button& ...

Electron's Express.js server waits for MongoDB to be ready before executing queries

As I work on a demo application, Express serves some React code that interacts with a MongoDB database hosted on mLab. The data is retrieved using SuperAgent calls in my main React code loaded via index.html. While everything works fine when starting the ...

The date displayed in moment.js remains static even after submitting a new transaction, as it continues to hold onto the previous date until

I am currently utilizing moment.js for date formatting and storing it in the database This is the schema code that I have implemented: const Schema = new mongoose.Schema({ transactionTime: { type: Date, default: moment().toDate(), ...

Is it possible to rewrite this function recursively for a more polished outcome?

The function match assigns a true or false value to an attribute (collapsed) based on the value of a string: function match(children) { var data = $scope.treeData for (var i = 0; i < data.length; i++) { var s = data[i] for (var ...

How to dynamically delete React Router Link components after they have been clicked on?

When using React Router, how do I remove the div that contains a Link component or the Link component itself when it is clicked on and the routing is complete? For instance, consider an app structured in the following way: ==Header== ==Link1 Link2== Onc ...

Axios Instance class request cancellation

In my Redux-saga project, I am working on implementing a polling function where I need to make a request every second. If there is no response from the endpoint, I want to cancel the previous request and initiate a new one using Axios client. I have organi ...

Exploring the World of jQuery Caching and Navigating Through Objects

I'm interested in learning more about jQuery caching and how it can enhance performance. Can you explain how to properly utilize this technique? From what I understand, when using a jQuery selector, you're essentially searching the DOM to create ...

Preventing a user from navigating away from a page without completing a specific action, such as clicking a submit button

I am in the process of developing an interactive quiz platform. The quiz includes a timer that begins counting down once the user initiates the quiz. Upon completing the quiz, the user is expected to submit their answers. If the user runs out of time, th ...

The jQuery .load method is having trouble loading a local HTML file within a Cocoa WebView

Having an issue with my Cocoa WebView where I'm attempting to load a local HTML file using jQuery.load(). My code snippet looks like this: $("#mydiv").load("test.html"); // The test.html file is located within the app's bundle Although no exce ...

Utilize React to Effectively Control Multiple Audio Files on a Single Page, Ensuring Only One Plays at a Time with Function

I am currently working on a new React app that includes multiple music cards on the same page. I am looking to modify the state of the previous card before updating the state of the new card. Essentially, I aim to pause the audio playing in the previous ca ...

What is the best way to play a random song when the user clicks a button?

My goal is to develop a website where users can click on an image and have a random song play from a playlist. I currently have a functioning code that activates one song, but it fails when adding multiple songs to the mix. <html> <head> ...

Updates to $scope are not reflecting in the application

My input includes a datalist that is populated by an angular get request as the page loads. <input list="data" /> <datalist id="data"> <option ng-repeat="item in items" value="{{item.data}}"> </datalist> The $http call is straig ...