I am trying to use PageObject pattern in my e2e tests, but I am getting a message that module is not found (Error: cannot find module InsuredSearchPage)
in /acceptance/insured/search/SearchPage.js
I have following
enter code here
var InsuredSearchPage = (function () {
'use strict';
function InsuredSearchPage() {
var searchButton = element(by.id(searchFormBtn));
var page = {
search: search
};
return page;
function search() {
searchButton.click();
}
}
return InsuredSearchPage;
})();
module.exports = InsuredSearchPage;
and in test (that is the same folder) i have this
var InsuredSearchPage = require("InsuredSearchPage");
When I run the test, I get 'Error: cannot find module InsuredSearchPage.' What am I doing wrong?
module. Exports is the object that is returned to the require() call. By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.
In programming, modules are components of a program with one or more functions or values. These values can also be shared across the entire program and can be used in different ways. In this article, I will show you how to share functions and values by exporting and importing modules in Node. js.
exports is a special object which is included in every JavaScript file in the Node. js application by default. The module is a variable that represents the current module, and exports is an object that will be exposed as a module. So, whatever you assign to module.
It's looking for InsuredSearchPage
package in node_modules
. You need to specify the location of InsuredSearchPage
relative to the directory the file is in:
var InsuredSearchPage = require("./InsuredSearchPage");
The docs have more information on using require()
On top of SomeKittens' answer
var InsuredSearchPage = require("./InsuredSearchPage");
I also had to change the last line in the required file from "module.exports = " to "exports.InsuredSearchPage = InsuredSearchPage"
Node 6.9.2, Protractor 4.0.13
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With