前言
在做前端开发时,总想借助一些方法,让代码更可靠。Vue这块前端的自动化测试主要包括:单元测试、端对端测试(e2e)、和快照测试,今天这里主要记录一下,基于Vue2.0的e2e测试。
依赖安装
在使用Vue-Cli创建Vue2.x的项目时,会有一个选项是否加入e2e测试,这里不建议使用使用这种方法,因为这种方法nightwatch的版本比较老,需要安装额外依赖。这里采用创建项目又自己配置的方法实现,也比较简单。
npm install nightwatch@^1.7.8 --save-dev
npm install nightwatch geckodriver chromedriver --save-dev
实现流程
-
nightwatch.conf.js 在根目录下创建nightwatch.conf.js ,nightwatch的主要配置文件,内容如下:
const Services = {}; loadServices();
module.exports = {
src_folders: ['test/nightwatch/spec'],
page_objects_path: '',
custom_commands_path: '',
custom_assertions_path: '',
globals_path : '',
webdriver: {},
test_settings: {
default: {
disable_error_log: false,
launch_url: 'https://nightwatchjs.org',
screenshots: {
enabled: false,
path: 'screens',
on_failure: true
},
desiredCapabilities: {
browserName : 'chrome'
},
webdriver: {
start_process: true,
server_path: (Services.chromedriver? Services.chromedriver.path : '')
}
},
firefox: {
desiredCapabilities : {
browserName : 'firefox',
alwaysMatch: {
acceptInsecureCerts: true,
'moz:firefoxOptions': {
args: [
]
}
}
},
webdriver: {
start_process: true,
port: 4444,
server_path: (Services.geckodriver ? Services.geckodriver.path : ''),
cli_args: [
]
}
},
chrome: {
desiredCapabilities : {
browserName : 'chrome',
'goog:chromeOptions' : {
w3c: false,
args: [
]
}
},
webdriver: {
start_process: true,
port: 9515,
server_path: (Services.chromedriver ? Services.chromedriver.path : ''),
cli_args: [
]
}
},
edge: {
desiredCapabilities : {
browserName : 'MicrosoftEdge',
'ms:edgeOptions' : {
w3c: false,
args: [
]
}
},
webdriver: {
start_process: true,
server_path: '',
cli_args: [
]
}
},
browserstack: {
selenium: {
host: 'hub-cloud.browserstack.com',
port: 443
},
desiredCapabilities: {
'bstack:options' : {
userName: '${BROWSERSTACK_USER}',
accessKey: '${BROWSERSTACK_KEY}',
}
},
disable_error_log: true,
webdriver: {
timeout_options: {
timeout: 15000,
retry_attempts: 3
},
keep_alive: true,
start_process: false
}
},
'browserstack.local': {
extends: 'browserstack',
desiredCapabilities: {
'browserstack.local': true
}
},
'browserstack.chrome': {
extends: 'browserstack',
desiredCapabilities: {
browserName: 'chrome',
chromeOptions : {
w3c: false
}
}
},
'browserstack.firefox': {
extends: 'browserstack',
desiredCapabilities: {
browserName: 'firefox'
}
},
'browserstack.ie': {
extends: 'browserstack',
desiredCapabilities: {
browserName: 'internet explorer',
browserVersion: '11.0'
}
},
'browserstack.safari': {
extends: 'browserstack',
desiredCapabilities: {
browserName: 'safari'
}
},
'browserstack.local_chrome': {
extends: 'browserstack.local',
desiredCapabilities: {
browserName: 'chrome'
}
},
'browserstack.local_firefox': {
extends: 'browserstack.local',
desiredCapabilities: {
browserName: 'firefox'
}
},
selenium_server: {
selenium: {
start_process: true,
port: 4444,
server_path: (Services.seleniumServer ? Services.seleniumServer.path : ''),
cli_args: {
'webdriver.gecko.driver': (Services.geckodriver ? Services.geckodriver.path : ''),
'webdriver.chrome.driver': (Services.chromedriver ? Services.chromedriver.path : '')
}
}
},
'selenium.chrome': {
extends: 'selenium_server',
desiredCapabilities: {
browserName: 'chrome',
chromeOptions : {
w3c: false
}
}
},
'selenium.firefox': {
extends: 'selenium_server',
desiredCapabilities: {
browserName: 'firefox',
'moz:firefoxOptions': {
args: [
]
}
}
}
}
};
function loadServices() {
try {
Services.seleniumServer = require('selenium-server');
} catch (err) {}
try {
Services.chromedriver = require('chromedriver');
} catch (err) {}
try {
Services.geckodriver = require('geckodriver');
} catch (err) {}
}
- runner.js
根目录下创建test/nightwatch 文件夹,在文件夹下创建runner.js ,让nightwatch明白这个一个vue项目,等待项目启动,具体内容如下:
process.env.NODE_ENV = 'testing'
const webpack = require('webpack')
const DevServer = require('webpack-dev-server')
const webpackConfig = require('../../build/webpack.prod.conf')
const devConfigPromise = require('../../build/webpack.dev.conf')
let server
devConfigPromise.then(devConfig => {
const devServerOptions = devConfig.devServer
const compiler = webpack(webpackConfig)
server = new DevServer(compiler, devServerOptions)
const port = devServerOptions.port
const host = devServerOptions.host
return server.listen(port, host)
})
.then(() => {
let opts = process.argv.slice(2)
if (opts.indexOf('--config') === -1) {
opts = opts.concat(['--config', 'nightwatch.conf.js'])
}
if (opts.indexOf('--env') === -1) {
opts = opts.concat(['--env', 'chrome'])
}
const spawn = require('cross-spawn')
const runner = spawn('./node_modules/.bin/nightwatch', opts, { stdio: 'inherit' })
console.log(runner)
runner.on('exit', function (code) {
server.close()
process.exit(code)
})
runner.on('error', function (err) {
server.close()
throw err
})
})
-
first.js test/nightwatch 下创建spec 文件夹,然后在文件夹内创建first.js ,可以根据自己的情况调整,主要写一些测试内容,其他的测试内容也可以继续写在此文件夹下。
module.exports = {
'my first tests': function (browser) {
browser
.url('http://localhost:8080')
.waitForElementVisible('#app', 5000)
.assert.elementPresent('.legend_wrap')
.assert.containsText('h1', 'Welcome to Your Vue.js App')
.end()
}
}
最终的目录结构如下图所示:
进行测试
在命令窗口中执行以下命令:
nightwatch
出现以下界面就是进行运行成功,并进行了first.js中的测试
注意事项
|