简体   繁体   中英

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. My issue is that I don't know how to properly utilize them. I am almost 100% sure it must be a syntax issue.

I am using the official selenium-webdriver module (ver 2.37.0), and co (ver 2.1.0) to create my generators.

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:

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. I must admit, I am not too familiar with either generators or promises, so I might be missing something very obvious.

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

  });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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