vendredi 22 juillet 2016

Generate new random value if value is in array

I have a situation where I'm using protractor to click a random link on the page. (There are a lot). I have an array of links that I don't want to click, so I want to know when my random link is in that array and generate a new random link.

Here's my working code to click a random link on the page

var noClickArray = ['link2', 'link3']; // array much bigger than this
var parent = this;

function() {
  var links = element.all(by.css('.links'));
  return links.count().then(function(count) {
    var randomLink = links.get(Math.floor(Math.random() * count));
    randomLink.getText().then(function(text) {
      parent.selectedLink = text; // used in a different function
      var containsLink = _.includes(noClickArray, text);
    });
    return randomLink.click();
  });
}

I'm using lodash to find if the randomLink text is in noClickArray but I don't know how to keep generating random values until the value does not exist in the array. How can I do this?




Aucun commentaire:

Enregistrer un commentaire