我想我误解了元素的工作原理..
HTML 代码:
<div id="div-item">
<a href="#">A link</a>
<form>
<div>
<select>
<option>1</option>
<option>2</option>
</select>
</div>
</form>
</div>
当我这样做时:
element(by.tagName('select')).all(by.tagName('option')).count();
这给了我 2,这是正确的
当我这样做时:
element(by.id('div-item')).element(by.tagName('select')).all(by.tagName('option')).count();
这给了我 0。我认为链接元素会找到子元素。这不正确吗?如何限制 .all(by.tagName('option')) 仅在此 div 内,而不是整个页面?
这是 xeditable 库。我的 HTML 代码是:
<div id="div-item" class="col-xs-3">
<a id="xeditable-link" href="#" ng-if="canEdit"
editable-select="user_id"
e-ng-options="user.id as user.name for user in users"
onbeforesave="updateProfile({user_id: $data})">
{{ showNameFromID(user_id) || 'none'}}
</a>
</div>
但这会生成大量 HTML 代码。它是这样的:
<div id="div-item" class="col-xs-3">
<a id="xeditable-link" href="#" ng-if="canEdit"
editable-select="user_id"
e-ng-options="user.id as user.name for user in users"
onbeforesave="updateProfile({user_id: $data})">
{{ showNameFromID(user_id) || 'none'}}
</a>
<form ...>
<div class="xeditable-controle..." ...blah blah>
<select ...ng-options="..." blah blah>
<option>1</option>
<option>2</option>
</select>
<span> ...the buttons... </span>
</div>
</form>
</div>
我的测试规格:
it('should pass ...', function() {
element(by.id('xeditable-link')).click(); // Click the link to bring up xeditable
element(by.tagName('select')).click(); // Click the select to bring up the popup
var allOptions = element(by.id('div-item')).element(by.tagName('select')).all(by.tagName('option'));
expect(allOptions.count()).toBe(2);
for (var i = 0; i < 2; ++i) {
expect(allOptions.get(i).getText()).toBe(testText[i]);
}
});
两个期望语句都失败了。计数为 0,而不是 2 和“NoSuchElementError:使用定位器找不到元素:By.tagName("select")”