Make a 'common' login.js include; with nightwatch.js tests

喜你入骨 提交于 2019-12-11 15:19:46

问题


When writing tests for my web app; I have to first simulate login before the rest of my tests can run and see inner pages. Right now I'm working on modulating the code, so that way I can just make an 'include' for the common function; such as my login. But as soon as I move the below code in a separate file, and call the include via require - it no longer runs as expected.

ie. the below logs in and allows my other functions, if, included in the same file. above my other inner screen functions.

// Login screen, create opportunity

this.LoginScreen = function(browser) {
        browser
                .url(Data.urls.home)
                .waitForElementVisible('#login', 2000, false)
                .click('#login')
                .waitForElementVisible('div.side-panel.open', 4000, false)
                .waitForElementVisible('input#email', 2000, false)
                .waitForElementVisible('input#password', 2000, false)
                .click('input#email')
                .pause(500)
                .setValue('input#email', Data.ProjMan.username)
                .click('input#password')
                .pause(500)
                .setValue('input#password', Data.ProjMan.password)
                .click('input#email') 
                .pause(500)
                .click('div.form.login-form .btn')
                .pause(5000)

        Errors.checkForErrors(browser);
};     

// Inner functions run after here, sequentially

But as soon as I move the above in a separate file, for instance; Logins.js, then call it at the top of the original test file with. (yes, correct path).

var Logins      = require("../../lib/Logins.js");

It just doesn't simulate the login anymore. Any thoughts? Should I remove the this.LoginScreen function wrapper, and call it differently to execute from the external file, or do I need to fire it from the original file again, aside from the external require path?

I have also tried wrapping 'module.exports = {' around the login function from separate file, but still failing.


回答1:


Nightwatch allows you to run your Page object based tests i.e you can externalize your common test functions and use them in your regular tests. This can be achieved using 'page_objects_path' property. I have added the common 'login' functionality and used it in sample 'single test' in the project here.

Working:

Place your common function in .js file and place it under a folder(ex: tests/pages/login.js) and pass the folder path in nighwatch config file as below:

 nightwatch_config = {
      src_folders : [ 'tests/single' ],
      page_objects_path: ['tests/pages'],

Below is an example of common login function (login.js):

var loginCommands = {

  login: function() {
    return this.waitForElementVisible('body', 1000)
      .verify.visible('@userName')
      .verify.visible('@password')
      .verify.visible('@submit')
      .setValue('@userName', 'Enter Github user name')
      .setValue('@password', 'Enter Github password')
      .waitForElementVisible('body', 2000)

  }
};

 module.exports = {
      commands: [loginCommands],
        url: function() {
          return 'https://github.com/login';
        },
      elements: {
        userName: {
          selector: '//input[@name=\'login\']',
          locateStrategy: 'xpath'
        },
        password: {
          selector: '//input[@name=\'password\']',
          locateStrategy: 'xpath'
        },
        submit: {
          selector: '//input[@name=\'commit\']',
          locateStrategy: 'xpath'
        }
      }
    };  

Now, in your regular test file, create an object for the common function as below and use it.

 module.exports = {
      'Github login Functionality' : function (browser) {

    //create an object for login
    var login = browser.page.login();
    //execute the login method from //tests/pages/login.js file
    login.navigate().login();

    //You can continue with your tests below:
    // Also, you can use similar Page objects to increase reusability
        browser
        .pause(3000)
          .end();
      }
    };


来源:https://stackoverflow.com/questions/52120357/make-a-common-login-js-include-with-nightwatch-js-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!