使用selenium向ng-tags-input添加标签

时间:2016-02-04 07:52:59

标签: angularjs selenium-webdriver ng-tags-input

我试图自动测试我添加了ng-tags-input的网页。我在使用selenium webdriver将标签添加到ng-tags-input时遇到了困难。 Selenium找到元素,但sendKeys()函数不会向其添加文本。

Selenium Code(v.2.49.0):

 driver.findElement(By.id("tags")).sendKeys("test, one, two, three");

HTML:

 <tags-input ng-model="tempTags" id="tags" ng-model-options="{ getterSetter: true }" on-tag-added="convertToTagString()" on-tag-removed="convertToTagString()"></tags-input>

最终HTML:

<tags-input ng-model="tempTags" id="tags" ng-model-options="{ getterSetter: true }" on-tag-added="convertToTagString()" on-tag-removed="convertToTagString()" class="ng-pristine ng-valid ng-isolate-scope ng-valid-max-tags ng-valid-min-tags ng-valid-leftover-text ng-touched">
<div class="host" tabindex="-1" ng-click="eventHandlers.host.click()" ti-transclude-append="">
  <div class="tags" ng-class="{focused: hasFocus}">
     <ul class="tag-list">
        <!-- ngRepeat: tag in tagList.items track by track(tag) -->
     </ul>
     <input class="input ng-valid ng-touched ng-dirty ng-valid-parse" autocomplete="off" ng-model="newTag.text" ng-model-options="{getterSetter: true}" ng-keydown="eventHandlers.input.keydown($event)" ng-focus="eventHandlers.input.focus($event)" ng-blur="eventHandlers.input.blur($event)" ng-paste="eventHandlers.input.paste($event)" ng-trim="false" ng-class="{'invalid-tag': newTag.invalid}" ng-disabled="disabled" ti-bind-attrs="{type: options.type, placeholder: options.placeholder, tabindex: options.tabindex, spellcheck: options.spellcheck}" ti-autosize="" type="text" placeholder="Add a tag" spellcheck="true" style="width: 69px;"><span class="input" style="visibility: hidden; width: auto; white-space: pre; display: none;">Add a tag</span>
  </div>
</div>
</tags-input>

1 个答案:

答案 0 :(得分:1)

SendKeys()到输入(不是ng-tags-input)的xpath工作。

driver.findElement(By.xpath("//*[@id=\"tags\"]/div/div/input")).sendKeys("test, five");