Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
Page Object Modelling - recursion within a method. Cypress

I am trying to create in Cypress some classes and methods for the website I test and one of the methods I managed to create is recursive. The method had worked fine when it was a "loose function" in a test case, but when I'm trying to implement it as a method in a class I am getting a RereferenceError: "choose_Randcategory() is not defined"

The method is supposed to select a random item from a drop-down list. There are at least 2 drop-down lists. Depending on what kind of an item has been selected from the second (or the last which an item was selected from) drop-down list, the next drop-down list may or may not be called by ajax request. The method checks whether the next drop-down list has been called and invokes itself recursively.

class DropDown_PO {

    choose_randCategory(id = String(Math.floor(Math.random() * (15)) + 1), number = 2) {
        let reccur = number + 1
        if (id != 0) {
                cy.get("#category_selector_5>:nth-child(" + id + ")").then(($elem) => {

                    cy.get("#category_selector_5").select($elem.text(), { force: true });

                })
     
        }

        cy.get("div[class='category_level_" + String(number) + "']").find('select').find('option').its('length').then(
            ($firstele) => {
                let randomlist = Math.floor(Math.random() * ($firstele - 2)) + 2
                cy.get("div[class='category_level_" + String(number) + "']").find('select').find('option').eq(randomlist).then(
                    ($choice) => {
                        cy.get("div[class='category_level_" + String(number) + "']").find('select').select($choice.text(), { force: true })
                        cy.wait(4000)
                    }
                )
            })

        cy.get("body").then(
            ($body) => {
                if ($body.find("div[class='category_level_" + String(number + 1) + "']").length) {
                    choose_randCategory(0, reccur);

                }
            })

    }
}

Many thanks in advance.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Generally this kind of error can handle by first define variable then assign value

var choose_randCategory = null;
class DropDown_PO {

    choose_randCategory = (id = String(Math.floor(Math.random() * (15)) + 1), number = 2) => {
        let reccur = number + 1
        if (id != 0) {
                cy.get("#category_selector_5>:nth-child(" + id + ")").then(($elem) => {

                    cy.get("#category_selector_5").select($elem.text(), { force: true });

                })
     
        }

        cy.get("div[class='category_level_" + String(number) + "']").find('select').find('option').its('length').then(
            ($firstele) => {
                let randomlist = Math.floor(Math.random() * ($firstele - 2)) + 2
                cy.get("div[class='category_level_" + String(number) + "']").find('select').find('option').eq(randomlist).then(
                    ($choice) => {
                        cy.get("div[class='category_level_" + String(number) + "']").find('select').select($choice.text(), { force: true })
                        cy.wait(4000)
                    }
                )
            })

        cy.get("body").then(
            ($body) => {
                if (choose_randCategory != null && $body.find("div[class='category_level_" + String(number + 1) + "']").length) {
                     choose_randCategory(0, reccur);

                }
            })

    }
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You're getting the reference error because choose_randCategory() is not defined globally, but instead exists on your DropDown_PO class. So to reference it within that class, you'll have to use this. before it.

    cy.get('body').then(($body) => {
      if (
        $body.find("div[class='category_level_" + String(number + 1) + "']")
          .length
      ) {
        // here is where we need to use `this.`
        this.choose_randCategory(0, reccur);
      }
    });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda