如何运行量角器

时间:2013-11-18 14:28:14

标签: angularjs protractor

我是AngularJS的新手。我正在尝试学习并使用Protractor进行一些端到端的测试。我一直在查看提供的信息here。当我到达“用protractor myConf.js跑”的部分时,我陷入了困境。

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

谢谢,

13 个答案:

答案 0 :(得分:15)

您需要通过节点运行它。

所以从项目的基础开始;

node node_modules\protractor\bin\protractor test\myConf.js

答案 1 :(得分:11)

您可以通过以下方式全局安装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

答案 2 :(得分:10)

这些是入门文档:

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

您需要在计算机上安装node.js以及npm节点包。安装完这两件事后,您可以按照上述文档中的其他说明进行操作。

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

答案 3 :(得分:9)

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

在本地安装npm-run-onceprotractorhttp-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驱动程序。

答案 4 :(得分: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

答案 5 :(得分:1)

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

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

答案 6 :(得分:1)

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

备份一步,npm本身使用基于文件系统的层次结构来查找可执行模块。如果您输入npm bin,npm会告诉您它首先要查找可执行文件(例如[project]/node_modules/.bin)。如果你在package.json中包含量角器,当你进行npm安装时,量角器会在你的.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"
  }
}

现在您可以使用npm run selenium运行您的selenium服务器,然后使用npm run test-e2e运行您的量角器测试。

这也是跨平台的,所以如果您使用的是Mac或Windows,您将被覆盖。

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

P.P.S。没有想要弄清楚上面这一点,但是npm也有前后挂钩,所以你可以用一个命令更新和运行selenium。

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

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

"scripts": {
  "selenium":    "webdriver-manager update && webdriver-manager start"
}

答案 7 :(得分: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

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

答案 8 :(得分:0)

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

areTherePossibleValues <- function(someString){
    someCode
}

areTherePossibleValues("x<1 & x>2")
[1] FALSE

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

npm install protractor

如需了解更多信息,请访问:Protractor - end-to-end testing for AngularJS。这是一个非常好的起点。

答案 9 :(得分:0)

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

您可以在Protractor Setup on IntelliJ

找到详细信息

答案 10 :(得分: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”文件夹。

现在创建一个配置文件ex: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文件夹中生成报告。

答案 11 :(得分:0)

这里我们有一个完整的初学者教程:Protractor for beginners video

答案 12 :(得分:0)

入门

我们研究了无头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 Server,请使用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