Codecept helper returning data
Hi Guys,
If we resolve a promise with some data from a codecept helper, how do we access the returned data?
Thanks
By using generator and yield:
Scenario('so something with data', function*() {
// ...
let data = yield I.getDataFromHelper();
});
I may oversee something, but I can't get it to work. My helper waits for an email and returns a promise
return new Promise((resolve) => {
mailListener.on("mail", (mail) => resolve(getUrlFromText(mail)))
})
and inside the test file I have
Scenario("I signup", function* (I) {
...
let url = yield I.clickEmailVerificationLink()
I.amOnPage(url)
})
However, the url, even though resolved correctly inside the promise, returns undefined.
@jhartma I don't see any errors in your code. Everything looks ok.
Here is wait implementation from Nighmare helper:
wait(sec) {
return new Promise(function (done) {
setTimeout(done, sec * 1000);
});
}
could you try to console log all intermediate results?
@DavertMik thanks for the quick response.
the helper return promise is logging the fetched url
return new Promise((resolve) => {
mailListener.on("mail", (mail) => {
const url = getUrlFromText(mail)
console.log(url)
resolve(url)
})
})
This is the test
Scenario("I signup @dev", function* (I) {
I.fillField(".input-username", "username")
I.fillField(".input-email", "[email protected]")
I.fillField(".input-password1", "password")
I.fillField(".input-password2", "password")
I.click(".button-create-account")
I.wait(1)
let url = yield I.clickEmailVerificationLink()
console.log("Url", url)
I.amOnPage(url)
And this is the output
Signup Page --
I signup @dev
• I fill field ".input-username", "username"
http://localhost:3000/confirm-account/305e31d68ef744feacf978cb002236ede2aede8e
• I fill field ".input-email", "[email protected]"
• I fill field ".input-password1", "password"
• I fill field ".input-password2", "password"
• I click ".button-create-account"
• I wait 1
Url undefined
✖ FAILED in 5399ms
It's a bit funny, the grab helpers return the data just fine, but no matter what I try in my custom helpers, even simple Promises such as
return new Promise((done) => done(4))
always returns undefined
@jhartma did you ever figure this out?
yes, it was a really stupid error. The code was sitting in a step_file instead of a helper file