设置
我们使用量角器,量角器 - 黄瓜和角度模拟来处理我们的e2e测试。构建由grunt管理。
我们有两个构建配置; pool
和local
。 pool
是我们的CI环境选择的配置,local
用于调试目的,并设置为在本地计算机上运行。 两个设置之间的主要区别在于local
在本地运行所有内容,而pool
使用远程Selenium服务器(在CI服务器本身之外)。
protractor.conf.js:
exports.config = {
params: {
widgetUrl: 'http://localhost:9980',
vudUrl: 'xxx',
testDelay: 3000,
},
// The timeout for each script run on the browser. This should be longer
// than the maximum time your application needs to stabilize between tasks.
allScriptsTimeout: 150000,
// A base URL for your application under test. Calls to protractor.get()
// with relative paths will be prepended with this.
baseUrl: 'http://localhost:' + (process.env.PORT || '9082'),
// list of files / patterns to load in the browser
specs: ['./e2e/features/*.feature'],
cucumberOpts: {
require: ['./e2e/steps/*.js', './e2e/pageObjects/*.js', './e2e/support/*.js'],
format: 'pretty',
tags: ['@context-test']
},
// Patterns to exclude.
exclude: [],
// ----- Capabilities to be passed to the webdriver instance ----
//
// For a full list of available capabilities, see
// https://code.google.com/p/selenium/wiki/DesiredCapabilities
// and
// https://code.google.com/p/selenium/source/browse/javascript/webdriver/capabilities.js
capabilities: {
browserName: 'chrome',
loggingPrefs: {
'driver': 'INFO',
'server': 'INFO',
'browser': 'INFO'
}
},
// ----- The test framework -----
//
// Jasmine and Cucumber are fully supported as a test and assertion framework.
// Mocha has limited beta support. You will need to include your own
// assertion framework if working with mocha.
framework: 'custom',
frameworkPath: 'node_modules/protractor-cucumber-framework',
mocks: {
dir: "mocks", // path to directory with mocks
default: []
},
onPrepare: function() {
// Chai config
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
global.expect = chai.expect;
console.log('params: ' + JSON.stringify(browser.params));
//browser.driver.manage().window().maximize();
}
};
我们在Before
钩子中配置我们的模拟(为了简洁起见,这里是剪切的):
this.Before(function(scenario, callback) {
// ...
let data = require(`${TEST_DATA_DIR}/default.js`);
let httpMocker = function() {
angular.module('httpMocker', ['ngMockE2E'])
.run(function($httpBackend) {
$httpBackend.whenPOST(...);
};
browser.addMockModule('httpMocker', httpMocker, {data})
// ...
callback();
});
问题
尽管测试设置相同,但在CI环境中运行时,ngMockE2E
不被调用。这可以通过以下测试来证明:
test.feature:
@Test
Feature: Test and debug
@context-test
Scenario: Get console output
Given I access the page
Then I get the log output
test.steps.js
module.exports = function() {
this.Given(/^I access the page$/, () => {
util.waitAndDo(() => true);
});
this.Then(/^I get the log output$/, () => {
util.waitAndDo(() => {
browser.manage().logs().get('browser').then(function(browserLog) {
console.log('log: ' + require('util').inspect(browserLog));
});
})
});
};
此测试将转储浏览器日志,而不是实际测试任何内容。在本地运行时,日志为空。 但是,当在CI环境中运行相同的测试时,日志显示对被模拟的URL的失败调用的错误。
我们已经验证CI环境中使用的网址与我们的模拟中的正则表达式正确匹配,因此它不是匹配项。该模块根本没有被调用。
重申一下 - 配置中唯一的主要区别是pool
配置使用了远程Selenium中心。这怎么会影响我们的测试运行方式,为什么它会阻止我们的模拟工作呢?