简体   繁体   English

使用单个Gulp任务运行Protractor

[英]Run Protractor with a single Gulp task

Is there a way to run e2e tests using Protractor and Gulp in a single task? 有没有办法在单个任务中使用Protractor和Gulp运行e2e测试?

Right now, in order to run e2e tests, I have to open 3 separate shells and run the following: 现在,为了运行e2e测试,我必须打开3个独立的shell并运行以下命令:

webdriver-manager update

webdriver-manager start

npm start (which runs the app server)

protractor protractor.conf.js (in a separate window)

There must be a simpler way of running these tests. 必须有一种更简单的方法来运行这些测试。 Any thoughts? 有什么想法吗?

Here is a solution that can work 这是一个可行的解决方案

var protractor = require("gulp-protractor").protractor;
var spawn = require('child_process').spawn; 

//run webdriver method
function runWebdriver(callback) {
    spawn('webdriver-manager', ['start'], {
        stdio: 'inherit'
    }).once('close', callback);
}

//run protractor.config method
function runProtractorConfig() {
    gulp.src('./**/*-page.spec.js')
        .pipe(protractor({
            configFile: 'protractor.config.js'
        }))
        .on('error', function (e) {
            throw e;
        });
}
//execute protractor.config after webdriver is executed
function runWebdriverProtractor(){
    runWebdriver(runProtractorConfig);
}
//put them into gulp task
gulp.task('run-protractor', runWebdriverProtractor);

Yes it's possible. 是的,这是可能的。

  1. First way: (only for running it from console with simple command " protractor [config file] " 第一种方式:(仅用于通过简单命令“protractor [config file]”从控制台运行它

Just run once command with NPM: 用NPM运行一次命令:

npm install protractor@2.5.1
npm install selenium-standalone-jar@2.45.0

This will install required packages. 这将安装所需的包。

Next modify the protractor.conf.js with selenium standalone instead of selenium address: 接下来用selenium standalone而不是selenium地址修改protractor.conf.js:

//seleniumAddress: 'http://localhost:4444/wd/hub',
seleniumServerJar: './node_modules/selenium-standalone-jar/bin/selenium-server-standalone-2.45.0.jar',

Now when you run protractor [ config file] like: protractor protractor.conf.js he will start selenium for himself and make tests. 现在当你运行protractor [config file]时:protractor protractor.conf.js他将为自己启动selenium并进行测试。

  1. Second way: 第二种方式:

You can implement that into build process, during build with GULP he will fire for you protractor and protractor will fire up selenium-driver himself. 您可以将其实施到构建过程中,在使用GULP进行构建期间,他将为您开发量角器,量角器将自行启动硒驱动器。

I see that its really tiring to you to run those command, why would you do that even, just use Gulp to make it for you. 我看到你执行这些命令真的很累,为什么你会这样做,只需用Gulp为你做。

I will explain you this on working example. 我将以工作实例向您解释这一点。

All you need is: 所有你需要的是:

Add to your (NPM) package.json file: (pick the version that suits you, this works for me. 添加到您的(NPM)package.json文件:(选择适合您的版本,这对我有用。

"gulp-protractor": "^2.1.0",
"protractor": "2.5.1",
"selenium-standalone-jar": "2.45.0",

Then you need the proper config in protractor.conf.js: You need to replace the "seleniumAddress" with direct JAR file so the protractor will fire it for you and attach to it automaticly! 然后你需要在protractor.conf.js中使用正确的配置:你需要用直接的JAR文件替换“seleniumAddress”,这样量角器就会为你激活它并自动附加到它! piece of file is: 一块文件是:

    framework: 'jasmine',
    //seleniumAddress: 'http://localhost:4444/wd/hub',
    seleniumServerJar: './node_modules/selenium-standalone-jar/bin/selenium-server-standalone-2.45.0.jar',

The seleniumServerJar is a reference path to the file which will be installed with "selenium-standalone-jar": "2.45.0", from your package.json file. seleniumServerJar是该文件的引用路径,该文件将与package.json文件中的“selenium-standalone-jar”:“2.45.0”一起安装。

And last step is to properly make a gulp task, here's mine: 最后一步是正确地完成gulp任务,这是我的:

var angularProtractor = require('gulp-angular-protractor');
    gulp.task('protractor-test', function(callback) {
        gulp
            .src(['./**/*.js'])  -< 
            .pipe(angularProtractor({
                'configFile': 'protractor.conf.js',
                'debug': true,
                'autoStartStopServer': true
            }))
            .on('error', function(e) {
                console.log(e);
            })
            .on('end', callback);

});

Enjoy! 请享用!

You can run testsuite as well by just adding --suite in 'args'. 您也可以通过在'args'中添加--suite来运行测试套件。

var angularProtractor = require('gulp-angular-protractor');
gulp.task('test', function (callback) {
     gulp
         .src([__dirname+'/public/apps/adminapp/**/**_test.js'])
         .pipe(angularProtractor({
             'configFile': 'public/apps/adminapp/app.test.config.js',
             'debug': false,
             'args': ['--suite', 'adminapp'],
             'autoStartStopServer': true
         }))
         .on('error', function(e) {
             console.log(e);
         })
        .on('end',callback);
});

A complete solution , (working with Travis CI) 完整的解决方案 (与Travis CI合作)

  1. npm configuration npm配置

Basically, configure http-server to start before you run gulp script. 基本上,在运行gulp脚本之前配置http-server以启动。 Later on you will have to run the tests via npm , fe. 稍后您必须通过npm ,fe运行测试。 npm test or npm run <action> if you choose other name. npm testnpm run <action>如果你选择其他名字。

"devDependencies": {
  "gulp-angular-protractor": "latest",
  "http-server": "^0.9.0",
  ...
},
"scripts": {
  "pretest": "npm install",
  "test": "(npm start > /dev/null &) && (gulp protractor)",

  "start": "http-server -a localhost -p 8000 -c-1 ./",

  "dev": "(npm start > /dev/null &) && (gulp dev)"
},
  1. setup gulp with gulp-angular-protractor gulp-angular-protractor设置gulp

Define the action you want to run. 定义要运行的操作。

var gulpProtractorAngular = require('gulp-angular-protractor');
...
gulp.task('protractor', function (callback) {
    gulp
        .src('tests/*.js')
        .pipe(gulpProtractorAngular({
            'configFile': 'protractor.conf.js',
            'debug': false,
            'autoStartStopServer': true,
            'verbose': false,
            'webDriverUpdate': {
                'browsers': ['ie', 'chrome']
            }
        }))
        .on('error', function (e) {
            console.log(e);
        })
        .on('end', callback);
});

See the above in action in GitHub project: https://github.com/atais/angular-eonasdan-datetimepicker 请参阅GitHub项目中的上述操作: https //github.com/atais/angular-eonasdan-datetimepicker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM