How do I access the current test object before a session is created
What are you trying to achieve?
- I am using webdriverio(helper)+browserstack for test execution.
- Before start of every testcase, using a container object I want to modify the desiredCapabilities.name with the actual Scenario title.
- This modification has to happen before a session is created for the testcase execution.
What do you get instead?
I have tried using :
-
event.test.before . The session is not created yet. . But I don't have access to the current test object.
-
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 ?
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:
- In codecept.json >
"manualStart": true - In custom hook; listen on
event.test.started, - Access the test object & modify the config as per your requirement.
- 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:
- In codecept.json >
"manualStart": true- In custom hook; listen on
event.test.started,- Access the test object & modify the config as per your requirement.
- 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.
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!
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.
There is Browserstack helper. Not sure if it's helpful: https://www.npmjs.com/package/codeceptjs-bshelper
@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 :)
You're right. The test results are sent at the end of execution.
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 👏
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
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