如何制作全局可访问变量? [英] How to make a globally accessible variable?

查看:117
本文介绍了如何制作全局可访问变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在nightwatch.js中创建可全局访问的变量?我正在使用一个变量来存储自定义的url(具体取决于我们的在线产品中加载的商店),但是我需要可以通过多个javascript函数对其进行访问.尽管它在文件的开头在函数外部声明,但似乎在每个函数结束后都会重置它的值.

How can I make a globally accessible variable in nightwatch.js? I'm using a variable to store a customized url (dependent on which store is loaded in our online product), but I need it to be accessible across several javascript functions. It appears the value of it resets after each function ends, despite it being declared outside of the function at the head of the file.

推荐答案

自从您提出问题以来已有一段时间了,对于您所请求的内容的支持可能以前(本来就没有)可用.现在是了.

It's been some time since you asked your question and support for what you requested might not have been (natively) available before. Now it is.

在开发人员指南中,根据您的需要,提供了两种方法来创建可从任何给定测试访问的全局变量.请参阅此处,以获取良好的阅读效果.

In the developer guide two methods are provided for creating global variables accessible from any given test, depending on your needs. See here for good reading.

方法1: 对于真正的全局全局人员,即所有测试和所有环境.在nightwatch.json文件的"globals_path"部分(即

Method 1: For truly global globals, that is, for all tests and all environments. Define an object, or pass a file, at the "globals_path" section of your nightwatch.json file, i.e.

"globals_path": "./lib/globals.js",

但是,您将需要导出变量,因此对Node进行梳理是一个好主意.这是一个基本的globals.js文件示例:

You will need to export the variables, however, so brushing up on Node is a good idea. Here is a basic globals.js file example:

var userNames = {
  basicAuth: 'chicken',
  clientEmail: 'SaddenedSnail@domain.com',
  adminEmail: 'admin@domain.com',
};

module.exports = {
  userNames: userNames
}

此对象/文件将在所有测试中用于 all 的任何环境,除非您指定了其他文件/对象,如下面的方法2所示.

This object/file will be used for all of your tests, no matter the environment, unless you specify a different file/object as seen in method 2 below.

要从您的测试套件中访问变量,请使用传递给每个函数(测试)的强制性浏览器/客户端变量,即:

To access the variables from your test suite, use the mandatory browser/client variable passed to every function (test), i.e:

 'Account Log In': function accLogin(client) {
    var user = client.globals.userNames.clientEmail;

    client
      .url(yourUrl)
      .waitForElementVisible('yourUserNameField', 1000)
      .setValue('yourUserNameField', user)
      .end();
}

方法2: 对于基于环境的全局变量,其随您指定的环境而变化.在nightwatch.json文件的"globals"部分中定义一个对象或传递一个文件,该文件嵌套在所需的环境下.即

Method 2: For environment based globals, which change depending on the environment you specify. Define an object, or pass a file, at the "globals" section of your nightwatch.json file, nested under your required environment. I.e.

"test_settings" : {
    "default" : {
      "launch_url" : "http://localhost",
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",
      "globals": {
        "myGlobal" : "some_required_global"
      }
    }
}

请注意,在编写本文时,守夜表似乎存在错误,因此使用方法2传递文件不起作用(至少在我的环境中).可以在此处找到有关该错误的更多信息.

Please note that at the time of writing, there seems to be a bug in nightwatch and thus passing a file using Method 2 does not work (at least in my environment). More info about said bug can be found here.

这篇关于如何制作全局可访问变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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