[英]Protractor: Trying to read text after writing to text box for e2e testing
在我的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,单击:[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');
});
由于这是您正在使用的input
元素,因此您需要读取value
属性:
expect(element(by.model('first_name')).getAttribute('value')).toEqual('Frank');
实际问题是你要覆盖expect()
- 在这种情况下你需要使用then()
手动解决promise:
element(by.model('first_name')).getAttribute('value').then(function (value) {
expect(value).to.equal('Frank');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.