2

Im writing some Protractor testcases by using a pageobject. However im having trouble using elements directly from the Pageobject file, in the spec file.

Possibly im missing some JS nuance, since i havent written that much in JS.

i would like to use some elements defined in Pageobject like so:

    var PageObject = function() 
    {
    var loginUsername = element(by.id('loginusername'));

    //other methods
    };
    module.exports = PageObject;

to be used in my spec file like so:

var PageObject = require('./pageObject.page.js');



describe( ' Login page ', function(){
    it('type something in the usernamefield', function(){
        var pageObject = new PageObject();
        pageObject.get();

        pageObject.loginUsername.sendKeys('Test');

    });
});

Using methods (for example get but also others)works fine, but using elements directly causes some undefined errors.

Im trying to copy from something like this, where supposedly it should work.

https://ramonvictor.github.io/protractor/slides/#/33

1 Answers1

3

Something you are missing,

bind your variable to object in pageObject.page.js, using this keyword

var PageObject = function() 
    {
       this.loginUsername = element(by.id('loginusername'));
       ......
    };
module.exports = PageObject;

In Spec first get the real application and do real work

it('type something in the usernamefield', function(){
        browser.get('"https://....');
        var pageObject = new PageObject();
        pageObject.loginUsername.sendKeys('Test');

    });

This works for me perfect,

Surendra Jnawali
  • 3,190
  • 5
  • 28
  • 44