acuminous / yadda

A BDD javascript library
412 stars 73 forks source link

Async steps with mocha & webdriver #224

Closed ignaciocavina closed 7 years ago

ignaciocavina commented 8 years ago

I'm having issues with async steps, as implemented in the mocha-webdriver example... I'm trying to make some async stuff within my step (ie calling an external service), but I can't find a way to avoid the flow to continue to the next step before my service call finishes. I tried creating a selenium promise but could not get that working. It all works perfect Any clues?

cressie176 commented 8 years ago

I can't really help without a link to some code, but you should know that webdriver.js used by the example has been deprecated in favour of webdriver.io. I should have removed the webdriver.js examples as the will only confuse. Sorry.

I didn't create examples for this as are now multiple web testing frameworks listed here that use yadda under the hood which are easier to get started with. You might still find the nightwatch example useful though.

ignaciocavina commented 8 years ago

Thanks for your response!

I wish I could switch to another stack, but for now we've got a lot of tests working with webdriver.

What I would like to do is something like this: (provided this exectutes using the example in mocha-webdriver)


var Client = require("node-rest-client").Client;
var client = new Client();

given("some service was called", function() {
    client.get("http://localhost/something");
})

I tried using selenium promises, but it did not work:


given("some service was called", function() {
    getSomethingFromClient();
})

function getSomethingFromClient() {
    var d = webdriver.promise.defer();
    client.get("http://localhost/something", function() {
        d.fulfill();
    });
    return d.promise;
}

It does work if I change to async:

given("some service was called", function(next) {
    client.get("http://localhost/something", function() {
        next();
    });
})

But then I loose all the benefits of webdriver ControlFlow?

cressie176 commented 8 years ago

It's a long while since I've done anything with webdriver and even then I found it was an unsupported, poorly documented, royal pita, so I'm probably not going to be much help.

Couple of things struck me as odd though...

  1. Returning d.promise from getSomethingFromClient is meaningless, since it's not referenced or returned from the calling step. Do you need to insert this promise into the control flow chain?
  2. When you say it works if you convert the function to async, but that you would lose the benefits of control flow, what benefits are you referring to? In the mocha-webdriver example each step is already executed in control flow.
cressie176 commented 7 years ago

Any update?

ignaciocavina commented 7 years ago

I ended up doing a driver.wait within my step. I noticed that steps themselves seem to execute / finish sort of "independently" from the "driver" flow. I mean, a step "finishes" in my console even before all the driver commands within it are finished. However, the first command does not execute until the last command in the previous step is finished. Guess the only way to write tests in a "sync" style (no callbacks) is sticking to the driver's control flow.

cressie176 commented 7 years ago

Yeah I think so. Ok to close?

ignaciocavina commented 7 years ago

Yes, sure