Protractor set global variables
It is possible to set globals from the Protractor config file with the help of params
property:
exports.config = {
// ...
params: {
glob: 'test'
}
// ...
};
And you can access it in the specs using browser.params.glob
.
See the reference config file.
The params object will be passed directly to the Protractor instance, and can be accessed from your test as browser.params. It is an arbitrary object and can contain anything you may need in your test. This can be changed via the command line as:
protractor conf.js --params.glob 'other test'
Update:
From the docs for browser.executeScript
:
If the script is provided as a function object, that function will be converted to a string for injection into the target window. Any arguments provided in addition to the script will be included as script arguments and may be referenced using the arguments object.
So, JavaScript scope in this case does not work, you function that is passed to browser.executeScript
won't have any closure variables from spec like browser
. But you can pass these variables explicitly:
browser.executeScript(function (glob) {
// use passed variables on the page
console.log(glob);
}, browser.params.glob);
Global variables aren't accessible in spec files protractor
params is an object and therefore requires :
to assign the value to myVar, not =
.
exports.config = {
params:{
myVar:'John'
},
}
This is called in the following manner
console.log(browser.params.myVar)
The second method should work as it is. You just need to call it
onPrepare() {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
//this global variable
global.myVar = 10000;
browser.waitForAngularEnabled(false);
browser.driver.manage().window().maximize();
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
}
Called with
console.log(myVar);
Have TS accept global variables defined in Protractor config?
This error is a TypeScript compilation time error that occurs because TS does not know about globals
and the fact that something was added there.
In order to fix that you need to add a declaration for globals somehow or just use assertions.
it('1@tests homepage', function () {
(global as any).logger.info('password for application: ' + pswd);
});
Of course any
can be replace with something more useful.
Another option is to create globals.d.ts
near to tsconfig.json
used for e2e tests and put this line there:
declare const logger: any; // Again, 'any' may be replaced with real type
This way you can access logger
as you wish
it('1@tests homepage', function () {
logger.info('password for application: ' + pswd);
});
You may find some other suitable options in this SO question
Storing global variable in a separate file for Protractor Tests
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'
}
Protractor: initialise Global variable in one js and use the same var in other js
You can dump the variable you need globally onto Protractor browser
object and use it anywhere in the Protractor run Environment
browser.libDir = baseDir + '/lib'
;
And then if you need it in any other test case you can directly use it as browser.libDir
A more professional way to handle this would be declaring global variables in onPrepare()
.
onPrepare: function() {
browser.libDir = baseDir + '/lib'
},
protractor angularJS global variables
Yes, you can easily do that using the onPrepare()hook in the protractor configuration:
exports.config = {
// ...
// A callback function called once protractor is ready and available, and
// before the specs are executed
// You can specify a file containing code to run by setting onPrepare to
// the filename string.
onPrepare: function() {
// you can also add properties to globals here
}
};
Adding global variables via a require Common Function file
This should be possible, I have posted a similar answer before but let me know if you have further concerns about the approach. The approach I took was to require the common function file in the onPrepare as a global variable. This way anything exported from the file is accessible throughout all the tests.
Storing global variable in a separate file for Protractor Tests
Related Topics
Getting a Unhandledpromiserejectionwarning When Testing Using Mocha/Chai
Reactjs: Prevent Multiple Times Button Press
Unsafe JavaScript Attempt to Access Frame with Url
Map Isn't Showing on Google Maps JavaScript API V3 When Nested in a Div Tag
Where Should Ajax Request Be Made in Flux App
Why Are Objects Not Iterable in JavaScript
How to Read Get Data from a Url Using JavaScript
How to Remove Leading and Trailing White Spaces from a Given HTML String
Why Is Bind Slower Than a Closure
Wrapping a Set of Dom Elements Using JavaScript
Javascript/Jquery: Set Values (Selection) in a Multiple Select
How to Set a Cookie with Expire Time
Jest: Timer and Promise Don't Work Well. (Settimeout and Async Function)
Assign Variable in If Condition Statement, Good Practice or Not