我正在关注一些我发现使用页面对象开始组织测试的示例。
例如:https://github.com/juliemr/ng-page-e2e/blob/master/test/angularsite_page.js http://codingsmackdown.tv/blog/2014/07/08/using-page-objects-in-angularjs-protractor/
这是我的页面对象文件:
var PageObjects = function() {
var preenchePsafe = browser.driver.findElement(by.id('global-search'));
var opcaoPsafe = browser.driver.findElement(by.css('#search > ul > li:nth-child(1) > a'));
this.abrirUrl = function() {
dvr.get('http://home.pp.psafe.com/');
};
this.pesquisarPsafe = function (pesquisa){
this.preenchePsafe.sendKeys(pesquisa);
this.opcaoPsafe.click();
};
};
module.exports = PageObjects;
这是我的测试:
var pageObjects = require('./page.js');
describe('PSafe Home', function () {
beforeEach(function () {
var pageObjects = new page();
isAngularSite(false);
handlePromise = dvr.getAllWindowHandles();
});
it ('Pesquisar PSafe', function () {
browser.get('http://home.psafe.com/');
pageObjects.pesquisarPsafe('PSafe');
})});
然后,当我执行时,我得到以下错误:
1)PSafe Home Pesquisar PSafe消息: ReferenceError:页面未定义Stacktrace: ReferenceError:未定义页面 在[object Object]。 (C:\ Users \用户QARJ \ WebstormProjects \ ProtractorPiloto \ piloto.js:6:31) 在c:\ Users \ QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \ protractor \ node_modules \ jasminewd \ index.js:94:14 在[object Object] .webdriver.promise.ControlFlow.runInNewFrame_(c:\ Users \ QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ webdriver \ promise.js:1654:20) at [object Object] .webdriver.promise.ControlFlow.runEventLoop_(c:\ Users \ QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ webdriver \ promise.js:1518:8) ====异步任务====异步测试函数:beforeEach() 在[object Object]。 (C:\ Users \用户QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \量角器\ node_modules \ jasminewd \ index.js:93:33) 在[object Object]。 (C:\ Users \用户QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \量角器\ node_modules \ minijasminenode \ lib中\异步-callback.js:45:37) at [object Object] .jasmine.Block.execute(c:\ Users \ QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \ protractor \ node_modules \ minijasminenode \ lib \ jasmine-1.3.1.js:1174:17) at [object Object] .jasmine.Queue.next_(c:\ Users \ QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \ protractor \ node_modules \ minijasminenode \ lib \ jasmine-1.3.1.js:2209:31) at [object Object] ._ onTimeout(c:\ Users \ QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \ protractor \ node_modules \ minijasminenode \ lib \ jasmine-1.3.1.js:2199:18) 错误 在[object Object]。 (C:\ Users \用户QARJ \ WebstormProjects \ ProtractorPiloto \ piloto.js:5:5) at [object Object] .jasmine.Env.describe_(c:\ Users \ QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \ protractor \ node_modules \ minijasminenode \ lib \ jasmine-1.3.1.js:913:21) at [object Object] .jasmine.Env.describe(c:\ Users \ QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \ protractor \ node_modules \ minijasminenode \ lib \ jasmine-1.3.1.js:898:15) at describe(c:\ Users \ QARJ \ WebstormProjects \ ProtractorPiloto \ node_modules \ protractor \ node_modules \ minijasminenode \ lib \ jasmine-1.3.1.js:658:27) 在对象。 (C:\ Users \用户QARJ \ WebstormProjects \ ProtractorPiloto \ piloto.js:3:1)
修改
在尝试了几件事后,我“得到了”做了以下事情:
这是我的spec文件:
var pageObjectsModule = require('./page.js');
describe('PSafe Home', function () {
var pageObject;
beforeEach(function () {
pageObject = new pageObjectsModule();
isAngularSite(false);
handlePromise = dvr.getAllWindowHandles();
});
it('Pesquisar PSafe', function () {
dvr.get('http://home.psafe.com/');
pageObject.pesquisarPsafe('PSafe');
})
});
这是我的页面对象文件
var PageObjects = function() {
//var preenchePsafe = dvr.findElement(by.id('global-search'));
//var opcaoPsafe = dvr.findElement(by.css('#search > ul > li:nth-child(1) > a'));
this.abrirUrl = function() {
dvr.get('http://home.psafe.com/');
};
this.pesquisarPsafe = function(pesquisa) {
dvr.findElement(by.id('global-search')).sendKeys(pesquisa);
//preenchePsafe.sendKeys(pesquisa);
//this.opcaoPsafe.click();
};
};
module.exports = PageObjects;
但是我必须映射所需的元素并在 pesquisarPsafe(pesquisa)中使用sendKeys,而是将其映射到函数外部(那些注释行)。我不知道为什么会这样。
答案 0 :(得分:0)
看起来您没有正确命名变量。尝试这样的事情:
var PageObjects = require('./page.js');
describe('PSafe Home', function () {
beforeEach(function () {
var pageObjects = new PageObjects();
isAngularSite(false);
handlePromise = dvr.getAllWindowHandles();
});
it ('Pesquisar PSafe', function () {
browser.get('http://home.psafe.com/');
pageObjects.pesquisarPsafe('PSafe');
})
});
答案 1 :(得分:0)
export class CosmicNovaSelectWardPage extends PageBase
{
private btnWardList : any;
private lblHeaderTitle : any;
private btnCancel : any;
constructor() {
super();
const element = Elements.CosmicNovaSelectWardPage;
this.btnWardList = super.findLocators(element.btnWardList.findBy, element.btnWardList.value);
this.lblHeaderTitle = super.findLocators(element.lblHeaderTitle.findBy, element.lblHeaderTitle.value);
this.btnCancel = super.findLocators(element.btnCancel.findBy, element.btnCancel.value);
}
/**
* Step: select specific ward from given ward list
* @param ward
* @returns {CosmicNovaBaseWardPage}
*/
public step_selectUserWard(ward: string): CosmicNovaBasePhysicianPage
{
log.info("Step: choose "+ward+" in ward list [:step_selectUserWard:]");
this.Helper_Actions.findTextAndClick(this.btnWardList,ward);
return new CosmicNovaBasePhysicianPage();
}