Storing global variable in a separate file for Pro

2020-04-20 11:33发布

问题:

I am trying to create a separate inventory file for Protractor Test where I can store all the reusable variable to be used by different test scrips. The sample Variable list is called Vars.js and the specs should import the variables from this file and consume those. However, this fails as shown below. Can this approach actually be used for storing reusable variables? Can I actually create a separate inventory file for protractor tests outside of conf.js?

Vars.js has the following content :

"use strict";

exports.config = {

function() {

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

}
};

and the spec file is as follows:

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')


})

});

The log

    [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

Update: a revised Vars.js where I used params as shown below also return the same failure.

"use strict";

exports.config = {

params: {

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

}
};

回答1:

The below approach should work for you.

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'
}