简体   繁体   English

硒webdriver promises如何使用yield(generators)?

[英]How to use yield (generators) with selenium webdriver promises?

I am trying to use generators in node 0.11.x to make my life a bit easier writing Selenium tests. 我正在尝试在node 0.11.x使用生成器,以使编写Selenium测试更加轻松。 My issue is that I don't know how to properly utilize them. 我的问题是我不知道如何正确利用它们。 I am almost 100% sure it must be a syntax issue. 我几乎100%确信这一定是语法问题。

I am using the official selenium-webdriver module (ver 2.37.0), and co (ver 2.1.0) to create my generators. 我正在使用官方的selenium-webdriver模块(版本2.37.0)和co (版本2.1.0)来创建我的生成器。

Here's a regular test with no generator/yield magic: 这是一个常规测试,没有生成器/屈服魔法:

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

Here are 2 attempts trying to get the same result with yield/generator magic: 这是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

As you can see, isPresent is always undefined , except when inside the then() callback of the promise. 如您所见, isPresent始终是undefined ,除非在promise的then()回调中。 I must admit, I am not too familiar with either generators or promises, so I might be missing something very obvious. 我必须承认,我对生成器或Promise不太熟悉,所以我可能缺少一些非常明显的东西。

I came up with the following solution. 我想出了以下解决方案。 It works, but I don't think it's ideal. 它有效,但我认为它不是理想的。 I have a feeling that there is a better/simpler way. 我有一种更好/更简单的方法。

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

  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM