Codeceptjs: How do I access the current test object before a session is created

Created on 20 Dec 2017  路  4Comments  路  Source: codeceptjs/CodeceptJS

What are you trying to achieve?

  1. I am using webdriverio(helper)+browserstack for test execution.
  2. Before start of every testcase, using a container object I want to modify the _desiredCapabilities.name_ with the actual Scenario title.
  3. This modification has to happen before a session is created for the testcase execution.

What do you get instead?

I have tried using :

  1. event.test.before
    . The session is not created yet.
    . But I don't have access to the current test object.

  2. event.test.started(test)
    . I have access to the current test object.
    . But the session is already created and there is no use modifying the config now.

Details

  • CodeceptJS version: 1.0..3
  • NodeJS Version: 6.12.0
  • Operating System: CentOS Linux release 7.4.1708 (Core)
  • WebDriverIO 4.9.11
  • Configuration file:

Is there any other way/event using which I can achieve this ?

Most helpful comment

Thanks @DavertMik !
Your solution worked for me 馃憤

I did the following:

  1. In codecept.json > "manualStart": true
  2. In custom hook; listen on event.test.started,
  3. Access the test object & modify the config as per your requirement.
  4. Start session using > container.helpers().WebDriverIO._startBrowser();

All 4 comments

Yes, it's known problem. We use manualStart option for that: http://codecept.io/helpers/WebDriverIO/#configuration

You will need to enable it, and then create a method that w

manualStart (optional, default: false) - do not start browser before a test, start it manually inside a helper with this.helpers["WebDriverIO"]._startBrowser()

See https://github.com/Codeception/CodeceptJS/pull/255

Thanks @DavertMik !
Your solution worked for me 馃憤

I did the following:

  1. In codecept.json > "manualStart": true
  2. In custom hook; listen on event.test.started,
  3. Access the test object & modify the config as per your requirement.
  4. Start session using > container.helpers().WebDriverIO._startBrowser();

In case someone is looking for a custom hook example:

const event = require('codeceptjs').event;
const container = require('codeceptjs').container;

module.exports = function () {

    event.dispatcher.on(event.test.started, function (test) {
        let index = test.file.lastIndexOf("/"); //get name of testfile
        let build = test.file.substr(index).replace('/', '').replace('_test.js', '');
        container.helpers().WebDriverIO.config.desiredCapabilities['name'] = test.title;
        container.helpers().WebDriverIO.config.desiredCapabilities['build'] = build;
        container.helpers().WebDriverIO.config.desiredCapabilities['project'] = 'testception';
        container.helpers().WebDriverIO._startBrowser();
    });
}

Thanks @DavertMik !
Your solution worked for me 馃憤

I did the following:

  1. In codecept.json > "manualStart": true
  2. In custom hook; listen on event.test.started,
  3. Access the test object & modify the config as per your requirement.
  4. Start session using > container.helpers().WebDriverIO._startBrowser();

Hi Prashant, will you be able to share this custom hook. I am trying the same to set names for browserstack.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hanthomas picture hanthomas  路  4Comments

DioNNiS picture DioNNiS  路  3Comments

jenschude picture jenschude  路  4Comments

davorb picture davorb  路  4Comments

bionicles picture bionicles  路  3Comments