Guide on combining two assertion statements using an OR condition in Cypress

Being new to the world of UI automation/Cypress, I am seeking assistance in setting up assertions on a JavaScript object returned by the cypress-ag-grid package.

The code I have is reading data from an ag-grid.

cy.get("#myGrid").getAgGridData().should((data)=>{
cy.log(data)
})

This code prints the following object in console:

[
{ id: 1, name: "tata", safetyRating: "-50" },
{ id: 2, name: "maruti", safetyRating: "-50" },
{ id: 3, name: "ford", safetyRating: "" },
{ id: 4, name: "skoda", safetyRating: "" }
]

As I iterate over the safetyRating field,

cy.get("#myGrid").getAgGridData().should((data)=>{
  data.forEach(({ safetyRating }) => {
    cy.wrap(+safetyRating).should('be.lt', -50);
  })
});

The test fails due to blank values being converted to 0, which is not less than or equal to -50. To pass the test case, I would like to add another assert condition in an 'OR' form, like this: .should('be.eq', 0);

Any better approaches for handling this issue are also welcomed.

Answer №1

All of your safety assessments are above -50, causing the .should('be.lt', -50) to not meet any data points.

To correct this, switch to using .should('be.lte', -50) which signifies "less than or equal to".

Answer №2

In my view, the utilization of +saftyRating is not the appropriate method in this scenario because there could potentially be a legitimate 0 rating within the grid.

An absence of value like saftyRating: "" varies from a zero value like saftyRating: "0" - one is explicitly provided while the other essentially means "unknown".

To evaluate two (or more) conditions, consider using a callback function similar to the following:

data.forEach(({ saftyRating }) => {
  cy.wrap(saftyRating)
    .should(value => {
      expect(value === '' || +value <= -50).to eq(true)
    })
})

Alternatively, you can also achieve this with satisfy:

data.forEach(({ saftyRating }) => {
  cy.wrap(saftyRating)
    .should('satisfy', (value) => {
      return value === '' || +value <= -50
    })
})

If additional requirements surface, you can easily expand upon the existing conditions.

For more examples, refer to this answer at How to check that element has either of classes in Cypress

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

Images are not being shown by Glide JS

I have implemented the Glide JS carousel within a VueJS project to showcase multiple images, however, I am encountering an issue where only the first image is being displayed. The <img/> tag has the correct src URL for all three images, but only th ...

Ways to verify login status on index.html

By using the code below in "index.php", I can determine the user's session status: <?php if(isset($_SESSION['id'])) { ?> <li><a href="controller.php?type=logout" class="btn btn-borders btn-primary">Log out</a>< ...

Tips for maintaining type information when using generics in constructors

class Registry<Inst, Ctor extends new (...args: unknown[]) => Inst, T extends Readonly<Record<string, Ctor>>> { constructor(public records: T) { } getCtor<K extends keyof T>(key: K) { return this.records[key] } getIns ...

Why is my Javascript XMLHttpRequest onreadystatechanged event not triggering?

I am facing an issue with my html file and a .txt file stored in the same directory on a webserver. In the html file, I have the following code: <html> <head> <script> window.onload = function() { receiveMessage(); } function recei ...

What is the best way to ensure that two promises are both resolved before triggering a function from within a promise?

In my code, I have a forEach loop on a matches fetch that looks like this: matches => { matches.forEach(match => { Promise.all([this.teamService.getTeam(match._links.homeTeam.href)]) .then(team => { match. ...

`How can one transfer data values from Views to Templates in Django?`

At the moment, I currently have two template files named no_results_tickets.html and results_tickets.html. The first template consists of two drop-down menus that allow users to select a year and a week number. Upon hitting the submit button, the web appli ...

Using the ng-src and ng-repeat directives in combination within an if/else statement

Seeking assistance with implementing an if/else statement to display different pictures based on a condition in AngularJS. Here is an example of the controller code: $scope.getFeatureNo = function (thisCase) { if (thisCase.featureno == 1) { ...

Exploring Mixed Type Arrays Initialization in Typescript using Class-Transformer Library

In my class, I have a property member that is of type array. Each item in the array can be of various types such as MetaViewDatalinked or MetaViewContainer, as shown below class MetaViewContainer{ children: (MetaViewDatalinked | MetaViewContainer)[]; ...

Creating a grid layout that activates an image when clicked using JavaScript

I've successfully implemented a gridview with an image column, but I'm looking to enhance the functionality. Specifically, I want to enlarge the clicked image and bring it into focus on the screen. I've managed to achieve this when the image ...

Exploring the wonders of nested jQuery $.when statements

My goal is to create the following code snippet: var async1 = $.when( a1() ).then(function(){ a2() }); var async2 = $.when( a3() ).then(function(){ a4() }); $.when(async1, async2).then(function(){ console.log("complete"); }); However, currently, wh ...

What is the best way to trigger a re-render of a component in React?

Currently, I am in the process of building my very first full-stack website. After a user signs in, their information is stored in the localStorage. The goal is to display the user's name in the header once they are logged in. However, my header is no ...

Guide to Implementing i18n-iso-countries Library in React

I am currently developing a React application and attempting to utilize the i18n-iso-countries package to retrieve a countries object in English where keys represent iso codes and values represent country names. This process is straightforward in Node.js, ...

Retrieve data that resets to undefined upon reloading from an array

Encountering an unusual error while working with TypeScript for the first time. Initially, when I use console.log(data), it displays an array with objects. However, upon reloading the webpage without making any changes, the console log shows undefined. con ...

What is the process for obtaining a list of all registered users?

Is there a way to retrieve a list of all the users registered in my course-booking system? Here's the code in my user.js controller: const User = require("../models/User"); const bcrypt = require("bcrypt"); const auth = require(&q ...

Running JavaScript in Laravel without explicitly calling it

I am facing a strange issue with my code. I have an input type="button" element along with a JavaScript function in the same page. The goal is to update a table column only when the user presses the button. However, the JavaScript function gets executed ev ...

Disallowing selection on input text field

I am seeking to disable selection on an input field while still allowing for focusing, with the following criteria: Preventing selection via mouse Preventing selection via keyboard (including Ctrl+A, shift+arrows) Permitting focus on the field using both ...

What is the best way to prevent a draggable div from moving past the edge of the viewport?

My situation involves a draggable div that functions as a popup window when a specific button is clicked. However, I noticed that dragging the div to the end of the viewport allows me to drag it out of the visible area, causing the body of the page to expa ...

Attempting to serialize a form using Ajax and jQuery

I am facing an issue with submitting user input from a modal using jQuery and AJAX. The problem is that the AJAX call is not capturing the user input. Even though the AJAX call is successful, when I check on the server side, the user input appears to be bl ...

Techniques for capturing Django's output from an Ajax request

I've been trying to utilize Ajax for converting my form data to JSON and sending it to my Django view. However, I'm encountering an issue where after successful processing in the view, I am returning a template response with some context data tha ...

Tips for creating a personalized URL for each user

Here is the updated code for routes.js and login.ejs: `module.exports = function(app, passport) { // ===================================== // HOME PAGE (with login links) ======== // ===================================== app.get('/&a ...