CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

[Question] How to use yield/generator function in custom steps

Open waryhermit opened this issue 9 years ago • 5 comments

...
Scenario('test successful login', function*(I) {

    I.amOnPage('/');
    console.log(yield I.grabCookie('languageFromCookie'));
...

Correctly outputs the cookie. But if I try the same in the steps file:

....
 return actor({

     cookietest: function* (email, password) {
      console.log('ran');
      this.amOnPage('/');
      //console.log(yield this.grabCookie('languageFromCookie'));
    },
...

It doesn't seem to even run. I do I.cookietest("admin", 'test'); and get no output. If I delete the * then it does output ran, but then I can't yield the cookie.

waryhermit avatar Jan 23 '17 22:01 waryhermit

Yes, inside helpers you work with a promise and no magic with generators/yield is implemented. To make function*() { yield } work in async/await manner I recommend to use co package. Install it, and require it inside your helper.

DavertMik avatar Jan 23 '17 22:01 DavertMik

@DavertMik OK, thank you. I am quite new to javascript and it's hard to figure this stuff out from google for some reason. Searches seem to work better for c# to me.

waryhermit avatar Jan 23 '17 22:01 waryhermit

@DavertMik I am stuck again.. I can't seem to make the webdriver promises work with co . For example:

'use strict';
let Helper = codecept_helper;
let I;
var co = require('co');
var Promise = require('bluebird');
var request = require('request');
var loginCount = 1; // Used to check for config page.
var async = require('asyncawait/async');
var await = require('asyncawait/await');
class SauceLabsSession extends Helper {

  /**
     * checks that authentication cookie is set
     */

  getcookies() {
    // access current client of WebDriverIO helper
    let client = this.helpers['WebDriverIO'].browser;

    // get all cookies according to http://webdriver.io/api/protocol/cookie.html
    // any helper method should return a value in order to be added to promise chain
    return client.cookie(function (err, res) {
      // get values
      console.log('here I am');
      return res.value;
    });
  }
  seeAuthentication2() {
    var countFiles1 = co.wrap(function* (client) {
      console.log(yield client.getcookies());
    })
    var test = co.wrap(function* (client) {
      yield Promise.delay(30*1000);
      console.log("30 seconds later...");
    })
    //return test(); -- This works
    return countFiles1(this); // This doesn't work
  }

module.exports = SauceLabsSession;

I never get the message here I am and I get an error: method.toUpperCase is not a function. I tried a basic delay promise from bluebird and it works. Any idea whats wrong with what I am doing?

waryhermit avatar Jan 26 '17 21:01 waryhermit

Okay, I got it working like this:

  getcookies() {
    // access current client of WebDriverIO helper
    let browser = this.helpers['WebDriverIO'].browser;
    //let browser = self.helpers['WebDriverIO'].browser;
    // get all cookies according to http://webdriver.io/api/protocol/cookie.html
    // any helper method should return a value in order to be added to promise chain
    return browser.cookie();
  }

But I still don't know how to wait for things like this.fillField('Login', 'admin') to complete inside a promise / co.wrap.

waryhermit avatar Jan 27 '17 15:01 waryhermit

returning simply a promise doesn't quite work. it waits for the promise's resolution, but yield doesn't return any meaningful value but null instead.

noway avatar Mar 19 '17 20:03 noway