vendredi 25 août 2017

custom function to select and click a random element in Nightwatch

I'm new to e2e testing (and JS). I use Nightwatch framework.

I'm trying to create a function that selects a random element from a list of elements with the same selector, and clicks on it.

This is what I tried:

const tryRandom = {
  pickOne() {
    this.api.elements('css selector', '.search-card-footer__button-container', function(res) {
       optionsLength = Math.floor((Math.random() * res.value.length) + 1);
    });
    this.api.waitForElementVisible(`.search-cards__item:nth-child(${optionsLength})`);
    this.api.click(`.search-cards__item:nth-child(${optionsLength}) .action-button`);
  }
};

But in this case, optionsLength is not defined.

Math.floor((Math.random() * res.value.length) + 1) returns a number. I want to use this number outside of the function.

I've tried to store the full function in a variable, as in:

const optionsLength = this.api.elements('css selector', '.search-card-footer__button-container', function(res) {
       Math.floor((Math.random() * res.value.length) + 1);
    });

But that way optionsLength logs [object Object] instead a number.

Aucun commentaire:

Enregistrer un commentaire