CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

Codecept helper returning data

Open shamusharkin opened this issue 9 years ago • 7 comments

Hi Guys,

If we resolve a promise with some data from a codecept helper, how do we access the returned data?

Thanks

shamusharkin avatar Jan 05 '17 12:01 shamusharkin

By using generator and yield:

Scenario('so something with data', function*() {
  // ...
  let data = yield I.getDataFromHelper();
});

DavertMik avatar Jan 10 '17 12:01 DavertMik

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 avatar Jan 16 '17 19:01 jhartma

@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 avatar Jan 16 '17 19:01 DavertMik

@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

jhartma avatar Jan 16 '17 20:01 jhartma

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 avatar Jan 17 '17 18:01 jhartma

@jhartma did you ever figure this out?

c0bra avatar Feb 27 '17 22:02 c0bra

yes, it was a really stupid error. The code was sitting in a step_file instead of a helper file

jhartma avatar Mar 19 '17 11:03 jhartma