user2871401
user2871401

Reputation: 1877

How to run Protractor

I'm new to AngularJS. I'm trying to learn and do some end-to-end tests with Protractor. I've been going through the information provided here. I'm stuck when I get to the part that says "Run with protractor myConf.js".

Is protractor a command-line program? Or what? What I'm trying to identify is, under what environment do I need to run "protractor myConf.js" within. I do NOT want to install protractor globally. I would like to run the module in a local context. Is that an option?

Thank you,

Upvotes: 16

Views: 61268

Answers (13)

Shahin
Shahin

Reputation: 29

Getting Started

We looked upon Protractor with Chrome headless, multiple browsers integrating with Sauce Labs. Lets look at the test automation execution reports, how we can integrate it.

awesome QA

Pre requisite

npm is distributed with Node.js- which means that when you download Node.js, you automatically get npm installed on your computer.

1. Install nodejs

First, install protractor globally on your system: install protractor as a development dependency:

2. run npm install -g Protractor

3. run npm install protractor --save-dev

To install and start the standalone Selenium Server manually, use the webdriver-manager command line tool, which comes with Protractor. This will install the server and ChromeDriver.

4. run npm install -g webdriver-manager

5. run updated webdriver-manager

This will start the server. You will see a lot of output logs, starting with INFO. The last line will be 'Info - Started org.openqa.jetty.jetty.Server'.

5. run start webdriver-manager

Leave the server running while you conduct your test sessions. In your config file, set seleniumAddress to the address of the running server. This defaults to http://localhost:4444/wd/hub.

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

Build and Test

Checkout github : https://github.com/shahing/Protractor-Web-Automation

Upvotes: 0

Pankaj Dubey
Pankaj Dubey

Reputation: 371

Here we have a complete beginners tutorial: Protractor for beginners video

Upvotes: 0

andriyze
andriyze

Reputation: 484

Here is example using Typescript, but if it is not your case, you can simply remove all 'tsc' stuff. Configure your package.json scripts section to look like this:

  "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"
  }

and run npm start in one terminal and npm test in another one.

Upvotes: 2

sandhya
sandhya

Reputation: 11

First you need to install the node.js from https://nodejs.org/en/download/ and then install protractor using "npm install -g protractor". This will install the protractor globally. I faced issues when I installed protractor locally. Better try to install it globally. Or you can provide all your dependencies in the package.json file like below:

{
  "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"
  }
}

There are dependencies for the reporting of the test results as well in the above package.json file. And you need to run webdriver-manager update. It is a helper tool to get an instance of a selenium server running.

You can put everything in the package.json file and run "npm install" to install all the dependencies. This will create a "node_modules" folder for you.

Now create a configuration file ex: conf.js. This can be something like this.

// 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.
};

Once you are done with the setup, create a test file. The tests are written using jasmine framework which contains "describe" and "it". "describe" will hold "it " which has the tests in it. You can go through this: http://www.protractortest.org/#/

Now run the test with "protractor conf.js". This will run the tests and generate the reports as well in the TestOutput folder which we have set up in the configuration file.

Upvotes: 0

Ashish Deshmukh
Ashish Deshmukh

Reputation: 448

I'm using IntelliJ for protractor tests. Also note that for this, IntelliJ Ultimate Edition is required along with node.js and protractor installations.

You can find the details here Protractor Setup on IntelliJ

Upvotes: 0

Reactgular
Reactgular

Reputation: 54771

You can install Protractor globally via:

$ npm install -g protractor

Afterwards it should be available on the command line (Windows/Linux)

$ protractor protractor.conf.js

To install just for the current project:

$ npm install protractor --save-dev

It can be run via the node_modules like this (Windows/Linux):

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

You can add it to your package.json for easier running:

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

Then later:

$ npm test

Upvotes: 14

Jagannath
Jagannath

Reputation: 78

Yes, it is possible to run protractor by using the following command :

npm install protractor

and you can then access it by :

./node_modules/.bin/protractor conf.js

For more info visit : Protractor - end-to-end testing for AngularJS . This a very good place to start.

Upvotes: 0

Ynot
Ynot

Reputation: 561

I think the best way to run protractor is by installing it local to your project and then run it with npm scripts.

Backing up one step, npm itself uses a hierarchy based on the file system to find executable modules. If you type npm bin npm will tell you the first place it is going to look to find something executable (e.g. [project]/node_modules/.bin). If you include protractor in your package.json, when you do an npm install, protractor will add a symlink in your .bin directory for both protractor and webdriver-manager.

There are a number of ways you can use this information to execute protractor. The ~~correct~~ best way I think though is to use npm scripts. When you use npm scripts npm will automatically load protractor from the local .bin directory.

Here's an example

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

So now you can run your selenium server with npm run selenium then run your protractor tests with npm run test-e2e.

This is also cross platform so if you are using mac or Windows you'll be covered either way.

NOTE: You can do stuff in these scripts that are not cross platform (npm docs) so if being cross platform is important to you and you want to do anything fancy I would recommend using shelljs.

P.P.S. Didn't want to clutter the point above but npm also has pre and post hooks so you can update and run selenium with one command.

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

Theoretically Windows should support && so you could also do this but ymmv...

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

Upvotes: 1

nir
nir

Reputation: 3140

You should use npm-run-all (or concurrently, parallelshell), because it has more control over starting and killing commands.

Once npm-run-once, protractor, http-server installed locally, you can modify package.json like that:

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 = Run commands in parallel.

-r = Kill all commands when one of them finishes with zero.

Running npm run test1 will start Selenium driver, start http server (to serve you files) and run protractor tests. Once all tests are finished, it will close the http server and the selenium driver.

Upvotes: 10

Madhu S N
Madhu S N

Reputation: 41

If you have any suite in config.js try this

suites: {

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

},

The tests can be executed from command line as below:

<path>protractor config.js --suite TESTCASES_RELATED_TO_SUITE1

This will only execute one test suite.

Upvotes: 0

tennisgent
tennisgent

Reputation: 14201

These are the getting started docs:

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

You need to have node.js installed on your machine, as well as the npm node package. Once you have those two things installed you can follow the rest of the directions in the docs above.

It should only take about 5-10 mins of installation before you have Protractor up and running. Let me know if you're still stuck.

Upvotes: 10

Brian F
Brian F

Reputation: 1660

You need to run it through node.

So from the base of your project;

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

Upvotes: 15

Andres D
Andres D

Reputation: 8900

I have a code generator that creates an empty protractor project. The instructions should be easy to follow:

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

Upvotes: 1

Related Questions