Ensure that the button click is validated repeatedly until it becomes disabled by utilizing

Seeking assistance with validating a button click using Cypress. The goal is to ensure the button is clickable only when slides are available. If no slides are present, the button should be disabled by adding a disabled class. Any recommendations for improving this validation process?

Here is the HTML code for when the next slide is available:

<button class="arrow"><span class="screen-reader-only">Previous slide</span></button>

HTML code for when the next slide is not available and the disabled class is added:

<button class="arrow disabled"><span class="screen-reader-only">Previous slide</span></button>

The Cypress code being used for validation:

it('Validate the button is clickable',()=> {
        cy.get('.arrow')
          .then(($btn) => {
              if($btn.is(":disabled")){
                  $btn.should('have.class','disabled')
                      .and('have.css','background-color','rgb(62, 64, 69') 
                      cy.log('Arrow is disabled and not clickable')
              } else {
                  cy.wrap($btn).click()
                  expect($btn).to.have.css('background-color','rgb(172, 42, 0)')
              }
          })

Answer №1

To check if the button is clickable, you can utilize .hasClass('disabled') since a disabled class is included. In this scenario where there are multiple arrow buttons, it's advisable to employ an each function rather than a then.

it('Verify button clickability', () => {
  cy.get('.arrow')
    .should('be.visible')
    .each(($btn) => {
      if ($btn.hasClass('disabled')) {
        cy.wrap($btn)
          .should('have.class', 'disabled')
          .and('have.css', 'background-color', 'rgb(62, 64, 69)')
        cy.log('The arrow is disabled and cannot be clicked')
      } else {
        expect($btn).to.have.css('background-color', 'rgb(172, 42, 0)')
        cy.wrap($btn).click()
      }
    })
})

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

Fixed bar on top, revealed only when the scroll attempts to conceal it

I have placed a menu on the top section of my website, but not at the very top. I would like it to stick to the top of the page when the user scrolls down and disappears from view. However, if the title is still visible, I want the menu to remain below it ...

Submitting data with ajax in MVC when an option is chosen in a dropdown menu

Within my form, I have multiple dropdown lists. Whenever a user selects an option from one of these dropdowns, I want that value to be saved in the backend database. To avoid reloading the page, I believe using Ajax is the best approach, but I need assista ...

Implementing a custom input component with a Vue change event

I am looking to incorporate the @change event into a custom input component I have created. Here is my custom input component: <template> <div class="w-100"> <div class="form-text"> <input :value="value" @ ...

TypeScript struggling to recognize specified types when using a type that encompasses various types

I have a defined type structure that looks like this: export type MediaProps = ImageMediaProps | OembedProps; Following that, the types it references are defined as shown below: type SharedMediaProps = { /** Type of media */ type: "image" | "oembed"; ...

Using both ng-if and ng-click in AngularJS for enhanced functionalities

There's a button that should display a toggle only when the value is greater than 0; otherwise, it shouldn't do anything. This snippet of code shows how things were prior to adding ng-if: <span >{{values.valuesNumber}} <i class= ...

Is it possible to use v-if in conjunction with a style tag to specify a different source file? Alternatively, is there a more efficient method I

I attempted the example provided below, but unfortunately, it did not function as expected. The reason behind my endeavor is that adding numerous modifiers (--tuned) to achieve the desired outcome seemed impractical. Therefore, I decided to try and link ...

Removing the Yellow Highlight on Input Field Following Email Autocomplete in Chrome

My username-password form is styled and working perfectly, but there's an issue that arises when I log in multiple times. Chrome automatically fills in my email, turning the username textbox yellow. It doesn't seem to happen with Firefox or Safar ...

Overusing For Loops for Cloning in JavaScript

REVISED: (I have pinpointed the issue previously discussed in the comments) this is the code I am working with: <script type="text/javascript"> for(var i=0; i<5; i++){ $(".image-wrap").clone().appendTo(".container").attr(&apo ...

Creating a dynamic JSON structure from an array list of elements: A step-by-step guide

I am faced with the task of transforming an array of employee IDs, listed as follows: empIds: [38670, 38671, 38672, 38673], into a JSON payload structured like this: { "members": [ { "EmployeeId": &quo ...

Quick way to access dropdown menu in Visual Code while selecting JavaScript/React code

While working on React code in Visual Studio Code, I accidentally triggered a dropdown menu when trying to copy and paste a section of my code. One of the items that caught my eye was labeled "Destructure JavaScript," which seemed intriguing. Unfortunately ...

Ant Design radio group buttons are currently dysfunctional

I'm having some trouble with radio group buttons. Can anyone assist me with this issue? (your help is greatly appreciated) Current Problem: Radio Group buttons are unclickable. Not sure where the issue lies! Technologies Used: React hooks, styled-co ...

When transitioning from Quasar 1 / Vue 2 to Quasar 2 / Vue 3, the Option Group feature may encounter selection issues

I've been working on creating dynamic option groups, and I recently came across some code that seemed to fit my needs. However, after modifying it for Quasar2/Vue3, I'm unable to select any options and can't figure out what's causing th ...

What is preventing this.form.post(URI) from accessing all API resource routes, beyond just one specific route?

In my API Resource Routes setup, I have multiple Controllers configured, but only one ('project' => 'API\ProjectController') seems to be accessible. Even though I have used the correct URIs in the route list for the other API Re ...

Utilizing Ajax for Django POST Requests

I have an app called register_login which handles login and registration operations. On my localhost:8000/login page, I have a form and I want the button to redirect to a function in the register_login app. However, I am facing difficulties achieving this ...

Updating Select Options Disabled/Enabled in Angular 2

In my Angular2 project, I have 2 select elements: <div ng-controller="ExampleController"> <form name="myForm"> <label for="companySelect"> Company: </label> <select name="companySelect" id= ...

Tips for converting a flat JavaScript array into a multidimensional one

I'm working on an example where I need to change some values. Here is the link to access it: http://jsfiddle.net/k83hj1jb/ The current output is a flat array: [ {"name":"options[11][140]","value":"140"}, {"name":"options[8][99]","value":"99" ...

Can I stream a file from an external server in NodeJS without routing it through my own server?

My website provides links for downloading or streaming media files from another server. Currently, my setup looks something like this: download = (req, res) -> song_id = req.params.id download_url = "http://mediaserver.com/#{song_id}" console.log ...

Exploring the quirky characters in the PHP mail() function

Currently using the php mail function, it's somewhat effective, although not my preferred method. However, it seems I must make do for now. The issue at hand is that special European characters are showing up strangely in the email. I attempted to se ...

In React Native, what exactly does it signify when you run the packager with the `--clearCache` flag?

Whenever I run my React Native apps, I rely on the command react-native run-ios. However, while debugging, it recommended using the "--clearCache" flag when running the packager. Can someone explain what this suggestion means? What exactly is the package ...

Tips for replicating input boxes in a while loop

https://i.sstatic.net/iuNIl.png HTML : <table> <tr> <td> <input type="text" name="name[]" value=" echo $ail"> <label >Address</label> <input type="text" name="coun ...