This simple test opens firefox browser for me:

 var webdriver = require('selenium-webdriver'); 
 var driver = new
 webdriver.Builder()
     .forBrowser('firefox')
     .build(); driver.get('http://www.google.com/ncr');

But when I try to put this code inside mocha test, firefox is not opened:

describe("simple", function () {
    it("simple", function (done) {
        var webdriver = require('selenium-webdriver'),
            By = webdriver.By,
            until = webdriver.until;

        var driver = new webdriver.Builder()
            .forBrowser('firefox')
            .build();

        driver.get('http://www.google.com/ncr');
    })
});

Code executes fine, webdriver and driver are not nulls, I don't observe any error messages, working folder is the same as in initial test. I use intellij idea mocha configuration for this. How can I fix or diagnose the problem?

You don't need any other test runner than the one you like. You can use Selenium with plain old Mocha, but because of Selenium's special handling of promises (ControlFlow), you have to force the resolution of promises to trigger browser actions:

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

describe("simple", function () {
  it("simple", function (done) {

    var driver = new webdriver.Builder()
        .forBrowser('firefox')
        .build();

    driver.get('http://www.google.com/ncr')
        .then(function() { done(); });
  });
});

To better understand ControlFlow and Promises in Selenium, I recommend reading http://marmelab.com/blog/2016/04/19/e2e-testing-with-node-and-es6.html

  • This has same problem in Intellij IDEA: browser window doesn't open. – Stepan Yakovenko May 15 '16 at 21:47
up vote 0 down vote accepted

To work with selenium, mocha, intellij and nodejs together, you have to use following syntax:

test = require('selenium-webdriver/testing');
var webdriver = require('selenium-webdriver')

test.describe('Simple',function(){
    test.it("test1",function(){
        this.timeout(120000);
        var driver = new webdriver.Builder()
            .forBrowser('firefox')
            .build();
        // do my testing
    }
}

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.