【发布时间】:2017-10-13 16:45:24
【问题描述】:
我有一个Angular 应用程序,我正在使用Protractor 对其进行测试。
HTML
<div id="all" class="row text-center">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-6">
<div class="dashboard-stat block panel padder-v bg-primary">
<div class="icon hidden-xs">
<img src="../assets/images/icon-ppt-inv.png">
</div>
<div>
<div id="value" class="font-thin h1 block">
{{summary.num | number:0}}
</div>
<div id="name" class="text-muted text-xs">
Albert
</div>
</div>
</div>
</div>
</div>
这是页面对象的代码:
'use strict';
var history_page = function (){
this.getStat = function(){
return element.all(by.css('#all'));
};
this.getName = function(){
return element(by.css('#name')).getText();
};
this.getValue = function(){
return element(by.css('#value')).getText();
};
};
module.exports = new history_page();
测试代码
var historyPage = require('./history_page.js');
it('Test', function(){
var history = historyPage.getStat().map(function (stat) {
return {
name: stat.historyPage.getName()
value: stat.historyPage.getValue(),
}
});
history.then(function (value) {
console.log(value);
});
});
由于某种原因,我不断收到错误消息,提示 getName 未定义。如果我更改以下两行
name: stat.historyPage.getName()
value: stat.historyPage.getValue(),
作为
name: stat.element(by.css('#name')).getText(),
value: stat.element(by.css('#value')).getText()
它工作正常。我不确定是什么原因。我真的想避免在我的测试页面上编写 css 定位器,因为它看起来不太好,而且这是一种不好的做法。我会感谢对我有帮助的建议。
【问题讨论】:
-
这并不能直接回答你的问题,但我推荐页面对象的星盘框架。在我看来,它会让你的事情变得更容易。
-
可以举个例子吗?
标签: javascript angularjs protractor