CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

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

Open prashantsail opened this issue 8 years ago • 12 comments

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 ?

prashantsail avatar Dec 20 '17 08:12 prashantsail

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

DavertMik avatar Dec 20 '17 14:12 DavertMik

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();

prashantsail avatar Jan 10 '18 05:01 prashantsail

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();
    });
}

timbergfreund avatar Mar 29 '18 10:03 timbergfreund

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.

Vaahin avatar Nov 19 '18 16:11 Vaahin

An old one, I know, but I'm currently facing the same issue.

In my case, it's a React Native app, that runs CodeceptJS + Appium and sends tests to BrowserStack.

I've tried to adapt the suggestions from @DavertMik and the hook from @timbergfreund to no avail. Not sure what I'm missing (except knowledge, that is :) ).

Here's the adjusted hook (with a few console.log to check what's going on):

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

As I'm not sure I must play with WebDriverIO in this case, I've also tried to hook to before, but that caused actual errors in the tests: TypeError: Cannot read property 'switchContext' of undefined.

Any help is welcome!

andreasciamanna avatar Jan 21 '21 11:01 andreasciamanna

It took me five hours, but I have a solution that sends the suite and test name to BrowserStack.

I'm gonna share it here for future reference.

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

const getCapabilities = () => container.helpers().Appium.config.desiredCapabilities

module.exports = function () {
  event.dispatcher.on(event.suite.before, async (suite) => {
    getCapabilities().suite = suite.title
  })
  event.dispatcher.on(event.test.before, async (test) => {
    getCapabilities().name = getCapabilities().suite + ' - ' + test.title
  })
}

I could probably do better, but I was too tired of dealing with this issue :)

I wish I could send the suite as a separate attribute and see the test grouped by suite inside the same build, but, as far as œ can see, there is no such feature in BrowserStack.

andreasciamanna avatar Jan 21 '21 16:01 andreasciamanna

There is Browserstack helper. Not sure if it's helpful: https://www.npmjs.com/package/codeceptjs-bshelper

kobenguyent avatar Jan 21 '21 16:01 kobenguyent

@PeterNgTr Thanks!

I did try using it, but it was throwing unhandled promise errors that were causing me even more confusion. Also, I don't quite like what it does, as it sends the data to BrowserStack (via REST API) after the test run.

I like my solution because it does exactly what I need and the way I need it.

The only thing that it lacks is better and cleaner code, but that can wait :)

andreasciamanna avatar Jan 21 '21 16:01 andreasciamanna

You're right. The test results are sent at the end of execution.

kobenguyent avatar Jan 21 '21 16:01 kobenguyent

It took me five hours, but I have a solution that sends the suite and test name to BrowserStack.

I'm gonna share it here for future reference.

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

const getCapabilities = () => container.helpers().Appium.config.desiredCapabilities

module.exports = function () {
  event.dispatcher.on(event.suite.before, async (suite) => {
    getCapabilities().suite = suite.title
  })
  event.dispatcher.on(event.test.before, async (test) => {
    getCapabilities().name = getCapabilities().suite + ' - ' + test.title
  })
}

I could probably do better, but I was too tired of dealing with this issue :)

I wish I could send the suite as a separate attribute and see the test grouped by suite inside the same build, but, as far as œ can see, there is no such feature in BrowserStack.

You are a legend @andreasciamanna ! I needed a quick solution for this for a POC I'm working on and this was it. Great note for the future reference 👏

roam-qa-alx avatar Mar 01 '21 01:03 roam-qa-alx

It took me five hours, but I have a solution that sends the suite and test name to BrowserStack.

I'm gonna share it here for future reference.

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

const getCapabilities = () => container.helpers().Appium.config.desiredCapabilities

module.exports = function () {
  event.dispatcher.on(event.suite.before, async (suite) => {
    getCapabilities().suite = suite.title
  })
  event.dispatcher.on(event.test.before, async (test) => {
    getCapabilities().name = getCapabilities().suite + ' - ' + test.title
  })
}

I could probably do better, but I was too tired of dealing with this issue :)

I wish I could send the suite as a separate attribute and see the test grouped by suite inside the same build, but, as far as œ can see, there is no such feature in BrowserStack.

works only for the first test case, for the other tests, the names remain the same as the first.

Thairam avatar Nov 21 '23 14:11 Thairam

@Thairam

works only for the first test case, for the other tests, the names remain the same as the first.

Can you provide an example? This works for me, you may be reading a variable from an incorrect scope if you're getting the wrong value

BenMusch avatar Mar 26 '24 17:03 BenMusch