【问题标题】:How to run single e2e test with grunt and protractor如何使用 grunt 和量角器运行单个 e2e 测试
【发布时间】:2014-04-23 09:25:10
【问题描述】:

我假设这是可能的,实际上很简单,但我对 grunt 和 protractor 都是新手,我无法在网上找到答案(可能我使用了错误的搜索条件)。

我在文件 test/e2e/Recipients.js 中有以下 e2e 测试:

describe('Recipients Tab', function() {

    beforeEach(function () {
        browser.get('#/recipients');
    });

    it('should have no e-mail list', function () {
        expect(element(by.css('accordion')).isPresent()).toBe(false);
    });
});

目前,我正在这样做:

grunt e2e

我的量角器配置文件:

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',
    capabilities: {
        'browserName': 'chrome'
    },
    specs: ['../e2e/**/*.js'],
    baseUrl : 'http://localhost:8080/spr',

    jasmineNodeOpts: {
        showColors: true // Use colors in the command line report.
    }
};

当然,这会运行我所有的测试,但在我开发特定测试时,我不想运行整个测试组。我想运行这个文件。

我该怎么做?有没有flag什么的?

谢谢

【问题讨论】:

  • 你的 protractor.conf.js 是什么样的?请将其编辑为您的原始问题
  • 你可以使用specs量角器选项来传递一个逗号分隔的JS文件列表来执行。您需要编辑 Gruntfile.js 以将此选项传递给量角器
  • JB Nizet,我刚刚尝试过,它确实有效。谢谢。想写一个我可以接受的答案吗?

标签: javascript angularjs gruntjs protractor end-to-end


【解决方案1】:

或者,将您的测试组织为一组test suites

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  capabilities: { 'browserName': 'chrome' },

  suites: {
    homepage: 'tests/e2e/homepage/**/*Spec.js',
    search: ['tests/e2e/contact_search/**/*Spec.js']
  },

  jasmineNodeOpts: { showColors: true }
};

并且只运行特定的测试套件,使用--suite 命令行参数:

protractor protractor.conf.js --suite homepage

另请参阅:Protractor for AngularJS

【讨论】:

  • 不知道这个。有用的东西
【解决方案2】:

您只需将 specs 选项传递给量角器 CLI。 specs 选项需要以逗号分隔的 JS 文件列表来运行。

您需要编辑 Gruntfile.js 以将此选项传递给量角器。

【讨论】:

  • grunt e2e --specs='js/test/e2e/PreviewSpec.js' 适合我。谢谢
【解决方案3】:

由于您使用的是 Grunt+Protractor,我建议不要在“protractor.conf.js”中设置单个测试,而是在“Gruntfile.js”中使用“grunt-protractor-runner”Grunt 模块。因此,您可以使用不同的配置设置任意数量的单个或多个测试

基本上,您将其包含在顶部:

   grunt.loadNpmTasks('grunt-protractor-runner');

然后,像这样在 grunt.initConfig 中设置你的任务:

grunt.initConfig({
.....
.....
.....
      protractor: {
      options: {
        configFile: "protractor.conf.js",
        keepAlive: true // If false, the grunt process stops when the test fails.
    },
    singleTestRun: {
        options: {
            args: {
                baseUrl: "http://yourDomain.com", // setting up base URL here
                specs: [
                    './specs/*.js',
                    './another/specs/*.js'
                ],
                capabilities: {
                    'browserName': 'chrome',
                    shardTestFiles: false
                },
            }
        }
    },
},
.....
.....
.....
});

然后,在同一个文件中注册 Grunt 任务:

grunt.registerTask('run-test', ['someTaskOne', 'protractor:singleTestRun', 'shell:someshellscript']);

然后,使用以下命令运行此任务:

grunt run-test

【讨论】:

    【解决方案4】:

    您只是在不需要运行的描述之前添加了 x 前缀。例如,如果您不需要运行测试套件,请按如下方式使用,

    xdescribe('Recipients Tab', function() {
    
    beforeEach(function () {
        browser.get('#/recipients');
    });
    
    it('should have no e-mail list', function () {
        expect(element(by.css('accordion')).isPresent()).toBe(false);
    });
    

    });

    【讨论】:

    • 不知道这个,它很有用,但据我了解,每次我想测试不同的东西时,我都需要在所有其他规范中添加 x。我想要类似于 maven 所做的事情: mvn -Dtest=com.project.MyTestClass test
    • @redwulf 你有没有在量角器中找到任何像 mvn 那样执行单一测试的东西?感谢您的帮助!
    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 2015-06-11
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多