将全局变量存储在单独的文件中以进行量角器测试

时间:2019-01-10 16:09:44

标签: javascript protractor karma-jasmine

我正在尝试为量角器测试创建一个单独的清单文件,在其中可以存储所有可重复使用的变量以供不同的测试条使用。样本变量列表称为Vars.js,规范应从该文件导入变量并使用它们。但是,如下所示失败。这种方法实际上可以用于存储可重用变量吗?我真的可以在conf.js之外为量角器测试创建单独的清单文件吗?

Vars.js具有以下内容:

"use strict";

exports.config = {

function() {

    global.loginMain = 'https://mytestsite.com/auth/login';
    global.TestText = 'I am the test Text';

}
};

,规格文件如下:

require ('./Vars.js')
require('..\\waitAbsent.js')
require("../node_modules/jasmine-expect/index.js")
describe('Vairables Import Test', function() {

console.log(global.loginMain);
console.log(global.TestText);

browser.get(global.loginMain);

it('Text Validation', function(){

expect(browser.getCurrentUrl()).toEqual('https://mytestsite.com/auth/login')


})

});

日志

    [10:55:29] I/local - Selenium standalone server started at http://192.168.1.187:51256/wd/hub
undefined
undefined
Started
(node:17800) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods
instead.
F

Failures:
1) Vairables Import Test encountered a declaration exception
  Message:
    TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type undefined
  Stack:
    TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type undefined
        at Url.parse (url.js:152:11)
        at urlParse (url.js:146:13)
        at Url.resolve (url.js:661:29)
        at Object.urlResolve [as resolve] (url.js:657:40)
        at ProtractorBrowser.get (C:\FCPS_I\FCPS\node_modules\protractor\built\browser.js:653:17)
        at Suite.<anonymous> (C:\FCPS_I\FCPS\TestBed_Scripts\TestBed.js:10:13)
        at Object.<anonymous> (C:\FCPS_I\FCPS\TestBed_Scripts\TestBed.js:5:1)

1 spec, 1 failure

更新:我使用如下所示的params的经过修订的Vars.js也返回了相同的失败。

"use strict";

exports.config = {

params: {

    loginMain: 'https://dss-esy.insystechinc.com/auth/login',
    TestText : 'I am the test Text',

}
};

1 个答案:

答案 0 :(得分:3)

下面的方法应该适合您。

conf.js

exports.config = {
  framework: 'jasmine',
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['app.js'],
  onPrepare: async() => {
      global.globalVariables = require('./globalVariables');
  }
};

app.js

describe('desribe the test', () => {
  it('the it', async () => {
      console.log(globalVariables.loginMain);
      console.log(globalVariables.TestText);
  })
})

globalVariables.js

module.exports = {
  loginMain :'https://mytestsite.com/auth/login',
  TestText : 'I am the test Text'
}