量角器 sendKeys returns NoSuchElementError

Protractor sendKeys returns NoSuchElementError

我正在学习 Protractor 并尝试在 angularjs.org 页面的输入字段中发送密钥,但我收到此错误:

NoSuchElementError: No element found using locator: By.model("projectList.search")

这是我的代码:

describe ("Search list", function() {
    it ("should search for jQuery and display 1 result", function() {
        browser.get("https://angularjs.org/");
        element(by.model("projectList.search")).sendKeys("jQuery");
    });
});

我尝试使用 by.id 并得到了同样的错误。我假设这是因为元素尚不可见引起的。当我添加 browser.pause() 并逐步(缓慢)时,测试通过。解决此问题的最佳方法是什么?

来自 angularjs.org

的相关 HTML
<input type="text" ng-model="projectList.search" class="search-query" id="projects_search"
       placeholder="Search">
<table>
  <thead>
  <tr>
    <th>Project</th>
    <th>Description</th>
    <th><a href="#/new"><i class="icon-plus-sign"></i></a></th>
  </tr>
  </thead>
  <tbody>
  <tr ng-repeat="project in projectList.projects | filter:projectList.search | orderBy:'name'">
    <td><a ng-href="{{project.site}}" target="_blank">{{project.name}}</a></td>
    <td>{{project.description}}</td>
    <td>
      <a ng-href="#/edit/{{project.$id}}"><i class="icon-pencil"></i></a>
    </td>
  </tr>
  </tbody>
</table>

你可以尝试类似的方法:

var elementSelector = by.model("projectList.search");
browser.driver.isElementPresent(elementSelector).then(function(isPresent){
    element(elementSelector).sendKeys("jQuery");
});

查看protractor docs了解更多有用的功能

我设法找到了一个名为 waitReady.js 的小型图书馆,它解决了我的问题。这是我的最终代码,

require './waitReady.js')
var searchBtnElm = element(by.model('projectList.search'));
searchBtnElm.waitReady().then(function() {
      searchBtnElm.sendKeys("myText");
});

waitReady.js真的适合你吗?
曾尝试用它来点击陈旧的元素,但有时无论如何都会发生异常。

已编写点击元素的函数:

function clickWait(element) {
    (element).waitReady().then(function () {
        return element.click();
    });
};

并像这样调用它:

clickWait(element);

框架:jasmine2