vendredi 25 août 2017

get number of items with the same selector in Nightwatch

I'm new to e2e testing (and JavaScript). I'm using Nightwatch framework and I'm trying to find how many elements are with X selector and click on a random one.

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


module.exports = {
  url: function getUrl() {
    return `${this.api.launchUrl}/products`;
  },
  commands: [tryRandom],
  elements: {
    addToShortlistBtn: '.search-card-footer__button-container'
  }
};

console.log(res.value.length) doesn't log anything. I've also tried res.value.count.

The const num outputs as NaN. I've also used other selectors and the result is the same.

Aucun commentaire:

Enregistrer un commentaire