0

我想我误解了元素的工作原理..

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")”

4

2 回答 2

1

尝试单个 css 定位器

$$('#div-item select [option]').count()
// The same as
element.all(by.css('#div-item select [option]')).count()
于 2015-01-07T15:04:46.790 回答
0

原来我在另一个 .html 文件中有一个“div-item”。由于 AngularJS 是一个单页应用程序,它选择了那个而不是我想要的那个。

于 2015-01-07T15:34:02.457 回答