Issue when using gulp to run protractor and seleni

2019-05-10 06:59发布

问题:

I am new to protractor and also new to gulp task.I had gulp file which looks like this:

    'use strict';
var global = {
    app_files: {
        spec: './e2e/**/*_spec.js'
    },
    folders: {
        specs: './specs'
    }
};
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var stylish = require('jshint-stylish');
var beautify = require('gulp-jsbeautifier');
var protractor = require('gulp-protractor').protractor;
// Download and update the selenium driver
var webdriver_update = require('gulp-protractor').webdriver_update;
var webdriver_standalone = require('gulp-protractor').webdriver_standalone;
// Downloads the selenium webdriver
gulp.task('webdriver_update', webdriver_update);
// Runs the selenium webdriver
gulp.task('webdriver_standalone', webdriver_standalone);
// Lint spec files
gulp.task('lint', function() {
    return gulp.src(global.app_files.specs).pipe(jshint()).pipe(jshint.reporter(stylish)).pipe(jshint.reporter('fail'));
});
// Beautify spec files
gulp.task('beautify', function() {
    return gulp.src(global.app_files.specs).pipe(beautify({
        config: '.jsbeautifyrc'
    })).pipe(gulp.dest(global.folders.specs));
});
gulp.task('e2e:local', ['lint', 'webdriver_update'], function() {
    gulp.src([global.app_files.specs], {
        read: false
    }).pipe(protractor({
        configFile: 'protractor.conf.js'
    })).on('error', function(e) {
        throw e;
    });
});
gulp.task('e2e', ['e2e:local']);

But when I run in protractor with - gulpe2e:local I got the following error:

MacBook-Pro:spec sab$ protractor protractor.conf.js -gulpe2e:local
Using the selenium server at http://127.0.0.1:4444/wd/hub
[launcher] Running 1 instances of WebDriver

/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/http/index.js:174
      callback(new Error(message));
               ^
Error: ECONNREFUSED connect ECONNREFUSED 127.0.0.1:4444
    at ClientRequest.<anonymous> (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/http/index.js:174:16)
    at emitOne (events.js:77:13)
    at ClientRequest.emit (events.js:169:7)
    at Socket.socketErrorListener (_http_client.js:265:9)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at emitErrorNT (net.js:1256:8)
    at nextTickCallbackWith2Args (node.js:455:9)
    at process._tickCallback (node.js:369:17)
From: Task: WebDriver.createSession()
    at Function.webdriver.WebDriver.acquireSession_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:157:22)
    at Function.webdriver.WebDriver.createSession (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:131:30)
    at [object Object].Builder.build (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/builder.js:445:22)
    at [object Object].DriverProvider.getNewDriver (/usr/local/lib/node_modules/protractor/lib/driverProviders/driverProvider.js:38:7)
    at [object Object].Runner.createBrowser (/usr/local/lib/node_modules/protractor/lib/runner.js:186:37)
    at /usr/local/lib/node_modules/protractor/lib/runner.js:276:21
    at _fulfilled (/usr/local/lib/node_modules/protractor/node_modules/q/q.js:797:54)
    at self.promiseDispatch.done (/usr/local/lib/node_modules/protractor/node_modules/q/q.js:826:30)
    at Promise.promise.promiseDispatch (/usr/local/lib/node_modules/protractor/node_modules/q/q.js:759:13)
    at /usr/local/lib/node_modules/protractor/node_modules/q/q.js:525:49
[launcher] Process exited with error code 1

Any help will be really appreciated.

Thanks

Sabbu

回答1:

The error indicates that protractor is attempting to connect to the webdriver, and the TCP connection is refused. This is probably because the webdriver is not running.

Your gulp file defines a task for starting the webdriver:

// Runs the selenium webdriver
gulp.task('webdriver_standalone', webdriver_standalone);

To run it:

gulp webdriver_standalone

Leave it running, and then run the gulp task to start the tests.

Alternatively, you can let protractor start and stop the webdriver for you. From the documentation:

You have 2 options to start the selenium server.

The first one is to let Protractor handle it automatically, including stopping it once your tests are done. To do that, simply point to the selenium jar in the protractor config file (you will need to update the version number accordingly) instead of the address:

// The file path to the selenium server jar ()
seleniumServerJar: './node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar',
// seleniumAddress: 'http://localhost:4444/wd/hub',