https://cypress.io logo
Navigate through categories until the end level ca...
# i-need-help
b
I have a category menus, each category has subcategories and so on. There can be various levels of categories and their subcategories. I am trying to write a cypress script that would click on the random category and then random subcategory all the way til the end level category which doesn't have anymore subcategories. This is what I have now, but it doesn't work:
Copy code
Typescript
Cypress.Commands.add('getRandomFromList', { prevSubject: true }, (subject) => {
  return cy
    .wrap(subject)
    .should('have.length.above', 0)
    .then(function ($items) {
      return Cypress._.sampleSize($items.toArray(), 1);
    });
});

 goToRandomCategory() {
    cy.get('#homepageCategories ul').getRandomFromList().click();
  }

  goToRandomEndLevelCategory() {
    this.goToRandomCategory();
    this.goToRandomSubCategory();
  }

  goToRandomSubCategory() {
    cy.location('pathname').should('include', '--c'); // to ensure that the category page is loaded
    cy.get('[itemtype*="SiteNavigationElement"]:nth-child(2) > ul').then((subcategories) => {
      if (subcategories.length > 0) {
        cy.wrap(subcategories).getRandomFromList().click();
        this.goToRandomCategory();
      }
    });
  }
It only clicks the homepage category and opens first level category but doesn't go further then that. The weird thing is that it shows category list, I can see it but the trace says the elements are not visible :/ Can you please help 😄 ?

https://cdn.discordapp.com/attachments/1105836979695915038/1105836979846914068/image.pngâ–¾

g
b
Thanks, @gray-kilobyte-89541 I really appreciate your help and advice 😄 It seems it works now
It seems it doesn't work in the end, at least I don't know how to use it properly I guess.
Copy code
Typescript
 goToRandomSubCategory() {
    cy.location('pathname').should('include', '--c');
    recurse(
      () => {
        return cy.get('#container-categorySidebar > div > ul li').should(Cypress._.noop)
      },
      ($subcategories) => $subcategories.length > 0,
      {
        post() {
          cy.location('pathname').should('include', '--c');
        },
        log: true,
        delay: 100,
        debugLog: true
      }
    ).getRandomFromList().click();
  }
I feel like I am totally getting this the wrong way
The way I have written it, I don't see it iterate more then once
Also, I tried applying the same principle you had in one of your videos by clicking the accordions and the the button if it finds it
but in my case the thing is that I am trying to click and traverse on is the same list of subcategories - So I need to click on the random subcategory through the category structure but at the same time if I reach the last category in the structure, then the list of subcategories doesn't exist anymore. So my `cy.get('#container-categorySidebar > div > ul li').should(Cypress._.noop) `can return a list of subcategories or if I am on the last level it returns an empty JQuery object
so I am not sure how to accomplish my goal here
g
Well, I cannot tell what you are doing wrong, do you have a simple standalone page example that I could use to write the test for you?
b
I am trying to do it for this page https://www.microspot.ch/de - I am trying to randomly click through the category menu until I reach the end-level category
g
I will see if I have time to write an example, but I would not write a test like that. Instead make a test that goes through the category menu the same way again and again
b
@gray-kilobyte-89541 did you maybe have a chance to take a look at it? Also, what do you mean the same way again and again? Can you describe a bit what you mean by that? you would not have random categories but rather click on specific ones in the list like always the first one or something else in mind?
g
yup, the test should be deterministic - repeat the same actions every time it runs, otherwise you would miss things or not be able to recreate the bug
ok, I have a little example of my own for showing how to do this, will make a video or a blog post, follow me on any channel to get notified
b
@gray-kilobyte-89541 thanks a lot, it means everything to me 😘