【问题标题】:Auto Starting Protractor Runner自动启动量角器转轮
【发布时间】:2015-04-29 13:35:35
【问题描述】:

我有一个名为“测试”的 Grunt 任务。此任务的职责是执行端到端测试。目前,如果我在单独的命令行窗口中启动 grunt-protractor-runner,我可以运行我的测试。我通过执行以下命令开始:

node_modules\grunt-protractor-runner\node_modules\protractor\bin\webdriver-manager start

我的问题是,如果 webdriver-manager 尚未启动,有没有办法可以将它作为我的 grunt 任务的一部分启动?如果是这样,怎么做?我见过像 grunt-contrib-connect 这样的任务,但我看不到这些任务如何让我的测试服务器作为任务的一部分运行。

【问题讨论】:

    标签: gruntjs


    【解决方案1】:

    如果你没有在 protractor 配置文件中定义 seleniumAddress,protractor 会帮你启动 selenium 服务器。

    看起来你几乎在那里。您的“测试”任务应该首先使用 grunt-contrib-connect 启动服务器来为您要测试的应用程序提供服务。然后该任务应使用 grunt-protractor-runner 启动量角器,量角器将启动 selenium 服务器(假设 seleniumAddress=null)。

    类似于以下内容:

    connect: {
      test: {
        options: {
          port: 9001,
          base: [
            'app'
          ]
        }
      }
    }
    
    protractor: {
      options: {
        keepAlive: true,
        configFile: 'protractor.conf.js'
      },
      run: {}
    }
    
    grunt.registerTask('test', [
      'connect:test',
      'protractor:run'
    ]);
    

    【讨论】:

    • 感谢您的反馈。不幸的是,我仍然被困住了。现在,当我运行 grunt 文件时,当我的量角器任务运行时出现错误消息:“UnknownCommandError: Cannot POST /wd/hub/session”。我注意到 grunt-contrib-connect 在 0.0.0.0:4444 上启动 Web 服务器。然后量角器任务说它使用 localhost:4444/wd/hub 的 selenium 服务器。我不确定哪个(如果有的话)是正确的。
    • 两个端口相同可能会成为问题。如果 selenium 服务器正常启动,我会感到惊讶,因为此时连接服务器应该已经启动。我假设您将连接服务器配置为在 4444 上启动?尝试将其更改为我上面示例中的 9001 之类的其他内容。
    【解决方案2】:

    要自动启动 webdriver,请将以下内容放入您的 grunt 文件中:

    grunt.initConfig: ({
        ..
        protractor: {
            test: {
                options: {
                    configFile: 'protractor.conf.js'
                }
            }
        },
        ..    
    }    
    ..
    grunt.registerTask('test': ['protractor:test']);
    

    并在您的 ./protractor.conf.js 中关注

        var chromeDriver =  
            './node_modules/protractor/selenium/chromeDriver';
        var platform = require('os').platform();
        var fs = require('fs');
    
        var platformChrome = chromeDriver + '-' + platform;
        if (fs.existsSync(platformChrome)){
            log.console('Using ' + platform + ' specific driver ');
            chromeDriver = platformChrome;
        }
    
        exports.config = {
    
            directConnect: true,
            chromeDriver: chromeDriver,
            // Capabilities to be passed to the webdriver instance
            capabilities: {
                'browserName': 'chrome',
                'chromeOptions': {
                    args: ['--no-sandbox']
                }    
            },
            ..
    }
    

    启动你的 grunt 服务器:

    grunt serve;
    

    然后在另一个终端开始你的测试:

    grunt test
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 2018-04-09
      相关资源
      最近更新 更多