Running node js selenium test case on Chrome get E

2019-05-31 17:14发布

Running a test case on selenium node js got error Error: ECONNREFUSED connect ECONNREFUSED.

Test case

var assert = require('assert'),
    test = require('selenium-webdriver/testing'),
    webdriver = require('selenium-webdriver');
test.describe('Google Search', function () {
    test.it('should work', function (done) {
        this.timeout(100000);
        var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
        driver.get('http://www.google.com');
        var searchBox = driver.findElement(webdriver.By.name('q'));
        searchBox.sendKeys('simple programmer');
        searchBox.getAttribute('value').then(function (value) {
            assert.equal(value, 'simple programmer');
        });
        driver.quit();
        done();
    });
});

And the error stack trace as follows

Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:63528 at ClientRequest. (node_modules\selenium-webdriver\http\index.js:238:15)

1条回答
2楼-- · 2019-05-31 17:51

I'm not sure your example would work, if you want to run the tests locally you would need to install and load the correct selenium drivers.

For chrome I tried to use the var driver = new webdriver.Builder().forBrowser('chrome') syntax with no luck, chrome would fire up but not run tests, I just saw the error you describe. However for FireFox var driver = new webdriver.Builder().forBrowser('firefox').build(); workes perfectly !

I found this works (running locally)

  var assert = require('assert'),
    webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until,
    chrome = require('selenium-webdriver/chrome'),
    firefox = require('selenium-webdriver/firefox');

  var path = require('chromedriver').path;
  var driver = chrome.Driver.createSession(new chrome.Options(), new 
  chrome.ServiceBuilder(path).build());

  driver.get('http://mysite/myapp/tests/functional/start.html');

  //tests here
查看更多
登录 后发表回答