Utilizing the invoke() method within the each() function in Cypress to access the href attribute

I recently started using Cypress and I'm attempting to retrieve the href attribute for each div tag within a group by utilizing invoke(), however, it is resulting in an error. Could anyone provide guidance on the correct way to achieve this?

cy.get('.bms-scoreboard__game-tile--mls').each(($el,index,$list) => {
            $el.get('a')
                .invoke('attr','href')
                .then(href => {
                    cy.request(href)
                        .its('status')
                        .should('eq',200)
                })
        })

Answer №1

It appears that using .get() may not be the best approach here as it only functions within the <body> tag and not within each instance of

'.bms-scoreboard__game-tile--mls'
.

You might want to consider utilizing .find() instead.

Here is an example using jQuery operators:

cy.get('.bms-scoreboard__game-tile--mls')
  .each(($el,index,$list) => {
    const href = $el.find('a').attr('href')
    cy.request(href)
      .its('status')
      .should('eq', 200)
  })
})

Alternatively, you can achieve this using Cypress operators:

cy.get('.bms-scoreboard__game-tile--mls')
  .each(($el,index,$list) => {
    cy.wrap($el).find('a')
      .invoke('attr','href')
      .then(href => {
        cy.request(href)
           .its('status')
           .should('eq',200)
      })
  })
})

Another option is to include "find" in the initial selector:

cy.get('.bms-scoreboard__game-tile--mls a')
  .each($a => {
    const href = $a.attr('href')
    cy.request(href)
      .its('status')
      .should('eq', 200)
  })
})

Answer №2

$el represents a JQuery element and is not part of the Cypress chain. To incorporate it into the Cypress chain, you must utilize cy.wrap().

cy.get('.bms-scoreboard__game-tile--mls').each(($el,index,$list) => {
            cy.wrap($el)
                .get('a')
                .invoke('attr','href')
                .then(href => {
                    cy.request(href)
                        .its('status')
                        .should('eq',200)
                })
        })

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

The functionality of v-tooltip ceases to operate when the element is deactivated

<button v-tooltip="'text'" :disabled=true>Some button</button> Can you provide an explanation for why the button is disabled without disabling the tooltip as well? ...

I am unable to modify the local JSON file during runtime

Is there a way to avoid bundling a local JSON file in my app? I am able to import and use it during development, but once the app is built, the file gets bundled and I can no longer access or modify it. I need the JSON file to remain separate so that I ca ...

Prevent the upward arrow from appearing on the first row and the downward arrow from appearing on the last row when

This is a straightforward question, but I'm unsure how to achieve it. I am currently working on sorting columns using jQuery. $(document).ready(function(){ $(".up,.down,.top,.bottom").click(function(){ var row = $(this).parents("tr:f ...

Generating a new array and merging different sets of keys together

Is there a way to modify how items are added to a jQuery array? Here is the current code snippet in use: var sub_updated = []; $('.current-sub-items').each(function() { $(this).find('.prod-select').each(function() { if ($(th ...

Send error messages directly to the client side or retrieve status codes and messages

When responding to an AJAX request, encountering an app error like data validation failure can be tricky. How can we effectively communicate this to the user? 1. Returning a Status Code and Fetching Message with JS $.ajax(options).done(function(response) ...

Determining Velocity of an Object in Three.js

Can anyone help me figure out how to calculate an object's velocity in three.js? I've checked the Object3D documentation but can't seem to find anything related to velocity. Appreciate any assistance, ...

unable to locate the sw.js file in the Vue plugin PWA

My experience with using the vue PWA plugin has been positive so far. I have one app that works great with it. The configuration for this app in VueJS looks like this: const WebpackNotifierPlugin = require('webpack-notifier') module.exports = { ...

Transfer the html div element to the ajax request

My server-side code runs multiple bigquery queries and organizes the results in a table. The client calls this code via an ajax request. I want to send the table/div generated by the server-side code to the client so that it can be rendered there. Is this ...

Encountering a ReferenceError while attempting to implement logic on a newly created page

I've been experimenting with building a website using the Fresh framework. My goal was to add a simple drop-down feature for a button within a navigation bar, but I'm struggling to figure out where to place the necessary code. I attempted creatin ...

What is the best way to convert the information from a <SelectInput /> component or similar components into another language?

Within my React admin v3 application, When retrieving data from the servers for my entity, I receive a unique identifier called a slug. This slug is a special key that needs to be translated on the client side. Here is an example of my <CallMeBackCre ...

Is it possible to refresh an iframe with PHP once a certain condition is satisfied?

I have a situation with two pages - one is my PHP script and the other is an HTML page containing two iframes. > <html> > > <iframe name=1></iframe> <iframe name=2></iframe> > > </html> Currently, ifram ...

Updating a nested property within an array of objects in MongoDB

Storing grades for an online education application using MongoDB. Here is a sample classRoom document stored in my mongoDB database. StudentGradeObjs are kept in an array within a GradeObject. GradeObjs are stored in an array of GradeObjects inside a class ...

Unable to insert rows into an array using sqlite3 in Node.js

I have a snippet of Node.js code that queries a SQLite Database and prints each row individually. The original code is as follows: var sqlite3=require('sqlite3').verbose(); var db=new sqlite3.Database('./database.db',(err)=>{ i ...

Display a single video on an HTML page in sequential order

My webpage contains a video tag for playing online videos. Instead of just one, I have utilized two video tags. However, when attempting to play both videos simultaneously, they end up playing at the same time. I am seeking a solution where if I start pla ...

Rearranging a JSON Object post editing

Currently, I am working on a project where I need to display and sort a list of items based on a JSON object. This includes sorting by both string values and integers within the object. So far, I have been successful in listing and sorting the items by st ...

What is the best way to update image CSS following a rotation using JavaScript?

Discover a neat CSS trick for always centering an image in a div, regardless of its size or aspect ratio. <style> .img_wrap { padding-bottom: 56.25%; width: 100%; position: relative; overflow: hidden; } #imgpreview { display: bl ...

Is it recommended to incorporate "return" in my callback function when coding in JavaScript?

Utilizing asynchronous functions in my JS application, I've encapsulated them within my own functions that take callback inputs. One question that I have is whether or not it's necessary to use the "return" keyword when calling the callback funct ...

Unable to assign a className to a personalized React component - troubleshooting needed!

I have a component that relies on another component. I am trying to pass CSS positioning from the outer component to the inner one by using the following code: import OptionsMenu from './OptionsMenu' import { withStyles } from 'material-ui/ ...

Leveraging next-generation JavaScript (NextJS), incorporate sass-loader for seamless inclusion of variables in each individual

I'm having trouble implementing a custom webpack configuration in my nextjs project. My objective is to automatically import @import "src/styles/variables.scss"; for all scss files in my application. I have successfully set up a webpack con ...

Code snippet that will emphasize the active page if there are multiple pages, such as highlighting page 2

My current project involves implementing a script to highlight the current page on a Tumblr blog. I found the script I am using here: $(function(){ $('a').each(function() { if ($(this).prop('href') == window.location.href) { ...