Mallesh JM
Mallesh JM

Reputation: 23

running protractor test using npm

I am new to protractor, node and all. I have learned to create scripts with protractor. But now I need to run this test scripts along the build.

I have seen people can run it with npm test/ npm test e2e. How can I achieve this.

My project structure,

webapp/test/e2e

In my project they are using webpack and karma.

Sorry, I am really noob at this and don't know how to set it up.

We also need to configure it in Jenkins, so it runs and generates a flag for success or failure.

Any suggestions to set it up would really really be helpful. Please be as elaborate as possible as it is hard for me to understand things as of now.

Thank you so much!

Upvotes: 0

Views: 2906

Answers (1)

sonhu
sonhu

Reputation: 961

If the package.json file is in the root folder of the project you can add the code below to it and run it using npm test and npm run test:e2e. This assumes you have Protractor and Karma installed locally in your project which would be a best practice (if it is not in the package.json as a dependency it probably is not).

"scripts": {
    "postinstall": "node_modules/protractor/bin/webdriver-manager update",
    "test": "node_modules/karma/bin/karma start webapp/test/unit/karma.conf.js"
    "test:e2e": "node_modules/protractor/bin/protractor webapp/test/e2e/conf.js"

}
  • postinstall - This will run webdriver-manager update locally automatically after running npm install, you could run into issues with missing drivers when using a CI Server like Jenkins that is continuously checking our your repository (it also saves you a CI step)

  • test - This is equivalent to karma start karma.conf.js

  • test:e2e - This is equivalent to protractor conf.js it is just running it locally. To run these tests type npm run test:e2e from the directory with your package.json

It is recommended to not combine Protractor and Karma together (although possible) Karma should be used for Unit and Integration tests and Protractor should be used for e2e tests.

As an extra tip it is possible to pass protractor or karma command line arguments through the npm scripts. Simply append it the way you normally would (e.g.
"webapp/test/e2e/conf.js --baseUrl=https://yourbaseurl.com/")

Your question regarding integrating NPM into Jenkins has already been answered fairly well here how to run npm/grunt command from jenkins but be aware running your tests entirely in Jenkins will cause it to run headlessly which when using Windows will cause it to run in Session 0 where the screen resolution will be smaller which causes some tests to fail, opening the selenium server beforehand in a Terminal/Powershell window will cause Jenkins to route your tests through that so it will visually display on your computer.

Upvotes: 1

Related Questions