-1

i have a case where i enter a searchTerm to a search field. Then I want to count the results shown to randomly select one of the entries. But I cannot realize it with cypress

let countOfElements = "";

    cy.get(this.SEARCH_RESULT + ' > a').then($elements => {
        countOfElements = $elements.length;
        cy.log(countOfElements)
        cy.log("Found " + countOfElements + " results for search term + " + searchTerm)
    });
    
    cy.get(this.SEARCH_RESULT + ' > a').invoke('val').as('searchEntries')

    //This is obviosly not working, but I don't get how to fix this.

    let randomNumber = this.getRandomNumberBetweenTwoValues(0, cy.get('@searchEntries')));
    cy.get(this.SEARCH_RESULT + ' > a').eq(randomNumber).click()

I tried different things, like storing the value with .as() but I never seem to have access to the value outside a .then() block. So how I can use the value in my "getRandomNumber..." function to decide with entry in the result list shall be selected?

Pls help. thx

Stefanie Uhl
  • 166
  • 1
  • 2
  • 11

1 Answers1

-1
let countOfElements = "";

    cy.get(this.SEARCH_RESULT + ' > a').then($elements => {
        countOfElements = $elements.length;
        cy.log(countOfElements)
        cy.log("Found " + countOfElements + " results for search term + " + searchTerm)
    });
    
    cy.get(this.SEARCH_RESULT + ' > a').invoke('val').as('searchEntries')

    //This is obviosly not working, but I don't get how to fix this.

    let randomNumber = getRandomNumberBetweenTwoValues(0, this.searchEntries);
    cy.get(this.SEARCH_RESULT + ' > a').eq(randomNumber).click()

Something like this could work. If this.searchEntries give undefined error then make the block function(){} instead of ()=>{}

Tirath Sojitra
  • 359
  • 4
  • 15