Protractor POM method is not recognizing

60 Views Asked by At

spec.js

describe('Testing an animal adoption flow using page object', function() {
     
    beforeEach(function() {
        browser.get('http://www.thetestroom.com/jswebapp/index.html');
    });
     
    var home_page = require('./pages/home_page.js');
    it ('Should be able to adopt an animal by page object', function() {
        home_page.enterName('Blabla');
        expect(home_page.getDynamicText()).toBe('Blabla');
        var animal_page = home_page.clickContinue();
         
        animal_page.selectAnimal(1);
        var confirm_page = animal_page.clickContinue();
         
        expect(confirm_page.getTitle()).toContain('Thank');
    });
});

home_page.js

require('./animal_page.js');
 
var home_page = function() {
     
    this.nameTextBox = element(by.model('person.name'));
    this.dynamicText = element(by.binding('person.name'));
    this.continueButton = element(by.buttonText('CONTINUE'));
     
    this.enterName = function(name) {
        this.nameTextBox.sendKeys(name);
    };
     
    this.getDynamicText = function() {
        return this.dynamicText.getText();
    };
     
    this.clickContinue = function() {
        this.continueButton.click();
        return require('./animal_page.js');
    };
};

Failures:

  1. Testing an animal adoption flow using page object Should be able to adopt an animal by page object
  Message:
[31m    Failed: home_page.enterName is not a function[0m
  Stack:
    TypeError: home_page.enterName is not a function
2

There are 2 best solutions below

2
Sergey Pleshakov On

You don't create an instance of your constructor function with new keyword. It should have been

var home_page = new (require('./pages/home_page.js'));

and you need to instruct js what you are exporting, so your home page should be

require('./animal_page.js');

var home_page = function() {

this.nameTextBox = element(by.model('person.name'));
this.dynamicText = element(by.binding('person.name'));
this.continueButton = element(by.buttonText('CONTINUE'));
 
this.enterName = function(name) {
    this.nameTextBox.sendKeys(name);
};
 
this.getDynamicText = function() {
    return this.dynamicText.getText();
};
 
this.clickContinue = function() {
    this.continueButton.click();
    return require('./animal_page.js');
};
}

module.exports = home_page; // <------ this line

but make sure you do the same with animal_page

0
Vijay On

I got the answer, we need to include

spec.js
const { browser } = require('protractor');

home_page.js
module.exports = new home_page();