angularjs - Protractor :找到要测试的正确元素

标签 angularjs testing protractor end-to-end

我想我误解了元素的工作方式..

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 内,而不是整个页面内?

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

两个 expect 语句都失败了。计数为 0,而不是 2 和“NoSuchElementError:使用定位器未找到元素:By.tagName(“select”)”

最佳答案

尝试单个 css 定位器

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

关于angularjs - Protractor :找到要测试的正确元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27822136/

相关文章:

spring - 在 Kotlin 上添加 Spring Security 后测试 Spring Controller POST 方法

testing - 从普通的 Groovy 迁移到 Gradle

linux - Protractor 2.28 - headless Chrome 57 - linux - 测试失败

javascript - 如何对使用 ng-bind-html 渲染的可编辑 DIV 中的文本进行 ng 建模?

html - 在页面中心对齐 Angular Material 卡

python - 如何防止vscode/ms-python清除测试结果?

javascript - 无法使用 Protractor 运行 cucumber 测试

jasmine - 使用 Jenkins 中的参数并传递到 JS conf 文件

javascript - 通过引用更新作用域对象的 Angular - 问题

javascript - angularjs中的自定义时间格式