使“普通" login.js包含;与nightwatch.js测试 [英] Make a 'common' login.js include; with nightwatch.js tests

查看:115
本文介绍了使“普通" login.js包含;与nightwatch.js测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我的Web应用编写测试时;在运行其余测试并查看内部页面之前,我必须首先模拟登录.现在,我正在调制代码,这样我就可以为通用函数创建一个"include".例如我的登录名.但是,一旦我将下面的代码移到一个单独的文件中,并通过require调用include,它将不再按预期运行.

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

但是,例如,一旦我将以上内容移到一个单独的文件中; Logins.js,然后在原始测试文件的顶部使用进行调用. (是的,正确的路径).

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");

它不再模拟登录了.有什么想法吗?我应该删除this.LoginScreen函数包装器,并以不同的方式调用它以从外部文件执行,还是需要从外部文件中再次触发它(除了外部require路径之外)?

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?

我还尝试过将'module.exports = {'包裹在来自单独文件的登录函数周围,但是仍然失败.

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

推荐答案

Nightwatch允许您运行基于Page对象的测试,即,您可以外部化常见的测试功能并在常规测试中使用它们.这可以通过使用' page_objects_path 属性来实现.我添加了常见的登录"功能,并在项目此处.

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.

工作:

将常用功能放置在.js文件中,并将其放置在文件夹下(例如:tests/pages/login.js),并将文件夹路径传递到nighwatch配置文件中,如下所示:

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'],

以下是常见登录功能(login.js)的示例:

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

这篇关于使“普通" login.js包含;与nightwatch.js测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆