[Question] How to use yield/generator function in custom steps
...
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.
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 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.
@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?
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.
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.