I am using WebdriverIO for test automation. In wdio.conf.js file I have configured the 'baseUrl' property.
I want to read the 'baseUrl' property value inside my test .js file. How can I do this?
I am using WebdriverIO for test automation. In wdio.conf.js file I have configured the 'baseUrl' property.
I want to read the 'baseUrl' property value inside my test .js file. How can I do this?
On
Use browser.options.baseUrl . If you use require, you're hard coding from that one file, which is fine, but you cannot do a wdio --baseUrl=http://myTestSite2.net to override the "global" baseUrl. Which you might want to do in multiple deployments in the future.
On
just save all your variable in before: function and can be used anywhere in your test. like the following example i use retry count wdio config file
before: function (capabilities, specs) {
expect = require('chai').expect;
should = require('chai').should();
assert = require('assert');
retryCount=2;
browser.maximizeWindow();
On
In wdio.config.js file define the url like this
var baseUrl = 'YOUR URL'
exports.config = {
baseUrl: baseUrl,
}
In Test file use / instead of adding complete url in browser.url('/'), it will use the baseUrl from the wdio.config.js file.
browser.url('/')
On
BaseUrl is available in the config object browser.config.baseUrl See https://github.com/webdriverio/webdriverio/blob/a4a5a46f786f8548361d7f86834b38f89dcb1690/packages/webdriverio/webdriverio-core.d.ts#L131
❒ wdio-v5
Lately, after writing a lot of tests for a project rewrite I've came to believe the best way to store/access global config variables is via the
globalobject.You can define them inside the
wdio.conf.jsfile's hooks. I defined mine in thebeforehook:Then, you can access them directly, anywhere in your test-files, or page-objects. This way, you greatly reduce the test-file's footprint (errr... codeprint) because you can call these directly in your test case:
As opposed to always doing
await browser.options.request.head(...,browser.options.baseUrl, etc.❒ wdio-v4
All the
wdio.conf.jsfile attributes (basically theconfigobject name-value pairs) are also stored inside thebrowser.optionsobject.Thus, a more elegant approach to access your global config values from inside your tests would be as presented below:
I'll go on a limb here and presume you want to read the
baseUrlvalue from yourwdio.config.jsfile, into yourtest.jsfile.TL;DR: In your
test.jsfile heading, add the following:var config = require('<pathToWdioConfJS>/wdio.conf.js').config;You can then access any
wdio.config.jsvalue via theconfig.<configOption>, in your caseconfig.baseUrl.Lastly, I would greatly recommend you read about exports and module exports.
WebdriverIO is built on NodeJS, so you will shoot yourself in the foot on the long run if you don't know how and when to use
exports,module.exports,require, or the difference between them.