在量角器使用页面对象模式正确 [英] Using Page Object Pattern Correctly in Protractor

查看:172
本文介绍了在量角器使用页面对象模式正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我使用量角器来测试它。
搜索结果的 HTML

I have an Angular app and I am using Protractor to test it.

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>

这里是code为Page对象:

    '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();

测试code

 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 的没有定义。如果我改变了下面两行

For some reason I keep getting an error saying getName is not defined. If I change the following two lines

name: stat.historyPage.getName() 
value: stat.historyPage.getValue(),

name: stat.element(by.css('#name')).getText(),
value: stat.element(by.css('#value')).getText()

它工作正常。我不知道是什么原因。我真的想避免我的测试页上写CSS定位器,因为它看起来并不好,这是一个不好的做法。我将AP preciate建议,帮助我。

It works fine. I am not sure what the reason is. I really want to avoid writing css locators on my test page as it does not look good and it is a bad practice. I will appreciate suggestions to help me.

推荐答案

我会提出完整的地图()块到Page对象:

I would move the complete map() block into the Page Object:

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();

这篇关于在量角器使用页面对象模式正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆