I have been working on implementing a page object pattern in my cucumber.js test automation suite with selenium webdriver. However, I am facing an error when trying to call the page object from my test step. The folder structure I am using is as follows:
features
|__pages - login_page.js
|__steps - login_steps.js
|__support - world.js
The feature file looks like this:
Feature File
Scenario: Login
Given I browse to the login page
When I enter the username "test" and password "test"
Then I should be logged in successfully
This is how my login page is structured:
'use strict';
exports.login = function () {
this.driver.get('https://www.example.com/login');
this.driver.findElement({ id: 'username' }).sendKeys('my username');
this.driver.findElement({ id: 'password' }).sendKeys('my password');
this.driver.findElement({ id: 'btn-login'}).click();
};
And here is my steps definition file:
'use strict';
var expect = require('chai').expect;
var loginpage = require('../pages/LoginPage');
module.exports = function() {
this.World = require('../support/world.js').World;
this.Given(/^I enter the username "(.*)", and password (.*)"$/, function (username, password) {
//call page object
loginpage.login();
});
};
However, when running the test, I encounter the following error message:
TypeError: Cannot read property 'get' of undefined
at Object.exports.login (/Users/Gerry/cuke/features/pages/LoginPage.js:9:16)
This error points to the line: this.driver.get('https://www.example.com/login');
The steps within the login function work fine if directly inserted into the steps without calling the page object.
Any suggestions or ideas on what could be going wrong here?