【问题标题】:How to use yield (generators) with selenium webdriver promises?如何使用带有 selenium webdriver 承诺的产量(生成器)?
【发布时间】:2013-11-16 02:19:37
【问题描述】:

我正在尝试在node 0.11.x 中使用生成器来让我的生活更轻松地编写Selenium 测试。我的问题是我不知道如何正确利用它们。我几乎 100% 确定这一定是语法问题。

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

这是一个没有生成器/产量魔法的常规测试:

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

这里有 2 次尝试使用 yield/generator 魔法来获得相同的结果:

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 的 then() 回调中。我必须承认,我对生成器或 Promise 都不太熟悉,所以我可能遗漏了一些非常明显的东西。

【问题讨论】:

  • 您是否正在运行带有 --harmony-generators 标志的节点?
  • 是的,我是。否则我会得到一个 SyntaxError。

标签: javascript node.js selenium-webdriver ecmascript-6 yield-keyword


【解决方案1】:

我想出了以下解决方案。它有效,但我认为它并不理想。我感觉有更好/更简单的方法。

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();
    });

  });

});

【讨论】:

    猜你喜欢
    • 2021-10-28
    • 2018-02-06
    • 2020-10-05
    • 2021-12-30
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    相关资源
    最近更新 更多