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

267
Views
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 answers
Answer question

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 Report

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 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!