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="Li4vYXNzZXRzL2ltYWdlcy9pY29uLXBwdC1pbnYucG5n"></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="Li4vYXNzZXRzL2ltYWdlcy9pY29uLXBwdC1pbnYucG5n">
            </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();
                        
这篇关于在量角器中正确使用页面对象模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
	
本文标题为:在量角器中正确使用页面对象模式
				
        
 
            
        - CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
 - Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
 - addEventListener 在 IE 11 中不起作用 2022-01-01
 - Fetch API 如何获取响应体? 2022-01-01
 - Flexslider 箭头未正确显示 2022-01-01
 - Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
 - 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
 - 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
 - 400或500级别的HTTP响应 2022-01-01
 - 失败的 Canvas 360 jquery 插件 2022-01-01
 
						
						
						
						
						