WebdriverIO: How to read baseURL value from wdio.conf.js. inside step definition file
Asked Answered
S

6

10

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?

Shakta answered 15/6, 2017 at 15:23 Comment(1)
You want to use the one from wdio.config.js file, or from test.js? You wrote it so sloppy I cannot tell what you want. Please edit your question and be more specific.Boorish
B
12

❒ 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 global object.

You can define them inside the wdio.conf.js file's hooks. I defined mine in the before hook:

before: function (capabilities, specs) {
  // =================
  // Assertion Library
  // =================
  const chai    = require('chai');
  global.expect = chai.expect;
  global.assert = chai.assert;
  global.should = chai.should();
  // ======================
  // Miscellaneous Packages
  // ======================
  global.langCode = langCode;
  global.countryCode = countryCode;
  global.request = require('superagent');
  global.allowedStatusCodes = [200, 301],
  // ===============
  // Custom Commands
  // ===============
  require('./test/custom_commands/aFancyMethod');
  require('./test/custom_commands/anotherOne');
  require('./test/custom_commands/andAnotherOne');
},

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:

describe(`Testing a random URL`, () => {
  it('Should return a HTTP valid status code', async () => {
    // Issue a HTTP request for the given URL:
    await request
      .head('https://random.org')
      .then(res => {
        console.info(`\n> Status code found: ${res.status} | MIME type found: '${res.type}'\n`);
        foundStatusCode = res.status;
      })
      .catch(err => {
        console.info(`\n> Status code found: ${err.status} | Error response found: '${JSON.stringify(err.response)}'\n`);
        foundStatusCode = err.status;
      });
    // Assert the HTTP Status Code:
    assert.include(allowedStatusCodes, foundStatusCode, `!AssertError: Route yields a bad status code! Got: ${foundStatusCode} | Expected: ${allowedStatusCodes}`);
  }); 

As opposed to always doing await browser.options.request.head(..., browser.options.baseUrl, etc.


❒ wdio-v4

All the wdio.conf.js file attributes (basically the config object name-value pairs) are also stored inside the browser.options object.

Thus, a more elegant approach to access your global config values from inside your tests would be as presented below:

> browser.options
{ port: 4444,
  protocol: 'http',
  waitforTimeout: 10000,
  waitforInterval: 500,
  coloredLogs: true,
  deprecationWarnings: false,
  logLevel: 'verbose',
  baseUrl: 'http://localhost',
  // ... etc ...
}

> browser.options.baseUrl
'http://localhost'

I'll go on a limb here and presume you want to read the baseUrl value from your wdio.config.js file, into your test.js file.

TL;DR: In your test.js file heading, add the following:

var config = require('<pathToWdioConfJS>/wdio.conf.js').config;

You can then access any wdio.config.js value via the config.<configOption>, in your case config.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.

Boorish answered 15/6, 2017 at 15:39 Comment(2)
Thankyou iamdanchiv :)Shakta
Thanks this is the best explanation I've seen on how to access variables from the config files!Ozonosphere
P
7

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.

Pivoting answered 17/7, 2018 at 7:43 Comment(0)
B
2

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

Blinking answered 21/9, 2020 at 22:34 Comment(1)
I find browser.config is undefined, (but browser.options is set), I'm using wdio v8.39 and the local-runner and mocha-frameworkMisgiving
C
1

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('/')
Conchoid answered 14/11, 2019 at 11:26 Comment(0)
C
1

You can use browser.options.baseUrl

Cathodoluminescence answered 16/5, 2023 at 20:21 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Wolof
A
0

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();
Admirable answered 6/9, 2019 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.