0

I need to do some node require commands in a webdriverJS test script, because these dont get entered into the webdriverJS command queue, I am wrapping them in .then() functions (to deal with the asyncrony)

e.g.

var webdriver = require('selenium-webdriver');

// create webdriver instance so promise chain can be setup 
var promise_builder = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).
                     build();

// wrap all functions in webdriver promises so they get managed by webdrivers
// command queue
promise_builder.sleep(0).then(function() {

    // Run "non-command-queue" commands
    var tests = require('./test_commands');
    tests(helpers, setup, webdriver, driver);

}).then(function(){
    // more non-webdriver commands
});

The problem here (other than the fact its inelegant) is that a browser instance is launched - just to achieve promise chaining.

Is there a better way to create the initial promise, e.g. a static method within the webdriver api for creating promises?

the_velour_fog
  • 2,094
  • 4
  • 17
  • 29

2 Answers2

0

This seems to work:

// create an instance of webdriver.promise.ControlFlow
var flow = webdriver.promise.controlFlow();

// use webdriver.promise.controlFlow#execute() to schedule statements into command queue
flow.execute(function() {

    // Run "non-command-queue" commands
    var tests = require('./test_commands');
    tests(helpers, setup, webdriver, driver);

}).then(function(){
    // more non-webdriver commands
});

An explantion can be found on this Webdriver JS website/docs site, i.e.

At the heart of the promise manager is the ControlFlow class. You can obtain an instance of this class using webdriver.promise.controlFlow(). Tasks are enqueued using the execute() function. Tasks always execute in a future turn of the event loop, once those before it in the queue (if there are any) have completed.

the_velour_fog
  • 2,094
  • 4
  • 17
  • 29
0

I would use webdriver.promise.createFlow(callback) to start a new control flow.

So you'd have something like this:

webdriver.promise.createFlow(function() {
    // Run "non-command-queue" commands
    var tests = require('./test_commands');
    tests(helpers, setup, webdriver, driver);
}).then(function(){
    // more non-webdriver commands
});

Documentation: http://selenium.googlecode.com/git/docs/api/javascript/namespace_webdriver_promise.html

Update

I am now leaning towards the webdriver.promise.controlFlow.execute() option that @the_velour_fog described, since I get errors with after hook failing when new controlFlow is created. Guess creating a new flow messes with mocha async functionality.

Alex
  • 9,250
  • 11
  • 70
  • 81