Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

653
Views
Cypress: ¿es posible seleccionar elementos aleatorios con eq() dinámicamente?

¿Es posible seleccionar un elemento aleatorio dentro de eq() ? Tengo el siguiente caso de uso: hay varios menús desplegables con diferentes opciones desplegables. Quiero que Cypress abra el menú desplegable, obtenga la cantidad máxima de opciones desplegables y luego seleccione una opción aleatoria del conteo. Me gustaría evitar hacer esto con variables separadas, sino directamente de forma dinámica dentro del comando. Así es como se ve mi intento actual, pero no funciona:

 cy.dropdownSelector().eq(0).click() cy.dropdownOptions().eq(Math.floor(Math.random() * cy.dropdownOptions().length)).click()
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Entonces, para generar un número aleatorio entre min (incluido) y max (incluido), debe usar esto:

 Math.floor(Math.random() * (max - min + 1)) + min

Así que ahora su código de ciprés será:

 cy.get('optionsselector') .its('length') .then((len) => { cy.get('optionsselector') .eq(Math.floor(Math.random() * ((len-1) - 0 + 1)) + 0) .click() })
about 4 years ago · Juan Pablo Isaza Report

0

Tengo un comando para obtener un índice aleatorio usando lodash ( https://lodash.com/docs/4.17.15 ) y evitar repetir el índice si necesita repetir la prueba con otro nodo, debe proporcionar la longitud de la matriz y un array con las posiciones que ya han sido probadas.

 const _ = require('lodash'); /** * Calculates a random position in a given array * @param {Number} length array length * @param {Array} positionsTested positions in the array that have already been tested */ Cypress.Commands.add('getRandomPosition', (length, positionsTested) => { if (positionsTested.length >= length) { return cy.wrap(null); } const i = _.random(length - 1); return cy.wrap((_.find(positionsTested, i)) ? cy.getRandomPosition(length, positionsTested) : i); }); // usage cy.dropdownSelector().then((elements) => { doTheTest([], elements); }); function doTheTest (positionsTested, elements) { cy.getRandomPosition(elements.length, positionsTested).then((index) => { if (index !== null) { positionsTested.push(index); const selectedElement = elements[index]; if (suitableToTest(selectedElement)) { // do something with the element } else { doTheTest(positionsTested, elements); } } else { cy.log('not enough elements to test'); } }); }
about 4 years ago · Juan Pablo Isaza Report

0

No hay necesidad de usar eq()

Puede usar el método de sample de Lodash que está integrado en Cypress. sample elegirá un artículo al azar de la colección.

Hace que la prueba sea más breve y clara:

 cy.get('selector').then(options => { cy.get(Cypress._.sample(options)).click() })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!