我的第一个堆栈溢出问题....
我试图在 Protractor 中链接所有语句,但出现错误。
TypeError: Object [object Object] has no method 'all'
我正在查看下一页上的 API 代码
http://angular.github.io/protractor/#/api?view=ElementArrayFinder.prototype.all
表示可以使用element.all(locator).all(locator)
以这个为例
var foo = element.all(by.css('.parent')).all(by.css('.foo'))
我的代码似乎非常相似,我很困惑为什么会收到此错误。我试过完全按照 API 示例中的代码结构来构建代码。我也尝试过 element.all(locator).element.all(locator).
我的目标是对 AREFS 进行 Ng 重复;找到文本等于 r_string 的那个(这是先前生成并添加到页面的字符串;期望该元素存在;单击该元素;
一些尝试:
var parent = element.all(by.repeater('labgroup in LabGroupService.allLabGroups'));
var child = parent.all(by.xpath('//option[text() = \'' + r_string + '\']'));
expect(child.count()).toBe('1');
和
var elem = element.all(by.repeater('labgroup in LabGroupService.allLabGroups')).all(by.xpath('//option[text() = \'' + r_string + '\']'));
expect(elem.count()).toBe('1');
最后是我正在使用的 HTML 片段。
<a ui-sref="root.user-management.labgroup({labgroupID: labgroup.id})" class="ng-binding" href="#/management/labgroup/43">1kvub4wgCvY9QfA</a>
</dd><!-- end ngRepeat: labgroup in LabGroupService.allLabGroups --><dd ng-repeat="labgroup in LabGroupService.allLabGroups" class="ng-scope">
<a ui-sref="root.user-management.labgroup({labgroupID: labgroup.id})" class="ng-binding" href="#/management/labgroup/47">3PNsny8lUMlMwBw</a>
</dd><!-- end ngRepeat: labgroup in LabGroupService.allLabGroups --><dd ng-repeat="labgroup in LabGroupService.allLabGroups" class="ng-scope">
<a ui-sref="root.user-management.labgroup({labgroupID: labgroup.id})" class="ng-binding" href="#/management/labgroup/42">c3NOI7Z3933ui3a</a>
</dd><!-- end ngRepeat: labgroup in LabGroupService.allLabGroups --><dd ng-repeat="labgroup in LabGroupService.allLabGroups" class="ng-scope">
编辑---------------------------------------- --------------------------------------------
我开始怀疑这是版本错误还是 Protractor 错误。为了进行调试,我确实包含了 API 页面中的源代码。
<div id='id1' class="parent">
<ul>
<li class="foo">1a</li>
<li class="baz">1b</li>
</ul>
</div>
<div id='id2' class="parent">
<ul>
<li class="foo">2a</li>
<li class="bar">2b</li>
</ul>
</div>
以及来自源页面的示例。
var foo = element.all(by.css('.parent')).all(by.css('.foo'))
expect(foo.getText()).toEqual(['1a', '2a'])
我仍然遇到同样的错误。
TypeError: Object [object Object] has no method 'all'
编辑 2---------------------------------------- --------------------------------------
我设法通过在实际的 html 代码中添加“data-class = labgroup-link”并使用这个 Protractor 代码来解决这个问题。
element.all(by.css('[data-class="labgroup-link"]')).filter(function(elem, index) {
return elem.getText().then(function(text) {
return text === r_string;
});
}).then(function(filteredElements) {
expect(filteredElements[0].isPresent()).toBe(true);
filteredElements[0].click();
ptor.sleep(100);
});
解决方案------------------------------------------------------------------------------------------------------------------------------------------------------------>
必须升级 Protractor 才能获得最新的 API。
最佳答案
Protractor >= 1.3.0
应该工作给定:https://github.com/angular/protractor/blob/f7c3c370a239218f6143a/lib/protractor.js#L177
var foo = element.all(by.css('.parent')).all(by.css('.foo'));
Protractor < 1.3.0
ElementArrayFinder 没有all
方法:https://github.com/angular/protractor/blob/master/docs/api.md#api-elementarrayfinder-prototype-get因此:
TypeError: Object [object Object] has no method 'all'
也许你想
var foo = element(by.css('.parent')).all(by.css('.foo'));
// or shorter
var foo = $('.parent').$$('.foo');
开始做
var foo = element.all(by.css('.parent')).all(by.css('.foo'));
关于javascript - 链接 Protractor 中的所有语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26067242/