简体   繁体   中英

Selenium-webdriver doesn't open firefox from mocha in nodejs

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?

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
    }
}

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

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