在量角器中正确使用页面对象模式

Using Page Object Pattern Correctly in Protractor

我有一个 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 定位器,因为它看起来不太好,而且这是一种不好的做法。我会很感激能帮助我的建议。

我会将完整的 map() 块移动到页面对象中:

var history_page = function () {
    this.all = element.all(by.css('#all'));

    this.getStat = function() {
        return this.all.map(function (stat) {
            return {
                name: stat.element(by.css('#name')).getText(),
                value: stat.element(by.css('#value')).getText()
            }
        });
    };
};

module.exports = new history_page();
'use strict';

    var history_page = function (){

        this.getStat = function(){
            return element.all(by.css('#all'));
        };

        this.getName = function(index){
            return this.getStat().then(function(stats){
                  return stats[index].get(index).element(by.css('#name'));
        };


    };

    module.exports = new history_page();

是的,定位器、函数(如@alecxe 提到的)和条件属于页面对象。否则就违背了他们的目的。

我会做类似的事情:

var historyPage = function() {
    this.stats = $$('.row');
    this.name = $('#name');
    this.getValue = $('#value');
};
module.exports = new history_page();

测试如下:

var historyPage = require('./history_page.js');

it('Test', function() {
    expect(historyPage.name.getText()).toBe('Albert');
});

对重复数据使用地图很酷...但在您的示例中,我不确定自己是否遵循。 #all 是唯一的,所以如果您要映射重复数据,您可能想要映射 .row?不确定。但它可能看起来像这样:

    this.getStat = function() {
        var that = this;
        return this.stats.map(function(stat) {
            return {
                // not tested... 
                name: stat.that.name.getText(),
                value: stat.that.value.getText()
            }
        });
    };

此外,我的 protractor_example repo

中有一些页面对象示例