How can I get an element randomly in a dropdowlist using protractor?

烂漫一生 提交于 2019-12-21 05:50:34

问题


I've this dropdownlist and I'm trying to get a value randomly and click on it. How can I do it? I can't use the class because there is other element with the same class. I don't have a clue.

<dropdownlist _ngcontent-lnd-30="">
<select class="form-control ng-pristine ng-valid ng-touched">
        <!--template bindings={}-->
<option value="null">Selecione um tipo de norma...</option>
<option value="5980dfc1-ed08-4e5f-bdd7-144beb2fafe3">Enunciado Orientativo</option>
<option value="e721782a-11ba-4828-ac3a-934f60652760">Instrução Normativa</option>
<option value="a4469d22-1188-467d-a78a-e385a2cc8eb9">Lei</option>
<option value="9d8ea2fd-efe9-410a-8062-f5607c56332d">Portaria</option>
<option value="8407a52d-a760-48a2-b780-ab93f5904565">Provimento</option>
<option value="8b20cc7f-6be1-43a5-a0b7-ac2fe695b14c">Resolução</option>
<option value="8fe058a8-ece3-4ef5-8f74-17255a90066f">Súmula</option>
 </select></dropdownlist>

回答1:


Count the number of options (it's asynchronous, so you will be chaining some promises), then get a random number between 1 and the total count, then get that specific element and click on it.

var allOptions = element(by.tagName('dropdownlist')).all(by.tagName('option'));

allOptions.count().then(function(numberOfItems) {
    return Math.floor(Math.random() * numberOfItems) + 1;
}).then(function(randomNumber) {
    allOptions.get(randomNumber).click();
});



回答2:


You can select a random index of the options array like so.

// get your options first
// var options = angular.element(...) or getElementByID(...) Whatever your flavor.
var selectRandomOption = options[Math.floor(Math.random() * options.length)]


来源:https://stackoverflow.com/questions/38133340/how-can-i-get-an-element-randomly-in-a-dropdowlist-using-protractor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!