简体   繁体   中英

How to export this function in Node.js into another module

I am using protractor to test an Angular JS application. I would like to use page object pattern therefor I need to export a function into another module.

Here is my page-login.js file:

require("./login.spec.js");
var LoginPage = function ()
{
   this.userName = element(by.id('login_form_user_input'));
   this.password =  element(by.id('login_form_password_input')) ;
   this.loginButton = element(by.id('login_form_signin_button'));
   this.loginText = element(by.css('#mainGlobalSearchBtn'));
   this.loginError = element(by.xpath('html/body/div[1]/div[1]/div[1]/form/div/p'));
   this.login = function (userName, password)
       {
         LoginPage.userName.sendKeys(userName);
         LoginPage.password.sendKeys(password);
         LoginPage.loginButton.click ();
         browser.waitForAngular ();
       };
};

 exports.LoginPage = LoginPage;

Now in another js file called login.spec.js under the same folder I would like to call the login this method :

var util = require ('util');
require("./myconfig.js");
describe('Login', function() {
var loginPage = require("./login-page.js");
var ptor;

beforeEach (function ()
{
     ptor = protractor.getInstance();
    ptor.get(settings.winURL);
    waits(2000);
    //Delete cookies to avoid saving password or username
});
it('should not login : incorrect login details', function()
{
     loginPage.login('incorrectusername','incorrectpassword');
     expect(loginPage.loginError.getText()).toContain('Access denied');
});

When Launching this code protractor is not entering username and passwoed in corresponding fields and I got this console ouput :

   1) Login should not login : incorrect login details
   Message:
   TypeError: Object #<Object> has no method 'login'
   Stacktrace:
   TypeError: Object #<Object> has no method 'login'
   at null.<anonymous> (C:\******\login.spec.js:34:20)

here is line 34 that is throwing the error :

loginPage.login('incorrectusername','incorrectpassword');

How can I use the login functioninside the login page function in another module?

The require( './login-page.js' ) returns a "class" function. It means that, to call login method of loginPage module you might to use new . Ex.:

it('should not login : incorrect login details', function()
{
    var page = new loginPage();
    page.login('incorrectusername','incorrectpassword');
    expect(page.loginError.getText()).toContain('Access denied');
});

Remove the require("./login.spec.js"); of the begin of your module. The test is running before the module creation.

Protractor has this awesome option called onPrepare. You put this in your config.js file. Your onPrepare can reference a file.

In this file you can have all the variables and functions you need!

Instead of "this" you use global.

global.userName = element(by.id('login_form_user_input'));
global.password =  element(by.id('login_form_password_input')) ;
global.loginButton = element(by.id('login_form_signin_button'));
global.loginText = element(by.css('#mainGlobalSearchBtn'));
global.loginError = element(by.xpath('html/body/div[1]/div[1]/div[1]/form/div/p'));
global.login = function (user, pw)
   {
     userName.sendKeys(user);
     password.sendKeys(pw);
     loginButton.click ();
     browser.waitForAngular ();
   };

then in your config you do:

//other config settings,
onPrepare: myvariables.js

in your spec, you don't need to require anything!

Just call what you need (minus the global)

beforeEach (function () {
 ptor = protractor.getInstance();
ptor.get(settings.winURL);
waits(2000);
//Delete cookies to avoid saving password or username
});
it('should not login : incorrect login details', function()
{
   login('incorrectusername','incorrectpassword');
 expect(loginError.getText()).toContain('Access denied');
});

you also might want to name your userName variable and the userName arg and password different things. (fixed above)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM