Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does one use module.exports and require in protractor tests?

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?

like image 673
epitka Avatar asked May 21 '14 18:05

epitka


People also ask

How does module export work?

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.

What is module in module export?

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.

What is the purpose of module exports in node js explain with an example?

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.


2 Answers

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

like image 191
SomeKittens Avatar answered Oct 19 '22 18:10

SomeKittens


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

like image 1
Denis Avatar answered Oct 19 '22 19:10

Denis