Store the value returned from either the URI or the response in the test context using Cypress IO

I am struggling to figure out how to extract a specific portion of a key from both the URL and the xhr response. I initially attempted using the URI method but couldn't specify to save only part of the value.

.url().then(($url) => {
            const moveKey = $url.text(/someString.+?(?=\/)/);
            cy.log(moveKey);
        })  

Another approach I tried involved using cy.route along with cy.wait, but unfortunately, the wait consistently times out.

cy.server()
        .route('/overview').as('getMove')
        .wait('@getMove').then((xhr) => {
            const moveKey = xhr.move.key;
        })

URL:

Answer №1

Cypress is not the cause of this issue. It just requires a basic understanding of javascript strings. Feel free to use the following code snippet to meet your needs.

it('test', ()=>{
    cy.url().then(fullURL=>{
      fullURL = 'https://example.com/move/12345/overview';
      let arr = fullURL.substr(fullURL.indexOf('move')).split('/');
      let moveKey = arr[1];
      cy.log(moveKey);
    }
  )})

Answer №2

With the helpful suggestion from @Srinu Kodi, I successfully solved this using the match method.

.url().then(($url) => {
            const idKey = $url.match(/specificString.+?(?=\/)/);
            cy.log(idKey);
        })  

The log displays an array containing the necessary key. Many thanks to everyone for their assistance!

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

Improving HTML coding with a more effective alternative to document.write()

Looking at the setup of my website, I have a menu button and comments section that need to appear on every page. Instead of manually adding this code to each HTML file, I decided to streamline the process by using a JavaScript file that generates all of th ...

Unidentified googletagmanager detected in vendors segment

Recently, my ad blocker detected an unfamiliar Google Tag Manager request originating from a chunk provided by one of my vendors. Is it typical for tracking to be embedded in dependencies like this? And what type of information can be collected from my we ...

Tips for utilizing AJAX to send data from a specific row

<table> <tr data-id="1"> <input type="text" name="name_1" value="abc"> <input type="text" name="value_1" value="1"> <a href="load_edit_row(this)">Edit</a> </tr> <tr data-id="2"> <input t ...

Understanding the user context in Chef script resources: What's the deal?

I've been working on setting up pm2 and pm2-logrotate as a non-root user (specifically using nginx user) with Chef 12. First, I globally installed pm2: [root@~] npm install -g pm2 Then, I configured the pm2 startup script to run as the nginx us ...

Link the values of mongoose array to a distinct identifier

This question may come across as vague, but I'll do my best to explain it clearly. Just a heads up, I'm relatively new to working with mongoose :) So, I have this mongoose schema where different values are stored for each user: let userSchema = ...

Tips for navigating the world of hybrid web applications that combine elements of both traditional multi-page applications and single-page

What are some best practices for developing a multi-page application using modern JavaScript frameworks? Multi-page Application In a multi-page application, we utilize multiple templates with "template syntax" to retrieve backend data and handle AJAX (if ...

Obtain a collection of the corresponding keys for a given value from dictionaries

I'm implementing a function that retrieves a list of keys associated with a specific value in the dictionary. Although I am able to print out the first key successfully, I'm facing difficulties in displaying subsequent keys. I understand that I ...

What is the best way to choose the element directly beneath a specific element using jQuery?

I am facing a challenge where I need to manipulate HTML content using only jQuery, without changing the original structure. The requirement is to display and slide down content with a specific class (current) upon clicking on an h1 tag. However, I am strug ...

Unexpected behavior in React-Native when filtering array objects

I am currently working on a react-native project where I am dealing with an array object that is being fetched from the backend. Here is a snippet of the Array. { "2010":[ { "id":1243, "eventName": ...

Initializing ng-app with a specific name will prevent the initialization of variables

Here is the code snippet I am working with: <div ng-app="" ng-init="show_login=false;count=0"> <button> ng-click="show_login=!show_login;count=count+1">INSPIRE</button> <form ng-show="show_login"> Username <input type="t ...

What is the best way to transfer an id from JavaScript to Rails while utilizing ajax?

Is there a way to call a rail route and pass in an ID from javascript? I have recently started using rails routes within my application, even in js. The current code I am using is: # app/assets/javascript/verifying_link.js $.ajax({ url: "/verify_link/ ...

JavaScript code that loads a copied mesh object using three.js

Currently using three.js version r59, encountering difficulties when attempting to duplicate a loaded model. The goal is to create multiple models through looping, with the plan to apply textures at a later stage. for (var i=0; i<5-1; i++){ va ...

What's the deal with this error message saying val.slice isn't a function?

In the process of developing a web application using express with a three-tier architecture, I have chosen to use a mysql database to store blogposts as a resource. Here is an illustration of how the table is structured: CREATE TABLE IF NOT EXISTS blogpos ...

Animating Chart.js 2 from right to left instead of from top to bottom

Here is the issue demonstrated in the jsfiddle below. Initially, the data inserts work well. However, when the data set reaches a cap of 10, an unwanted behavior occurs where the data points are animated top-down instead of moving leftward. This can be qu ...

pressure exerted on the body

Recently, I've been working on a project for the website . One of the main issues I've encountered is that the <body> element is consistently 20px lower than it should be at the top. This has led to visible problems with the background grad ...

Enhance user interactivity on your website by incorporating jQuery and CSS for

<table id="tab"> <tr aaa="one" bbb="ooo"><td>xxx</td><</tr> <tr aaa="two" bbb="one"><td>xxx</td><</tr> <tr aaa="three" bbb="one"><td>xxx</td><</tr> ...

Working with color fills in Three.js shapes

Looking for some help with code that generates a yellow circle: let radius = 5, segments = 64, material = new THREE.LineBasicMaterial( { color: 0xF0C400 } ), geometry = new THREE.CircleGeometry( radius, segments ); geometry.vertices.shift ...

Issue with disabled button in Angular 2 when using Chrome's autocomplete feature

In a basic login form, the login button is initially disabled with the following code: [disabled]="!password || !loginName" When Chrome's autocomplete feature fills in the values for loginName and password, the button remains disabled after the pa ...

What are the steps to successfully set up npm 3 beta on a Windows computer?

After attempting to upgrade my npm version to 3 using npm install npm@3 -g, I found that it is still on version 2.x: https://i.sstatic.net/NVOkB.png What steps should be taken to successfully install the latest beta release? ...

Validation in Laravel appears to be ineffective when managing schedules

I have a table that contains schedules for each subject. I want to ensure that every schedule is unique and not duplicated. The table includes columns for room, teacher, time, day, and checker who verifies the schedule. It's essential that there are n ...