16

我是 AngularJS 的新手。我正在尝试使用 Protractor 学习并进行一些端到端测试。我一直在浏览这里提供的信息。当我到达“Run with protractor myConf.js”的部分时,我被卡住了。

量角器是命令行程序吗?或者是什么?我要确定的是,我需要在什么环境下运行“protractor myConf.js”。我不想在全球范围内安装量角器。我想在本地环境中运行该模块。这是一个选择吗?

谢谢,

4

13 回答 13

15

您需要通过节点运行它。

所以从你的项目的基础开始;

node node_modules\protractor\bin\protractor test\myConf.js
于 2013-12-17T02:04:56.630 回答
14

您可以通过以下方式全局安装 Protractor:

$ npm install -g protractor

之后它应该可以在命令行上使用(Windows/Linux)

$ protractor protractor.conf.js

仅为当前项目安装:

$ npm install protractor --save-dev

它可以通过node_modules这样的方式运行(Windows/Linux):

$ ./node_modules/.bin/protractor protractor.conf.js

您可以将其添加到您package.json的以便于运行:

"scripts": {
    "test": "./node_modules/.bin/protractor protractor.conf.js"
}

然后后来:

$ npm test
于 2015-04-07T19:42:24.033 回答
11

这些是入门文档:

https://github.com/angular/protractor/blob/master/docs/getting-started.md

您需要在您的机器上安装node.js以及npm节点包。一旦你安装了这两个东西,你就可以按照上面文档中的其他说明进行操作。

在Protractor启动并运行之前,它应该只需要大约 5-10 分钟的安装时间。如果您仍然卡住,请告诉我。

于 2013-11-18T15:18:05.403 回答
10

您应该使用npm-run-all(或concurrently, parallelshell),因为它可以更好地控制启动和终止命令。

一旦npm-run-once, protractor,http-server在本地安装,您可以像这样修改 package.json:

scripts: {
  "webdriver-start": "./node_modules/protractor/bin/webdriver-manager update && ./node_modules/protractor/bin/webdriver-manager start",
  "protractor": "./node_modules/protractor/bin/protractor ./tests/protractor.conf.js",
  "http-server": "./node_modules/http-server/bin/http-server -a localhost -p 8000",
  "python-example": "python -m SimpleHTTPServer",
  "test1": "npm-run-all -p -r webdriver-start http-server protractor",
  "test2": "npm-run-all -p -r webdriver-start python-example protractor"
}

-p = 并行运行命令。

-r = 当其中一个以零结束时终止所有命令。

运行npm run test1将启动 Selenium 驱动程序,启动 http 服务器(为您提供文件)并运行量角器测试。完成所有测试后,它将关闭 http 服务器和 selenium 驱动程序。

于 2016-07-05T21:42:01.363 回答
2

这是使用 Typescript 的示例,但如果不是您的情况,您可以简单地删除所有“tsc”内容。将您的 package.json脚本部分配置为如下所示:

  "scripts": {
    "postinstall": "node node_modules/protractor/bin/webdriver-manager update",
    "pretest": "npm run tsc",
    "test": "npm run eslint && npm run protractor",
    "eslint": "node node_modules/eslint/bin/eslint.js '*.js' 'test/**/*.js' 'test/**/*.ts'",
    "protractor": "node node_modules/protractor/bin/protractor",
    "start": "node node_modules/protractor/bin/webdriver-manager start",
    "tsc": "node node_modules/typescript/bin/tsc"
  }

npm start在一个终端和npm test另一个终端中运行。

于 2017-11-08T17:15:21.863 回答
1

我有一个代码生成器,可以创建一个空的量角器项目。这些说明应该很容易遵循:

https://npmjs.org/package/generator-protractor

于 2013-12-14T03:58:36.797 回答
1

我认为运行量角器的最佳方法是将其安装到您的项目本地,然后使用 npm 脚本运行它。

备份一步,npm 本身使用基于文件系统的层次结构来查找可执行模块。如果你输入npm binnpm 会告诉你它首先会在哪里寻找可执行文件(例如[project]/node_modules/.bin)。如果你在 package.json 中包含 protractor,当你执行 npm 安装时,protractor 会在你的 .bin 目录中为 protractor 和 webdriver-manager 添加一个符号链接。

您可以通过多种方式使用此信息来执行量角器。~~正确~~ 我认为最好的方法是使用 npm 脚本。当你使用 npm 脚本时,npm 会自动从本地 .bin 目录加载量角器。

这是一个例子

package.json
{
  "name": "awesomeapp",
  "version": "1.0.0",
  "devDependencies": {
    "protractor": "latest"
  },
  "scripts": {
    "test-e2e": "protractor protractor.conf",
    "selenium": "webdriver-manager start"
  }
}

所以现在你可以运行你的 selenium 服务器,npm run selenium然后运行你的量角器测试npm run test-e2e

这也是跨平台的,因此如果您使用的是 mac 或 Windows,那么无论哪种方式都可以满足您的需求。

注意: 您可以在这些脚本中执行非跨平台(npm 文档)的操作,因此如果跨平台对您很重要并且您想做任何花哨的事情,我建议您使用shelljs

PPS 不想混淆上面的观点,但 npm 也有 pre 和 post 钩子,因此您可以使用一个命令更新和运行 selenium。

"scripts": {
    "preselenium": "webdriver-manager update",
    "selenium":    "webdriver-manager start"
}

理论上Windows应该支持&&所以你也可以这样做但是ymmv ...

"scripts": {
  "selenium":    "webdriver-manager update && webdriver-manager start"
}
于 2017-01-17T19:59:02.580 回答
0

是的,可以使用以下命令运行量角器:

npm install protractor

然后您可以通过以下方式访问它:

./node_modules/.bin/protractor conf.js

欲了解更多信息,请访问:Protractor - AngularJS 的端到端测试。这是一个非常好的起点。

于 2017-01-26T09:59:25.440 回答
0

如果你有任何套房config.js试试这个

suites: {

  TESTCASES_RELATED_TO_SUITE1: ['TEST_CASES/Suite1/**/*spec.js'],
  TESTCASES_RELATED_TO_SUITE2: ['TEST_CASES/Suite2/**/*spec.js']

},

可以从命令行执行测试,如下所示:

<path>protractor config.js --suite TESTCASES_RELATED_TO_SUITE1

这只会执行一个测试套件。

于 2015-07-17T04:01:09.650 回答
0

我正在使用 IntelliJ 进行量角器测试。另请注意,为此,需要 IntelliJ Ultimate Edition 以及 node.js 和量角器安装。

您可以在 IntelliJ 上的 Protractor Setup中找到详细信息

于 2017-08-15T15:03:37.413 回答
0

首先,您需要从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 文件夹中运行测试并生成报告。

于 2017-10-11T06:44:24.187 回答
0

在这里我们有一个完整的初学者教程:初学者视频量角器

于 2018-03-13T05:23:57.987 回答
0

入门

我们查看了 Protractor 与 Chrome 无头、与 Sauce Labs 集成的多个浏览器。让我们看看测试自动化执行报告,我们如何集成它。

很棒的质量检查

先决条件

npm 与 Node.js 一起分发,这意味着当您下载 Node.js 时,您的计算机上会自动安装 npm。

1. Install nodejs

首先,在您的系统上全局安装量角器:将量角器安装为开发依赖项:

2. run npm install -g Protractor

3. run npm install protractor --save-dev

要手动安装和启动独立的 Selenium 服务器,请使用 Protractor 附带的 webdriver-manager 命令行工具。这将安装服务器和 ChromeDriver。

4. run npm install -g webdriver-manager

5. run updated webdriver-manager

这将启动服务器。你会看到很多输出日志,以 INFO 开头。最后一行将是“信息 - 已启动 org.openqa.jetty.jetty.Server”。

5. run start webdriver-manager

在进行测试会话时让服务器保持运行。在您的配置文件中,将 seleniumAddress 设置为正在运行的服务器的地址。这默认为http://localhost:4444/wd/hub

6. Finally run your script - Protractor<location of your config file>conf.js

构建和测试

结帐 github:https ://github.com/shahing/Protractor-Web-Automation

于 2019-01-18T16:01:12.073 回答