Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda