使用Jenkins建立量角器的持续集成

时间:2014-01-24 16:48:11

标签: selenium jenkins continuous-integration protractor

我正在使用Protractor编写自动化测试脚本,现在我需要使用Jenkins为此设置CI。

需要执行的任务是:

  1. 启动selenium standalon服务器。
  2. 使用conf.js文件开始测试。
  3. 停止selenium独立服务器。
  4. 在这方面有人可以提供帮助吗?

5 个答案:

答案 0 :(得分:38)

我创建了一个小的 bash脚本来执行此操作。

# start selenium
./node_modules/protractor/bin/webdriver-manager start > /dev/null 2>&1 &

# wait until selenium is up
while ! curl http://localhost:4444/wd/hub/status &>/dev/null; do :; done

# run the build
grunt cibuild --force

# stop selenium
curl -s -L http://localhost:4444/selenium-server/driver?cmd=shutDownSeleniumServer > /dev/null 2>&1

此脚本是从jenkins中的自由风格项目调用的(Build > Execute shell)

enter image description here

然后通过读取量角器测试结果生成测试结果报告。因此,您必须从量角器生成junit报告(查看here):

onPrepare: function() {
  // The require statement must be down here, since jasmine-reporters
  // needs jasmine to be in the global and protractor does not guarantee
  // this until inside the onPrepare function.
  require('jasmine-reporters');
  jasmine.getEnv().addReporter(
    new jasmine.JUnitXmlReporter('xmloutput', true, true));
},

要在jenkins中显示报告,我在作业中添加了一个帖子构建操作:Publish JUnit test result report

enter image description here

答案 1 :(得分:12)

或者,您可以将其作为Grunt任务运行。首先在Jenkins上安装grunt。安装protractor_webdriver和量角器的NPM包。设置配置文件以指向node_module路径和配置文件路径。

http://sideroad.secret.jp/articles/grunt-on-jenkins/

然后安装量角器节点模块。 Gruntfile看起来与此类似。我创建了一个测试目录,其中包含conf和spec文件。

module.exports = function (grunt) {
  grunt.initConfig({
    protractor_webdriver: {
        your_target: {
            options: {
                path: 'node_modules/protractor/bin/',
                command: 'webdriver-manager start'
            }
        }
    }, 
    protractor: {
        options: {
            configFile: "node_modules/protractor/referenceConf.js", // Default config file
            keepAlive: true, // If false, the grunt process stops when the test fails.
            noColor: false, // If true, protractor will not use colors in its output.
            args: {
            // Arguments passed to the command
            }
        },
        your_target: {
            options: {
                configFile: "test/conf.js", // Target-specific config file
                args: {} // Target-specific arguments
            }
        }
    }
});

grunt.registerTask('p:test', [
    'protractor_webdriver',
    'protractor'
]);  
});

答案 2 :(得分:7)

最新的量角器允许您直接从conf.js(或您拥有的任何量角器入口点)运行selenium独立服务器。

注释掉(或删除)seleniumAddress: 'http://localhost:4444/wd/hub',行,并将其替换为seleniumServerJar: './node_modules/protractor/selenium/latest.jar'

默认情况下,

latest.jar未安装,我将其创建为通过npm install protractor --save安装的最新版本的符号链接。这样可以延长同一目录中conf.js个文件的使用寿命。 在./node_modules/protractor/selenium/文件夹中,我运行了ln -s selenium-server-standalone-2.48.2.jar latest.jar

答案 3 :(得分:0)

你可以使用更简单的Gulp。

installing gulp in Jenkins System之后,您可以安装npm依赖项(npm install)&直接在Jenkins中运行gulp任务作为windows批处理命令,如下所示:

enter image description here  在使selenium服务器启动并运行并提供各种其他参数的后台,您可以在gulpfile.js中使用像'gulp-angular-protractor'这样的包,如下所示:

gulpfile.js

'use strict';

 var gulp = require('gulp'),
 gulpProtractorAngular = require('gulp-angular-protractor'),
 gulpStart = gulp.Gulp.prototype.start,
 currentStartTaskName;

 gulp.Gulp.prototype.start = function (task) {
    currentStartTaskName = task;
    gulpStart.apply(this, arguments);
};
function executeWebTests(suiteName, appName) {
    return gulp.src([])
        .pipe(gulpProtractorAngular({
            'configFile': './conf.js',
            'debug': false,
            'autoStartStopServer': true,
            args: [
                '--suite', suiteName,
                '--capabilities.browserName', 'chrome',
                '--params.APPNAME', appName,
                '--params.SUITENAME', currentStartTaskName,
                '--capabilities.platformName', 'Windows'],
            keepAlive: false
        }))
        .on('error', function (e) {
            console.log('Ended with below ERROR::',e);
            process.exit(1);
        })
        .on('end', function () {
            console.log('Test complete');
            process.exit();
        });
}

gulp.task('RegressionSuiteTask', function () {
    executeWebTests('regressionTests,','Application_Name');
});

<强> conf.js

 suites: {
          regressionTests: ['testCases/**/*.js']//will run all specs in subfolders 
         },

答案 4 :(得分:0)

我知道这已经解决了,并希望针对初学者创建Jenkins作业并运行测试。我建议在配置文件中使用selenium-server-standalone jar并从Jenkins调用配置文件 的 conf.js

    ..  
    exports.config = {
        //seleniumAddress: 'http://localhost:4444/wd/hub',  
        seleniumServerJar: 'node_modules/protractor/node_modules/webdriver-manager/selenium/selenium-server-standalone-3.5.3.jar',
    ....
    //html reporter logic
    .....

创建Jenkins工作

  • 在Jenkins服务器上安装节点js
  • 安装Html Publisher Plugin以获得端到端测试报告

  • 创建自由式项目或任何您需要的

  • 转到构建部分 - &gt;添加构建步骤并选择执行Windows 如果Windows中的Jenkins服务器选择执行,则批处理命令 Shell for Linux

enter image description here

  • 调用conf.js(安装包并调用您的配置文件)

enter image description here

  • 报告获得后建立行动部分 - &gt;添加发布Html 报告并调用您的报告文件(文件假设来自您的 项目)

enter image description here

但是,您可以使用gulp或类似的其他软件包自定义执行命令。谢谢