如果值在数组中,则生成新的随机值

coc*_*coa 5 javascript arrays random protractor

我有一种情况,我使用量角器点击页面上的随机链接.(有很多).我有一系列链接,我不想点击,所以我想知道我的随机链接何时在该数组中并生成一个新的随机链接.

这是我的工作代码,用于单击页面上的随机链接

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();
  });
}
Run Code Online (Sandbox Code Playgroud)

我正在使用lodash来查找randomLink文本是否在noClickArray但我不知道如何继续生成随机值,直到数组中不存在该值.我怎样才能做到这一点?

ale*_*cxe 5

我认为你的问题太复杂了.我只是过滤掉你不想filter()事先点击的链接:

function() {
  var links = element.all(by.css('.links'));
  links = links.filter(function (link) {
      return link.getText().then(function(text) {
          return !_.includes(noClickArray, text);
      });
  });

  return links.count().then(function(count) {
    var randomLink = links.get(Math.floor(Math.random() * count));
    return randomLink.click();
  });
}
Run Code Online (Sandbox Code Playgroud)