简体   繁体   English

用量角器从下拉菜单中选择一个元素

[英]Selecting an element from a drop-down with Protractor

I am having serious trouble using the regular selenium webdriver on one page of my application which is build with Angular. 我在使用Angular构建的应用程序的一页上使用常规Selenium Webdriver时遇到了严重麻烦。 I have added the dependency of ngwebdriver in my pom.xml and so far I have solved some of the issues. 我在pom.xml中添加了ngwebdriver的依赖关系,到目前为止,我已经解决了一些问题。 Only with the drop-down, I still haven't figured out how to do this. 仅通过下拉菜单,我仍然没有弄清楚该如何做。 This is the drop-down I am talking about: 这是我正在谈论的下拉列表:

 <d3-data-list _nghost-c8="" ng-reflect-data-source="[object Object]" ng-
reflect-html-id="documents_glossary_entityPicke" ng-reflect-allow-blank-
option="true" ng-reflect-blank-option-text="All entities" ng-reflect-default-
caption="Choose an entity"><div _ngcontent-c8="" class="dropdown d3-data-
list" id="documents_glossary_entityPicker">
  <button _ngcontent-c8="" aria-expanded="true" aria-haspopup="true" class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button" id="documents_glossary_entityPicker_button">
All entities
<span _ngcontent-c8="" class="caret" 
id="documents_glossary_entityPicker_caret"></span>
  </button>
  <ul _ngcontent-c8="" aria-labelledby="dropdownMenu1" class="dropdown-menu" 
ng-reflect-infinite-scroll-handle-in-progress="false" 
id="documents_glossary_entityPicker_dropdown">
    <!--bindings={
      "ng-reflect-ng-if": "true"
}--><li _ngcontent-c8="">
      <a _ngcontent-c8="" 
id="documents_glossary_entityPicker_blank_option">All entities</a>
</li>

then the options from the drop-down are like this: 那么下拉菜单中的选项如下:

</li><li _ngcontent-c8="">
  <a _ngcontent-c8="" id="documents_glossary_entityPicker_option_2">Entity three</a>
</li>

The problem is that the order of the elements in my drop-down is not maintained because if an element (entity) is edited on another page then it will be at the bottom of the list. 问题是我的下拉菜单中的元素顺序无法保持,因为如果在另一个页面上编辑了一个元素(实体),则该元素将位于列表的底部。 So I can never really know where exactly my element is in the drop-down list. 因此,我永远无法真正知道我的元素在下拉列表中的确切位置。

Is there something like browser.findElement(ByAngular.buttonText("text")) which I can use in my case? 是否有类似browser.findElement(ByAngular.buttonText("text"))类的东西可以使用? Thanks. 谢谢。

I guess you're looking for the filter() or .reduce() possibilities. 我猜您正在寻找filter().reduce()可能性。

For .reduce() check the example here 对于.reduce()在此处查看示例

For .filter() your code should look something like this: 对于.filter()您的代码应如下所示:

element.all(by.css('ul li a')).filter(function (elm) {
    return elm.getAttribute('value').then(function (text) {
        return text === "TheValueYouLookFor";
    });
}).first().click();

I didn't test this .filter myself, but it's pretty straight forward from the Protractor documentation . 我没有.filter测试过这个.filter ,但是从Protractor文档中可以很直接地看出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM