无法正确执行量角器的页面对象模式

时间:2014-11-28 13:41:47

标签: javascript jasmine protractor

我一直在尝试在页面对象模式中调整我的测试代码。但是我对此感到很难过。

我有以下代码:

这是我的页面对象文件

PageObjects = function() {

    var preenchePsafe = dvr.findElement(by.id('global-search'));
    var opcaoPsafe = dvr.findElement(by.css('#search > ul > li:nth-child(1) > a'));


    this.pesquisarPsafe = function(pesquisa) {

        //dvr.findElement(by.id('global-search')).sendKeys(pesquisa);
        preenchePsafe.sendKeys(pesquisa);
        opcaoPsafe.click();

    };

    };

module.exports = PageObjects;

这是我的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 preenchePsafe = dvr.findElement(by.id('global-search'));
 var opcaoPsafe = dvr.findElement(by.css('#search > ul > li:nth-child(1) > a'));

 preenchePsafe.sendKeys(pesquisa);
 opcaoPsafe.click();

取消注释该行

 //dvr.findElement(by.id('global-search')).sendKeys(pesquisa);

我的测试工作正常。但我不想这样做,使用这样的页面对象毫无意义。由于我是javascript和jasmine的初学者,我认为这可能是一个代码问题。

2 个答案:

答案 0 :(得分:1)

页面对象中未定义

dvr。要么你需要:

var dvr = require('some/path.js')

位于页面对象文件的顶部或构造函数内部。

另外,我建议您使用element(by.css())$()代替dvr.findElement(by)。例如:

// $() is the same as element(by.css())
var preenchePsafe = $('global-search'));
// You can chain element calls.
var opcaoPsafe = $('#search > ul').$$('li').get(1).$('a');

答案 1 :(得分:1)

我猜你的堆栈跟踪部分是这么说的 TypeError: object is not a function

因为在spec文件的beforeEach函数中,您尝试将pageObject实例化为pageObjectsModule function 的新实例,但是pageObjectsModule被定义为对象在你的第一行。

var pageObjectsModule = require('./page.js'); 

describe('PSafe Home', function () {
    var pageObject;

    beforeEach(function () {
        pageObject = pageObjectsModule; // This line should fix the problem.
        isAngularSite(false);
        handlePromise = dvr.getAllWindowHandles();
    });

    it('Pesquisar PSafe', function () {

        dvr.get('http://home.psafe.com/');
        pageObject.pesquisarPsafe('PSafe');

    });

});

或者,你可以这样做:

var pageObject = require('./page.js'); // define the pageObject object as soon as you require it.

describe('PSafe Home', function () {
   // var pageObject;

    beforeEach(function () {
     // pageObject = new PageObjectsModule(); // now you don't need this line
        isAngularSite(false);
        handlePromise = dvr.getAllWindowHandles();
    });

    it('Pesquisar PSafe', function () {

        dvr.get('http://home.psafe.com/');
        pageObject.pesquisarPsafe('PSafe');

    });

});