如何使用selenium webdriver承诺产量(发电机)?

我正在尝试在node 0.11.x使用生成器,以使我的生活更轻松地编写Seleniumtesting。 我的问题是,我不知道如何正确使用它们。 我几乎100%肯定它必须是一个语法问题。

我使用官方的selenium-webdriver模块(版本2.37.0)和co (版本2.1.0)来创build我的生成器。

这是一个没有发生器/产量魔法的常规testing:

 driver.isElementPresent(wd.By.css('.form-login')).then(function (isPresent) { console.log(isPresent); // true }); 

这里有两个尝试获得与yield / generator magic相同的结果的尝试:

 var isPresent = yield browser.isElementPresent(wd.By.css('.form-login')); console.log(isPresent); // undefined var isPresent = yield browser.isElementPresent(wd.By.css('.form-login')).then(function (isPresent) { console.log(isPresent); // true }); console.log(isPresent); // undefined 

正如你所见, isPresent总是undefined ,除非在promise的callbackisPresent中。 我必须承认,我不太了解发电机或承诺,所以我可能会错过一些非常明显的东西。

我想出了以下解决scheme。 它有效,但我不认为这是理想的。 我有一种感觉,有一个更好/更简单的方法。

 describe("The login form", function() { it("should have an email, password and remember me fields and a submit button", function *() { var results = []; yield browser.isElementPresent(wd.By.css('.form-login')) .then(function (isPresent) { results.push(isPresent); }); yield browser.isElementPresent(wd.By.css('.form-login input[name="email"]')) .then(function (isPresent) { results.push(isPresent); }); yield browser.isElementPresent(wd.By.css('.form-login input[name="password"]')) .then(function (isPresent) { results.push(isPresent); }); yield browser.isElementPresent(wd.By.css('.form-login button[type="submit"]')) .then(function (isPresent) { results.push(isPresent); }); results.forEach( function (result) { result.must.be.true(); }); }); });