Phonecat Tutorial App
Phonecat Tutorial App
A great way to get introduced to AngularJS is to work through this tutorial, which walks you
through the construction of an AngularJS web app. The app you will build is a catalog that
displays a list of Android devices, lets you filter the list to see only devices that interest you,
and then view details for any device.
Follow the tutorial to see how AngularJS makes browsers smarter — without the use of native
extensions or plug-ins:
The tutorial guides you through the entire process of building a simple application, including
writing and running unit and end-to-end tests. Experiments at the end of each step provide
suggestions for you to learn more about AngularJS and the application you are building.
You can go through the whole tutorial in a couple of hours or you may want to spend a
pleasant day really digging into it. If you're looking for a shorter introduction to AngularJS,
check out the Getting Started document.
Environment Setup
The rest of this page explains how you can set up your local machine for development. If you
just want to read the tutorial, you can go straight to the first step: Step 0 - Bootstrapping.
Install Git
You can download and install Git from https://git-scm.com/download. Once installed, you
should have access to the git command line tool. The main commands that you will need to
use are:
Download angular-phonecat
Clone the angular-phonecat repository located at GitHub by running the following command:
cd angular-phonecat
The tutorial instructions, from now on, assume you are running all commands from within
the angular-phonecat directory.
Install Node.js
In order to install dependencies (such as the test tools and AngularJS itself) and run the
preconfigured local web server, you will also need Node.js v6+.
You can download a Node.js installer for your operating system
from https://nodejs.org/en/download/.
Check the version of Node.js that you have installed by running the following command:
node --version
In Debian based distributions, there might be a name clash with another utility called node.
The suggested solution is to also install the nodejs-legacy apt package, which
renames node to nodejs.
If you need to run different versions of Node.js in your local environment, consider
installing Node Version Manager (nvm) or Node Version Manager (nvm) for Windows.
By installing Node.js, you also get npm, which is a command line executable for downloading
and managing Node.js packages. We use it to download the AngularJS framework as well as
development and testing tools.
Once you have Node.js installed on your machine, you can download these dependencies by
running:
npm install
Running npm install will also automatically copy the AngularJS framework and other
dependencies necessary for our app to work into the app/lib/ directory.
Note the angular-phonecat project is setup to install and run these utilities via npm scripts.
This means that you do not have to have any of these utilities installed globally on your
system to follow the tutorial. See Installing Helper Tools below for more information.
The project is preconfigured with a number of npm helper scripts to make it easy to run the
common tasks that you will need while developing:
http-server ./app
Running the Development Web Server
While AngularJS applications are purely client-side code, and it is possible to open them in a
web browser directly from the file system, it is better to serve them from an HTTP web
server. In particular, for security reasons, most modern browsers will not allow JavaScript to
make server requests if the page is loaded directly from the file system.
The angular-phonecat project is configured with a simple static web server for hosting the
application during development. Start the web server by running:
npm start
This will create a local web server that is listening to port 8000 on your local machine. You
can now browse to the application at http://localhost:8000/index.html.
To serve the web app on a different IP address or port, edit the "start" script
within package.json. You can use -a to set the address and -p to set the port. You also need to
update the baseUrl configuration property in e2e-test/protractor.conf.js.
npm test
This will start the Karma unit test runner. Karma will read the configuration
file karma.conf.js, located at the root of the project directory. This configuration file tells
Karma to:
It is good to leave this running all the time, in the background, as it will give you immediate
feedback about whether your changes pass the unit tests while you are working on the code.
You don't have to manually run this command. Our npm scripts are configured so that it will
be automatically executed as part of the command that runs the E2E tests.
Since Protractor works by interacting with a running application, we need to start our web
server:
npm start
Then, in a separate terminal/command line window, we can run the Protractor test scripts
against the application by running:
It is good to run the E2E tests whenever you make changes to the HTML views or want to
check that the application as a whole is executing correctly. It is very common to run E2E
tests before pushing a new commit of changes to a remote repository.
Each version of Protractor is compatible with specific browser versions. If you are reading
this some time in the future, it is possible that the specified Protractor version is no longer
compatible with the latest version of Chrome that you are using.
If that is the case, you can try upgrading Protractor to newer version. For instructions on how
to upgrade dependencies see Updating dependencies.
Updating dependencies
In order to avoid surprises, all dependencies listed in package.json are pinned to specific
versions (this is what the package-lock.json file is about). This ensures that the same version
of a dependency is installed every time.
Since all dependencies are acquired via npm, you can use the same tool to easily update them
as well (although you probably don't need to for the purpose of this tutorial). Simply run the
preconfigured script:
This will update all packages to the latest version that satisfy their version ranges (as specified
in package.json) and also copy the necessary files into app/lib/. For example,
if package.json contains "some-package": "1.2.x", it will be updated to the latest 1.2.x version
(e.g. 1.2.99), but not to 1.3.x (e.g. 1.3.0).
If you want to update a dependency to a version newer than what the specificed range would
permit, you can change the version range in package.json and then run npm run update-
deps as usual.
Common Issues
Protractor dependencies
Under the hood, Protractor uses the Selenium Standalone Server, which in turn requires
the Java Development Kit (JDK) to be installed on your local machine. Check this by
running java -version from the command line.
If JDK is not already installed, you can download it here.