Tips for choosing a specific point on a Vuetify <v-slider> using Cypress

Currently, I am exploring how my application responds when a user interacts with a specific area on a basic Vuetify <v-slider>. How can I replicate this scenario in a Cypress integration test effectively?

For instance, to click on the center of the <v-slider>, I can use the following code snippet:

cy.get(#sliderID).find('input[role="slider"]').click()

However, I wish to click on another location of the <v-slider>. To illustrate, for a <v-slider> that has a minimum value of 0 and a maximum value of 100, I aim to simulate a click on 25. How can I accomplish this task?

Answer №1

GMaiolo's explanation is spot on.

If you find it useful, here is a simple function I created (inspired by GMaiolo's explanation) to click on a specific point of a Vuetify v-slider:

// example usage: clickVSlider('#selectID input[role="slider"]', 0.25)

const clickVSlider = (sliderSelector, percentFromLeft) => {
  const sliderWidth = Cypress.$(sliderSelector).width()
  const sliderHeight = Cypress.$(sliderSelector).height()
  const pixelsFromLeft = percentFromLeft * sliderWidth
  const pixelsFromTop = 0.5 * sliderHeight
  cy.get(sliderSelector).click(pixelsFromLeft, pixelsFromTop)
}

Alternatively, if you prefer a more 'composable function' approach:

// example usage: cy.get('#selectID input[role="slider"]').then(clickVSlider(0.25))

const clickVSlider = percentFromLeft => subject => {
  const sliderWidth = subject.width()
  const sliderHeight = subject.height()
  const pixelsFromLeft = percentFromLeft * sliderWidth
  const pixelsFromTop = 0.5 * sliderHeight
  cy.wrap(subject).click(pixelsFromLeft, pixelsFromTop)
}

Another option is to create a Cypress custom command, but remember not to go overboard with their usage:

// cypress/support/commands.js
// example usage: cy.get('#selectID input[role="slider"]').clickVSlider(0.25)

Cypress.Commands.add('clickVSlider', {prevSubject: true}, (subject, percentFromLeft) => {
  const sliderWidth = subject.width()
  const sliderHeight = subject.height()
  const pixelsFromLeft = percentFromLeft * sliderWidth
  const pixelsFromTop = 0.5 * sliderHeight
  cy.wrap(subject).click(pixelsFromLeft, pixelsFromTop)
})

Answer №3

I attempted to work with Cato Minor's solution, but it didn't quite suit my needs. It seems that there may have been some changes to how <v-slider> is displayed in the DOM.

My current environment includes "vue": "^2.6.11" and "vuetify": "^2.6.10".

Even though I tried using Cato Minor's custom command, I had to target a different clickable element due to the rendered HTML structure of v-slider.

<div class="v-slider v-slider--horizontal theme--dark">
    <input value="137" id="input-150" disabled="disabled" readonly="readonly" tabindex="-1" data-cy="parent-child-space-slider">
    <div class="v-slider__track-container">...</div>
    <div role="slider" tabindex="0" aria-valuemin="40" aria-valuemax="300" aria-valuenow="137" aria-readonly="false" aria-orientation="horizontal" class="v-slider__thumb-container primary--text" style="left: 37.3077%;">...</div>
</div>

In this case, only the parent div contained useful information about width and height.

To make it work for me, I adapted the approach like so:

cy.get('[data-cy="parent-child-space-slider"]').parent().clickVSlider(0.50);

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

Issue: jQuery Datatable not functioning properly while attempting to populate the table through JavaScript.Explanation: The

I'm currently working on populating a table using JavaScript. However, I've encountered an issue where the table is being populated but it shows "No data available in table". Furthermore, when I try to interact with the data, it disappears. This ...

What could be the reason that my JSON request is not functioning properly?

I am currently working on creating a movie search application. This project marks my first time delving into json and I'm facing some issues with my code. As of now, I have it set up and running smoothly on localhost through xampp. On submitting the ...

Vue's drag-and-drop functionality is effective for dragging entire groups, but it does not work

Below is the code that enables dragging of groups in toListFetchRouteA1: <draggable id="first" data-source="juju" :list="toListFetchRouteA1" class="list-group" draggable=".item" group="a"> <div class="list-group-item item" v-for="teamfetch in ...

Utilizing Vuex store to showcase data in component according to route

The data stored in Vuex is as follows: state: { news: [ { id: 1, title: "placeholder", text: "Lorem ipsum doloret imes", date: "20-01-2020", type: "management" }, { id: 2, title: "Revenue", text: "Lorem ipsum doloret imes", date: "20-01 ...

IE and Firefox display different responses when encountering an empty XML document

When working with jQuery to read an XML file, I occasionally encounter the situation where the XML is empty. In this case, I anticipate that the error function (no_info) will be triggered because the file is not formatted as expected for the dataType. Int ...

Start the vue-cli-service serve command and run cucumber tests one after the other

I am trying to figure out how to run vue-cli-serve, start the development server, and then run my cucumber tests without having to run separate scripts in different terminals. I'm wondering if there is a way to achieve this with just one command. ...

Before each existing DIV in a loop, a new div is inserted using the insertBefore

const len = document.getElementById('parent').children.length for (let i = 0; i < len; i++) { const div = document.createElement("div"); document.getElementById('parent').insertBefore(div, document.getElementById('parent' ...

The process of incorporating a function into a website's source code using a web driver

In the source code, there is a button with an onclick event to change the paragraph content. However, the code provided does not seem to be functioning properly. ((JavascriptExecutor) this) .executeScript("function test123() { y=docume ...

In JavaScript, how is the symbol "." referred to as?

While I am familiar with its purpose and the language terminology, could you please provide the official name for the period/dot used in Javascript/jQuery? Appreciate your help! ...

What are the steps to modify the placeholder of the formik <Field/> component?

Struggling to figure out how to make the date input empty with Formik. I just want it to display nothing initially, but have been unable to do so after hours of trying different methods. <Field name="date" type="date" className ...

Is React.js susceptible to XSS attacks through href attributes?

When user-generated links in an href tag appear as: javascript:(() => {alert('MALICIOUS CODE running on your browser')})(); This code was injected via an input field on a page that neglects to verify if URLs begin with http / https. Subseque ...

How can I link a custom event to a particular component in Vue3's router-view?

This application belongs to me //App.vue <template> <Header ref="header"></Header> <RouterView @emitDelet="deleteHeader"> </RouterView>> <Footer></Footer> </template> The rout ...

Ways to create a clickable anchor tag without using any text

I am currently designing my own website and incorporating links to various social media platforms using icons. However, I am facing an issue where the links are not clickable. For a detailed look at my problem, you can refer to this JSFiddle: http://jsfid ...

Can you explain the distinction between App: React.FunctionComponent and App = (): React.FunctionComponent()?

Currently exploring the depths of typescript. Can someone clarify the distinction between these two code snippets: const App: React.FunctionComponent<CustomProps> = (props: CustomProps) => { return <div>Hello World!</div>; }; and: ...

From Google Assistant to Python Results

Is there a way to control a Bitcraze Crazyflie drone using Google Home? I'm looking to give the command "Drone fly to x3 y4" which will be processed through Firebase and result in the Google Assistant Output: "Flying to x3 y4". Additionally, I need an ...

Vue - Issue with DOM not updating after array sorting operation

I've been working on sorting some JSON data with Vue, and I've encountered an issue. Although the data changes when checked in the Vue console debugger, the DOM doesn't reflect these updates. Here's the Vue code I'm using: Array. ...

Tips for incorporating a download button into a video player using Plyr JS

I'm using Plyr JS and I am trying to add a download option for each video. Here is what I've done so far to make the download option work: Even though I have included: controlsList="nodownload" <video controls crossorigin playsinline contro ...

What are the steps to utilize webpack for refreshing the script tag in a jade file?

Can webpack be used to automatically update the script tag in a jade/pug template to point to the minified and cache-busted JS bundle? Most examples I've come across demonstrate how plugins can be used to convert the jade template to HTML, but I am us ...

Azure Chatbot that logs conversations in Webchat whenever the user selects 'none of the above' option

Recently, I've delved into the world of web chat services and embarked on a journey to craft a chat bot using pure JavaScript code that can seamlessly integrate into any HTML file. Despite consulting Microsoft's documentation, I find myself in a ...

Trouble with clicking buttons in Java and Selenium web scraping

Hey everyone, Currently, I'm working on a Java Selenium script that automates clicking and filling forms for me. I've written a line of code that's causing me some trouble. The intention is to click on a button, but it's not happening ...