Protractor e2e testing for angular

Start December I did some work kickstaring our e2e test scripts for our app. Since angular js is now using protractor for e2e tests then team agreed its best to (try) use protractor.

Protractor is a node package that is a wrapper around WebDriverJS and Protractor is used for running automated tests in a browser.
WebDriverJS (and Protractor) apis are asynchronous. All functions return promises. WebDriverJS maintains a queue of pending promises, called the control flow. Protractor has adapted Jasmine so that each spec waits until the control flow is empty. Jasmine expects understand promises and add a task to the control flow to be executed after others are executed.
Protractor runs on top of Selenium-Webdriver which is a browser automation framework. You have to run your Protractor tests on top of a running selenium standalone server (installed as part of Protractor install).
(*this content from Protractor pages)

Learnings:
  • I'm overall positive on protractor (after approx 1 days effort so far) but going is slower than I'd like (see following notes for some reasons). Update: a month later and still positive, we have a dedicated resource to build out the scripts.
  • I have created a number of end to end tests including: login/logout & create flow over 4 pages
  • the PageObject is a good pattern to follow when writing tests to encapsulate pages; you want to be able to share the code, here's how:
    • I put the PageObject code in 1 file 
    • in that file I export the pages e.g. module.exports.IndexPage = IndexPage;
    • I then require that into all my spec files at the top i.e.  var pageObjects = require('./pageObjects.js');
    • within the specs, I then reference and use: var indexPage = new pageObjects.IndexPage()
  • learning curve
    • the github protractor page documentation is basic, I learned most from the protractor specs themselves and issues in github. Update: its getting better.
    • stack complexity; protractor sits on webdriver sits on selenium, its not always clear what to use e.g. to ptor or not (so far I've not used ptor and just use browser). And docs seem to use various various forms
    • This runs on top of Selenium-Webdriver which is a browser automation framework. You have to run your Protractor tests on top of a running selenium standalone server (installed as part of Protractor install).
  • I like that it follows the Jasmine syntax which we're using with Karma


Install Protractor and Selenium:
see "Setup and Config" section here note: this has now changed since I originally installed
  1. I installed protractor locally: sudo npm install -g protractor
  2. it includes a script to install Selenium which you must run as a node script i.e. node ./node_modules/protractor/bin/install_selenium_standalone
  3. then start selenium locally under /usr/local/lib: ./selenium/start
  4. then in another cli I run protractor tests: protractor <myconfig>

Set Breakpoint on a test:
 how to use the debugger to set a breakpoint:
  • add a line to your jasmine test where you wish to break: browser.debugger()
  • run protractor with debug option: protractor debug <myconfig>
  • it will break within node on command line, hit c to continue
  • it should break on the line with browser.debugger()
I find myself running in the debugger alot lately when developing scripts. You can edit the spec and then just hit run in the debugger and it will run with latest. Faster.


Running tests in the cloud with SauceLabs

  • you can run your protractor tests against saucelabs (useful to run IE tests for mac users)
  • setup a saucelabs account
  • change your protractor config file to include sauceUser (your saucelabs user id) and sauceKey (not your saucelabs password, its a long hash you can see bottom left on your saucelab account)
  • I had to comment out the seleniumAddress property (otherwise it ran selenium locally)
  • you must ensure your baseUrl in the protractor config is a globally accessible url (e.g. staging), because saucelabs won't run against localhost
  • then run protractor from command line as normal
  • it should run against saucelabs




Comments

Popular posts from this blog

deep dive into Material UI TextField built by mui

angular js protractor e2e cheatsheet

react-router v6.4+ loaders, actions, forms and more