Home > Back-end >  Page Object Modelling - recursion within a method. Cypress
Page Object Modelling - recursion within a method. Cypress

Time:05-19

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.

CodePudding user response:

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);

                }
            })

    }
}

CodePudding user response:

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);
      }
    });
  • Related