7

在我的 html 文档中,我有以下代码:

<label class="field" for="first_name">First Name:</label>
<span class="pull-right"><input type="text" id="first_name">
    name="first_name" ng-model="first_name">
</span>

这会给我一个文本框,旁边有一个标签,上面写着“名字”。

然后我使用以下代码在文本框中写下我的名字:

element(by.model('first_name')).sendKeys('Frank');

此代码将在文本框中写入 Frank,但现在我的目标是尝试从文本框中读取文本,以确保它确实写了我的名字。我在做这件事时遇到了很多麻烦。

我尝试使用以下代码从文本框中读取:

expect(element(by.model('first_name')).getText()).to.equal('Frank');

但我得到这个错误:

AssertionError: 预期 { Object (locator_, parentElementFinder_, ...) } 等于 'Frank'

另外,当我尝试做一个:

console.log(element(by.model('first_name')).getText());

我收到此错误:

{ locator_: { findElementsOverride: [Function], toString: [Function: toString] }, parentElementFinder_: null, opt_actionResult_: { then: [Function: then], cancel: [Function: cancel], isPending: [Function: isPending] } , opt_index_: undefined, click: [Function], sendKeys: [Function], getTagName: [Function],
getCssValue: [Function], getAttribute: [Function], getText: [Function], getSize: [Function], getLocation: [ Function],
isEnabled: [Function], isSelected: [Function], submit: [Function], clear: [Function], isDisplayed: [Function], getOuterHtml: [Function], getInnerHtml: [Function], toWireValue: [Function] }

尝试使用时出现同样的错误getAttribute('value')。我不确定该错误的确切含义,也不确定我在使用console.log(). 我对使用量角器有点陌生。非常感谢您的任何帮助,并提前感谢您。

编辑:完整的 spec.js

var chai            = require('chai'),
    chaiAsPromised  = require('chai-as-promised');

chai.use(chaiAsPromised);

expect = chai.expect;

before(function() {
    // suite wide initial setup here
    browser.get("http://127.0.0.1:5000/");
    browser.waitForAngular();
});

it('Should redirect and load template',function(){
    element(by.id('authentication'))
        .element(by.css('.text-center'))
        .element(by.linkText('Click here'))
        .click();
    expect(browser.getCurrentUrl()).to.eventually.have.string('/#/home');
});

it('Should Enter name into text box', function(){
    element(by.model('first_name')).sendKeys('Frank');
    expect(element(by.model('first_name')).getAttribute('value'))
        .to.equal('Frank');
});
4

1 回答 1

8

由于这是input您正在使用的元素,因此您需要阅读该value属性:

expect(element(by.model('first_name')).getAttribute('value')).toEqual('Frank');

实际问题是您正在覆盖expect()- 在这种情况下,您需要使用以下方法手动解决承诺then()

element(by.model('first_name')).getAttribute('value').then(function (value) {
    expect(value).to.equal('Frank');
});
于 2015-01-26T20:34:29.847 回答