首先您需要从https://nodejs.org/en/download/ 安装node.js,然后使用“npm install -g protractor”安装量角器。这将全局安装量角器。我在本地安装量角器时遇到了问题。最好尝试全局安装。
或者您可以在 package.json 文件中提供所有依赖项,如下所示:
{
"dependencies": {
"protractor": "4.0.3",//any latest versions of these.
"protractor-jasmine2-screenshot-reporter": "0.3.2",
"jasmine-terminal-reporter": "1.0.3"
},
"scripts": {
"postinstall": "node node_modules\\protractor\\bin\\webdriver-manager update"
}
}
在上面的 package.json 文件中也有报告测试结果的依赖项。你需要运行 webdriver-manager 更新。它是一个帮助运行 selenium 服务器实例的工具。
您可以将所有内容放在 package.json 文件中并运行“npm install”来安装所有依赖项。这将为您创建一个“node_modules”文件夹。
现在创建一个配置文件,例如:conf.js。这可能是这样的。
// An example configuration file. There are the reporters which are used to give the test results. There are many reporters. You can use which ever is convenient. The below reporters are for example to show how to configure them.
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var JasmineTerminalReporter = require('jasmine-terminal-reporter');
//To get the Current Date and Time. To make the test output more clear, you can give date.
var currentDate = new Date(),
currentHoursIn24Hour = currentDate.getHours(),
month = currentDate.getMonth() + 1,
totalDateString = currentDate.getDate() + '-' + month + '-' + currentDate.getFullYear() +
'-' + currentHoursIn24Hour + 'h-' + currentDate.getMinutes() + 'm';
var htmlReporter = new HtmlScreenshotReporter({
pathBuilder: function (currentSpec, suites, browserCapabilities) {
'use strict';
return currentSpec._suite.description + totalDateString + '/' + browserCapabilities.get('browserName') + '/' + currentSpec.description;
},
dest: 'TestOutput',
cleanDestination: false,
showSummary: true,
showQuickLinks: true
});
exports.config = {
directConnect: true,//If you make this flag true, it connects the browser directly.
capabilities: {
'browserName': 'chrome'
},
//this is to bring up the test dependencies. Some kind of setup. run once
beforeLaunch: function () {
'use strict';
return new Promise(function (resolve) {
htmlReporter.beforeLaunch(resolve);
});
},
//once per capabilities.
onPrepare: function () {
jasmine.getEnv().addReporter(htmlReporter);
jasmine.getEnv().addReporter(new JasmineTerminalReporter({
isVerbose: true,
showColors: true
}));
},
//A callback function called once all tests have finished running and
// the WebDriver instance has been shut down.
afterLaunch: function (exitCode){
return new Promise(function(resolve){
htmlReporter.afterLaunch(resolve.bind(this, exitCode));
});
},
getPageTimeout: 120000,
allScriptsTimeout: 120000,
specs: ['../TestScripts/*.js']//This contains the test files.
};
完成设置后,创建一个测试文件。测试是使用包含“describe”和“it”的 jasmine 框架编写的。 “describe” 将持有其中包含测试的“it”。你可以通过这个:http://www.protractortest.org/#/
现在使用“protractor conf.js”运行测试。这将在我们在配置文件中设置的 TestOutput 文件夹中运行测试并生成报告。