gpt4 book ai didi

javascript - Protractor 页面对象继承

转载 作者:IT老高 更新时间:2023-10-28 22:12:12 33 4
gpt4 key购买 nike

鉴于我正在利用页面对象模式构建我的 angularjs Protractor e2e 测试套件。

并且我尽可能合理地将页面对象代码分开在不同的文件中。

  1. 启用页面对象继承的好方法是什么? javascript经典继承?基于 Object.create() 的继承?其他?

  2. 我应该在页面对象中保持期望吗?或支持 Martin Fowler optinion by moving them to an assertion library ?在这种情况下,在这个 javascript-nodejs 技术堆栈中究竟会是什么样子?

我准备了一个live jsfiddle playground here所以你可以尝试你的改进。

或者只是在答案中粘贴代码,为了清楚起见,我将在下面粘贴 jsfiddle 内容:

loginPage.js

"use strict";

// A Page Object is a Singleton, so no need to constructors or classic js inheritance,
// please tell me if I'm wrong or what's the utility of creating a (new LoginPage())
// every time a spec need to use this login page.
var loginPage = {
// Page Object Elements
userElm: $('.user.loginPage'),

// Page Object Assertions
// Martin Fowler [doesn't favor](http://martinfowler.com/bliki/PageObject.html)
// assertions in page objects, I'm open to suggestions on how to move
// the assertions away from the Page Object and see how an assertion library
// could like like in protractor.
assertInputsDisplayed: function() {
return ('Assertion: this.userElm: '+this.userElm);
},

// Page Object Actions
get: function () {
return ('navigating to LoginPage with userElm: '+this.userElm);
}
};

module.exports.loginPage = loginPage;

loginDialog.js

"use strict";

var loginPage = require('./loginPage.js').loginPage;
var helpers = require('./helpers.js');

// Inherit properties from another Page Object
var loginDialog = helpers.extend({}, Object.create(loginPage), {
// Page Object Elements
userElm: $('.user.loginDialog'),

// Page Object Actions
get: function () {
return ('navigating to LoginDialog with userElm: '+this.userElm);
},

logout: function () {
return ('logging out of Dialog. user was: '+this.userElm);
}
});

module.exports.loginDialog = loginDialog;

helpers.js

"use strict";

// some helper to avoid adding an external dependency for now
var extend = function(target) {
var sources = [].slice.call(arguments, 1);
sources.forEach(function (source) {
for (var prop in source) {
target[prop] = source[prop];
}
});
return target;
};

usage.js

"use strict";

// Mock $() for easy unit testing this on nodejs REPL
global.$ = function(args) { return ('$BUILT '+args); };

var loginPage = require('./loginPage.js').loginPage;
var loginDialog = require('./loginDialog.js').loginDialog;

console.log(loginPage.userElm); //=> '$BUILT .user.loginPage'
console.log(loginDialog.userElm); //=> '$BUILT .user.loginDialog'
console.log(loginPage.get()); //=> 'navigating to LoginPage with userElm: $BUILT .user.loginPage'
console.log(loginDialog.get()); //=> 'navigating to LoginPage with userElm: $BUILT .user.loginDialog'
console.log(loginPage.assertInputsDisplayed()); //=> 'LoginPage assertion: this.userElm: $BUILT .user.loginPage'
console.log(loginDialog.assertInputsDisplayed()); //=> 'LoginPage assertion: this.userElm: $BUILT .user.loginDialog'

//loginPage.logout(); //=> TypeError: Object #<Object> has no method 'logout'
console.log(loginDialog.logout()); //=> 'logging out of Dialog. user was: $BUILT .user.loginDialog'

最佳答案

这是我为培训我的一些同事创建好的 Protractor 测试套件而设置的教程的链接。

一切都是实时的,您可以访问、探索等演示站点。

https://github.com/Droogans/ProtractorPageObjects

这将为您设置安装、大纲、组织技术等。

如果您有任何问题,请随时提出问题。

关于javascript - Protractor 页面对象继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21466505/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com