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

2022-01-16 00:00:00 angularjs protractor javascript

我有一个 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 id="value" class="font-thin h1 block">{{summary.num |号码:0}}</div><div id="name" class="text-muted text-xs">阿尔伯特</div></div></div></div></div>

这是页面对象的代码:

 '使用严格';var history_page = function (){this.getStat = 函数(){return element.all(by.css('#all'));};this.getName = 函数(){return element(by.css('#name')).getText();};this.getValue = 函数(){return element(by.css('#value')).getText();};};module.exports = new history_page();

测试代码

 var historyPage = require('./history_page.js');它('测试',函数(){var history = historyPage.getStat().map(function (stat) {返回 {名称:stat.historyPage.getName()值:stat.historyPage.getValue(),}});history.then(函数(值){控制台.log(值);});});

由于某种原因,我不断收到错误消息,提示 getName 未定义.如果我更改以下两行

名称:stat.historyPage.getName()值:stat.historyPage.getValue(),

作为

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

它工作正常.我不确定是什么原因.我真的想避免在我的测试页面上编写 css 定位器,因为它看起来不太好,而且这是一种不好的做法.我会感谢对我有帮助的建议.

解决方案

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

var history_page = function () {this.all = element.all(by.css('#all'));this.getStat = function() {返回 this.all.map(function (stat) {返回 {名称:stat.element(by.css('#name')).getText(),值:stat.element(by.css('#value')).getText()}});};};module.exports = new history_page();

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>

Here is the code for the Page Object:

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

Test 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);
    });


     });

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

as

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

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.

解决方案

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

相关文章