ArticleZip > How To Use Protractor With Angular 2

How To Use Protractor With Angular 2

Protractor is an essential tool for testing Angular 2 applications, ensuring that your code works smoothly and your users have a great experience. In this guide, we'll walk you through how to use Protractor with Angular 2, step by step.

Before diving into testing your Angular 2 app with Protractor, make sure you have Node.js installed on your system. Protractor is an end-to-end testing framework for Angular and AngularJS applications, built on top of WebDriverJS. It provides a high-level API to interact with your application using real browsers.

The first step is to install Protractor globally on your system using npm. Open your terminal and run the following command:

npm install -g protractor

This command will install Protractor globally on your machine, making it accessible from anywhere in your system.

Next, you need to update WebDriver Manager, which is a helper tool provided by Protractor to manage the Selenium Server. Run the following command in your terminal:

webdriver-manager update

This command will download the necessary binaries and tools required to run the Selenium Server for your tests.

Once you have Protractor and WebDriver Manager set up, you can create your Protractor configuration file. In the root directory of your Angular 2 project, create a file named `protractor.conf.js` and paste the following configuration:

Javascript

exports.config = {
  framework: 'jasmine',
  specs: [
    './e2e/**/*.e2e-spec.ts'
  ],
  capabilities: {
    browserName: 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200',
  SELENIUM_PROMISE_MANAGER: false
};

This configuration tells Protractor to use the Jasmine testing framework, where to find your spec files, which browser to use, and the base URL of your Angular 2 application.

With your configuration set up, you can now write your Protractor tests using TypeScript. Create a new file in your project's `e2e` directory, for example, `app.e2e-spec.ts`, and write your test spec:

Javascript

import { browser, element, by } from 'protractor';

describe('My App', () => {
  it('should display welcome message', () => {
    browser.get('/');
    expect(element(by.css('h1')).getText()).toEqual('Welcome to My App!');
  });
});

In this test spec, we are telling Protractor to navigate to the root URL of our application and then check if a specific element (in this case, an `h1` tag) contains the expected text.

To run your Protractor tests, start the Selenium Server by running the following command in your terminal:

webdriver-manager start

Then, in a separate terminal window, run your Protractor tests:

protractor protractor.conf.js

Protractor will launch a Chrome browser window, navigate to your Angular 2 application, and run your tests, providing you with feedback on whether they pass or fail.

And that's it! You are now equipped to use Protractor to test your Angular 2 application effectively. Happy testing!