andrepm
andrepm

Reputation: 877

Can't execute page objects pattern with protractor properly

I've been trying to adapt my test code within page objects pattern. But i'm having some hard time with this.

I have the following code:

This is my page objects file

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;

This is my spec file

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

    });

});

When i comment the lines

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

And uncomment the line

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

My test works properly. But i dont want to do like this, it makes no sense use page objects like this. Since i'm a beginner in javascript and jasmine, i think it may be an code strucute issue.

Upvotes: 0

Views: 850

Answers (2)

aricearice
aricearice

Reputation: 124

I'm guessing that part of your stack trace says TypeError: object is not a function

because in the beforeEach function of your spec file, you're trying to instantiate pageObject as a new instance of the pageObjectsModule function, but pageObjectsModule is defined as an object in your first line.

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

    });

});

Alternatively, you can do this:

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

    });

});

Upvotes: 1

Andres D
Andres D

Reputation: 8900

dvr is not defined in the page object. Either you need a:

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

At the top of your page object file or inside the constructor.

Also, I would recommend you to use element(by.css()) or $() instead of dvr.findElement(by). For example:

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

Upvotes: 1

Related Questions