使用 Jenkins 配置 Protractor 的持续集成

43

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

它需要执行以下任务:

  1. 启动selenium独立服务器。
  2. 使用conf.js文件启动测试。
  3. 停止selenium独立服务器。

有人能在这方面提供帮助吗?

5个回答

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的“自由风格项目”(构建 > 执行 shell)中调用的。

enter image description here

然后通过读取Protractor测试结果生成测试报告。因此,您需要从Protractor生成Junit报告(请看这里):

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

输入图片说明


当我尝试使用"jasmine"对象执行"jasmine.getEnv().addReporter(...."部分时,出现了"undefined is not a function"的错误提示。有什么解决方法吗? - chulian
你的依赖项中是否有jasmine? - gontard
1
固定变量:jasmineReporters = require('jasmine-reporters'); .................新的 jasmineReporters.JUnitXmlReporter('protractor_output', true, true, 'testresults.e2e.') 但现在这产生了一个新问题 :( http://stackoverflow.com/questions/24730495/nodejs-protractor-jasmine-junitxmlreporter-runs-the-test-but-without-waiting - chulian
请确保您正在使用 jasmine-reporters v1.0.0 版本,目前不支持更新版本 https://github.com/angular/protractor/blob/master/docs/faq.md#how-do-i-produce-an-xml-report-of-my-test-results - Ben
3
你能否在回答中新增一个部分,介绍在 Jenkins 中使报告可见的下一步操作? - Snekse

12

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

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

然后安装protractor节点模块。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'
]);  
});

我不知道这是否适用于UNIX系统,但它绝对在Windows上无法工作... Selenium在前台运行,因此任务永远不会释放,在“启动Selenium服务器”后挂起... - Thomas Stubbe

7
最新版本的protractor允许您直接从conf.js(或任何其他protractor入口点)运行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

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 服务器上安装 node js

  • 安装 Html Publisher Plugin 以获取端到端测试报告

  • 创建自由风格项目或根据您的需求创建其他类型的项目

  • 进入“构建”部分 -> 添加构建步骤,如果 Jenkins 服务器在 Windows 上,则选择“执行 Windows 批处理命令”,否则选择“执行 Shell”(适用于 Linux)

enter image description here

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

enter image description here

  • 用于报告,请前往“后构建操作”部分 -> 添加“发布HTML报告”,并调用您的报告文件(文件假定来自项目的根目录)

enter image description here

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


0

您可以使用更简单的 Gulp。

Jenkins 系统中安装 gulp 后,您可以通过以下方式在 Jenkins 中直接运行 npm 依赖项(npm install)和 gulp 任务:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接