最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在browserstack运行多个nightwatch实例

IT培训 admin 8浏览 0评论

在browserstack运行多个nightwatch实例

我将nightwatch与browserstack集成在一起。当我运行一个实例时,它运行正常。但是当第一个实例正在工作并且有人试图同时运行第二个测试时(在不同的浏览器上或者只是从项目的另一个分支)第一个实例得到错误“无法连接到页面”,所以可能是第一个selenium服务器关闭连接。这有什么办法吗? Ofc我在browserstack中有两个并行测试空间。配置:nightwatch.conf.js

const chromedriver = require('chromedriver');

require('nightwatch-cucumber')({
    cucumberArgs: [
        '--require', './tests/step_definitions/hooks.js',
        '--require', './tests/step_definitions',
        '--format', 'json:reports/cucumber.json',
        '--format-options', '{"colorsEnabled":true}',
        './tests/features'
    ]
});

module.exports = {
    output_folder: 'reports',
    custom_assertions_path: 'tests/assertions',
    custom_commands_path: "./node_modules/nightwatch-commands/commands",
    live_output: true,
    disable_colors: false,
    test_settings: {
        default: {
            launch_url: 'http://pptm_nightwatch:3000',
            selenium_host: 'pptm_hub',
            desiredCapabilities: {
                browserName: 'chrome',
                javascriptEnabled: true,
                acceptSslCerts: true,
                loggingPrefs: {
                    'browser': 'ALL'
                }
            },
            selenium: {
                cli_args: {
                    'webdriver.chrome.driver': chromedriver.path
                }
            },
        },
        chrome: {
            desiredCapabilities: {
                browserName: 'chrome',
                javascriptEnabled: true,
                acceptSslCerts: true,
                loggingPrefs: {
                    'browser': 'ALL'
                }
            },
            selenium: {
                cli_args: {
                    'webdriver.chrome.driver': chromedriver.path
                }
            }
        },
    }
};

nightwatch.browserstack.conf.js

const defaultConfig = require('./nightwatch.conf');

const browserstackConfig = {
  selenium: {
    start_process: false,
    host: 'hub-cloud.browserstack',
    port: 80,
  },

  test_settings: {
    default: {
      desiredCapabilities: {
        'browserstack.user': "bla",
        'browserstack.key': "bla",
        'browserstack.local': true,
      },
      globals: defaultConfig.test_settings.default.globals,
    },
    edge: {
      desiredCapabilities: {
        browser: 'edge',
      },
    },
    chrome: {
        desiredCapabilities: {
            browserName: 'chrome',
            javascriptEnabled: true,
            acceptSslCerts: true,
            loggingPrefs: {
                'browser': 'ALL'
            }
        },

    },
  },
};

const nightwatchConfig = Object.assign({}, defaultConfig, browserstackConfig);

Object.keys(nightwatchConfig.test_settings).forEach((key) => {
  const config = nightwatchConfig.test_settings[key];

  config.selenium_host = nightwatchConfig.selenium.host;
  config.selenium_port = nightwatchConfig.selenium.port;
  config.desiredCapabilities = Object.assign(
    {},
    nightwatchConfig.test_settings.default.desiredCapabilities,
    config.desiredCapabilities,
  );
});

module.exports = nightwatchConfig;

server.js:

var _ = require('lodash'),
    fs = require('fs'),
    path = require('path'),
    express = require('express'),
    cons = require('consolidate'),
    bodyParser = require('body-parser');

var PORT = 3000;

function runServer(done) {
    var app = express();

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));

    app.engine('html', cons.lodash);
    app.set('views', path.join(__dirname, './templates'));
    app.set('view engine', 'html');
    app.get('/page/:pageId', function(req, res){
      res.status(200).render(req.params.pageId, TEMPLATE_OPTIONS);
    });
    app.post('/form-endpoint', function (req, res) {
        res.status(200).send('<body>POST(' + JSON.stringify(req.body)+')</body>');
    });

    var server = app.listen(PORT, function () {
        done()
    });

    return server;
}
module.exports = runServer;

if (require.main === module) {
    runServer(function () {
        console.log("Starting server on port " + PORT + ". Please visit e.g. http://localhost:" + PORT + "/page/formSimple.html");
    });
}
回答如下:

这表示当您的测试仍在执行时,BrowserStackLocal二进制文件将断开连接。似乎问题是由于BrowserStackLocal二元生成多个具有相同修饰符的生成。让我用示例场景解释一下。

  • 测试工程师1在上午10点运行测试(假设此测试运行15分钟):这将在Test Engineer 1的计算机上创建BrowserStackLocal连接并开始测试执行。
  • 测试工程师2在上午10:10运行测试:如果此BrowserStackLocal连接使用相同的密钥,它将断开已在Test Engineer 1的计算机上运行的先前会话,并在Test Engineer 2的计算机上启动本地连接。这可能导致测试工程师1基于所实现的错误处理逻辑运行测试的超时或终止。

按顺序运行测试时,不会影响任何测试,因为上述情况不会出现。

为避免这种情况,请确保在启动BrowserStackLocal时使用localIdentifer参数。

如果您使用命令行启动二进制文件,请使用以下命令:

苹果电脑:

./BrowserStackLocal --key ACCESS_KEY --local-identifier RandomString

视窗:

BrowserStackLocal.exe --key ACCESS_KEY --local-identifier RandomString

在您的测试脚本中,请添加以下功能'browserstack.localIdentifier':'RandomString'

这应该使您的desiredCapabilities块看起来像这样:

 desiredCapabilities: {
    'browserstack.user': "bla",
    'browserstack.key': "bla",
    'browserstack.local': true,
    'browserstack.localIdentifier':'RandomString'
  }

对于每个测试工程师,此RandomString应该是唯一的,以确保他们的测试连接不会重叠或影响其他工程师的连接。

在browserstack运行多个nightwatch实例

我将nightwatch与browserstack集成在一起。当我运行一个实例时,它运行正常。但是当第一个实例正在工作并且有人试图同时运行第二个测试时(在不同的浏览器上或者只是从项目的另一个分支)第一个实例得到错误“无法连接到页面”,所以可能是第一个selenium服务器关闭连接。这有什么办法吗? Ofc我在browserstack中有两个并行测试空间。配置:nightwatch.conf.js

const chromedriver = require('chromedriver');

require('nightwatch-cucumber')({
    cucumberArgs: [
        '--require', './tests/step_definitions/hooks.js',
        '--require', './tests/step_definitions',
        '--format', 'json:reports/cucumber.json',
        '--format-options', '{"colorsEnabled":true}',
        './tests/features'
    ]
});

module.exports = {
    output_folder: 'reports',
    custom_assertions_path: 'tests/assertions',
    custom_commands_path: "./node_modules/nightwatch-commands/commands",
    live_output: true,
    disable_colors: false,
    test_settings: {
        default: {
            launch_url: 'http://pptm_nightwatch:3000',
            selenium_host: 'pptm_hub',
            desiredCapabilities: {
                browserName: 'chrome',
                javascriptEnabled: true,
                acceptSslCerts: true,
                loggingPrefs: {
                    'browser': 'ALL'
                }
            },
            selenium: {
                cli_args: {
                    'webdriver.chrome.driver': chromedriver.path
                }
            },
        },
        chrome: {
            desiredCapabilities: {
                browserName: 'chrome',
                javascriptEnabled: true,
                acceptSslCerts: true,
                loggingPrefs: {
                    'browser': 'ALL'
                }
            },
            selenium: {
                cli_args: {
                    'webdriver.chrome.driver': chromedriver.path
                }
            }
        },
    }
};

nightwatch.browserstack.conf.js

const defaultConfig = require('./nightwatch.conf');

const browserstackConfig = {
  selenium: {
    start_process: false,
    host: 'hub-cloud.browserstack',
    port: 80,
  },

  test_settings: {
    default: {
      desiredCapabilities: {
        'browserstack.user': "bla",
        'browserstack.key': "bla",
        'browserstack.local': true,
      },
      globals: defaultConfig.test_settings.default.globals,
    },
    edge: {
      desiredCapabilities: {
        browser: 'edge',
      },
    },
    chrome: {
        desiredCapabilities: {
            browserName: 'chrome',
            javascriptEnabled: true,
            acceptSslCerts: true,
            loggingPrefs: {
                'browser': 'ALL'
            }
        },

    },
  },
};

const nightwatchConfig = Object.assign({}, defaultConfig, browserstackConfig);

Object.keys(nightwatchConfig.test_settings).forEach((key) => {
  const config = nightwatchConfig.test_settings[key];

  config.selenium_host = nightwatchConfig.selenium.host;
  config.selenium_port = nightwatchConfig.selenium.port;
  config.desiredCapabilities = Object.assign(
    {},
    nightwatchConfig.test_settings.default.desiredCapabilities,
    config.desiredCapabilities,
  );
});

module.exports = nightwatchConfig;

server.js:

var _ = require('lodash'),
    fs = require('fs'),
    path = require('path'),
    express = require('express'),
    cons = require('consolidate'),
    bodyParser = require('body-parser');

var PORT = 3000;

function runServer(done) {
    var app = express();

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));

    app.engine('html', cons.lodash);
    app.set('views', path.join(__dirname, './templates'));
    app.set('view engine', 'html');
    app.get('/page/:pageId', function(req, res){
      res.status(200).render(req.params.pageId, TEMPLATE_OPTIONS);
    });
    app.post('/form-endpoint', function (req, res) {
        res.status(200).send('<body>POST(' + JSON.stringify(req.body)+')</body>');
    });

    var server = app.listen(PORT, function () {
        done()
    });

    return server;
}
module.exports = runServer;

if (require.main === module) {
    runServer(function () {
        console.log("Starting server on port " + PORT + ". Please visit e.g. http://localhost:" + PORT + "/page/formSimple.html");
    });
}
回答如下:

这表示当您的测试仍在执行时,BrowserStackLocal二进制文件将断开连接。似乎问题是由于BrowserStackLocal二元生成多个具有相同修饰符的生成。让我用示例场景解释一下。

  • 测试工程师1在上午10点运行测试(假设此测试运行15分钟):这将在Test Engineer 1的计算机上创建BrowserStackLocal连接并开始测试执行。
  • 测试工程师2在上午10:10运行测试:如果此BrowserStackLocal连接使用相同的密钥,它将断开已在Test Engineer 1的计算机上运行的先前会话,并在Test Engineer 2的计算机上启动本地连接。这可能导致测试工程师1基于所实现的错误处理逻辑运行测试的超时或终止。

按顺序运行测试时,不会影响任何测试,因为上述情况不会出现。

为避免这种情况,请确保在启动BrowserStackLocal时使用localIdentifer参数。

如果您使用命令行启动二进制文件,请使用以下命令:

苹果电脑:

./BrowserStackLocal --key ACCESS_KEY --local-identifier RandomString

视窗:

BrowserStackLocal.exe --key ACCESS_KEY --local-identifier RandomString

在您的测试脚本中,请添加以下功能'browserstack.localIdentifier':'RandomString'

这应该使您的desiredCapabilities块看起来像这样:

 desiredCapabilities: {
    'browserstack.user': "bla",
    'browserstack.key': "bla",
    'browserstack.local': true,
    'browserstack.localIdentifier':'RandomString'
  }

对于每个测试工程师,此RandomString应该是唯一的,以确保他们的测试连接不会重叠或影响其他工程师的连接。

发布评论

评论列表 (0)

  1. 暂无评论