量角器:找到合适的元素进行测试

时间:2015-01-07 14:55:49

标签: angularjs testing protractor end-to-end

我认为我误解了元素是如何运作的。

HTML code:

<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.我认为链接元素找到子元素。这不正确吗?如何仅在此div中限制.all(by.tagName('option')),而不是整个页面?

这是可修改的库。我的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]);
    }
});

两个期望陈述都失败了。 count是0,而不是2和“NoSuchElementError:使用locator找不到元素:By.tagName(”select“)”

2 个答案:

答案 0 :(得分:1)

尝试单个css定位器

$$('#div-item select [option]').count()
// The same as
element.all(by.css('#div-item select [option]')).count()

答案 1 :(得分:0)

原来我在另一个.html文件中有一个'div-item'。由于AngularJS是一个单页面应用程序,它正在拿起那个而不是我想要的那个。