2015-01-26 101 views
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'); 

该代码会写弗兰克在文本框中,但现在我的目标是试图读取文本框中的文本,使确定它实际上写了我的名字。这样做我有很多麻烦。

我使用下面的代码从文本框读取尝试:

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

但我得到这个错误:

AssertionError: expected { Object (locator_, parentElementFinder_, ...) } to equal 'Frank'

而且当我尝试做一个:

console.log(element(by.model('first_name')).getText()); 
我得到这个错误:

尝试使用getAttribute('value')当我得到同样的错误。我不确定错误意味着什么,我不太清楚当我使用console.log()时我正在看什么。我有点新使用量角器。任何帮助非常感谢,并提前谢谢你。

编辑:FULL 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'); 
}); 

回答

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'); 
}); 
+0

我已经试过这个,但我得到了同样的错误:AssertionError:expected {Object(locator_,parentElementFinder_,...)}等于'Frank' – Frank 2015-01-26 21:11:49

+0

@FrankInsana你可以发布你的规范的完整代码,并注意量角器版本是否使用?谢谢。 – alecxe 2015-01-26 21:17:23

+0

OMG我爱你!有用!!我尝试使用“.then”,但显然我做错了。哇谢谢你!! +1 – Frank 2015-01-26 21:34:02