CodeWithMMAK

How to Install Protractor: A Step-by-Step Guide for Beginners

Learn how to install Protractor globally or locally for your Angular and AngularJS end-to-end testing projects. Includes setup tips for VS Code.

CodeWithMMAK
November 23, 2018
7 min

Introduction

🎯 Quick Answer

To install Protractor, open your terminal and run npm install -g protractor for a global installation, or npm install protractor --save-dev to install it locally within your project. After installation, verify it by running protractor --version. Remember that Protractor also requires WebDriver Manager to manage browser drivers, which can be updated using webdriver-manager update.

Protractor is an end-to-end test framework for Angular and AngularJS applications. It runs tests against your application in a real browser, interacting with it as a user would. It is built on top of WebDriverJS and provides specialized locators for Angular elements.

📖 Key Definitions

Protractor

An end-to-end (E2E) testing framework specifically designed for Angular applications, though it can also test non-Angular sites.

Global Installation (-g)

Installing a package in a system-wide directory, making the command available in any terminal window.

Local Installation

Installing a package within a specific project's node_modules folder, ensuring version consistency for that project.

WebDriverJS

The JavaScript implementation of the Selenium WebDriver API, which Protractor uses to control browsers.

Why Use Protractor?

  • Angular Support: Built-in support for Angular-specific locators like by.model, by.binding, and by.repeater.
  • Automatic Waiting: Automatically waits for Angular to finish tasks before proceeding to the next test step, reducing "flaky" tests.
  • Real Browser Testing: Runs tests in real browsers like Chrome, Firefox, and Safari.
  • Jasmine Integration: Comes bundled with the Jasmine test framework by default.

🚀 Step-by-Step Implementation

1

Open Visual Studio Code

Launch VS Code and open the folder where you want to create your testing project.

2

Open the Integrated Terminal

Go to the Terminal menu and select New Terminal (or use the shortcut Ctrl + ).

3

Create a Project Directory

If you haven't already, create a new folder for your tests:

Code Snippet
mkdir my-protractor-tests
cd my-protractor-tests
4

Initialize npm

Create a package.json file to manage your dependencies:

Code Snippet
npm init -y
5

Install Protractor

For a local project installation (recommended), run:

Code Snippet
npm install protractor --save-dev

Note: Use -g instead of --save-dev if you specifically need a global installation.

6

Update WebDriver Manager

Protractor uses webdriver-manager to download browser drivers. Run the following to get the latest versions:

Code Snippet
npx webdriver-manager update

Common Errors & Best Practices

⚠️ Common Errors & Pitfalls

  • 'protractor' is not recognized

    This occurs if you installed it locally but are trying to run it without npx. Use npx protractor or add it to your package.json scripts.

  • Version Mismatch

    If your Chrome browser is newer than your ChromeDriver, tests will fail. Always run webdriver-manager update regularly.

  • Java Not Found

    WebDriver Manager requires Java to run the Selenium Server. Ensure you have the Java Development Kit (JDK) installed and configured in your PATH.

Best Practices

  • Always prefer local installation over global. This prevents "it works on my machine" issues when sharing code with teammates or CI/CD pipelines.
  • Use TypeScript with Protractor for better type checking and IDE support.
  • Keep your webdriver-manager updated to match your local browser versions.
  • Ignore minor npm warnings during installation unless they explicitly stop the process.

Frequently Asked Questions

Is Protractor deprecated?

Yes, the Angular team has officially deprecated Protractor. For new projects, it is recommended to use Playwright or Cypress.

Can I test non-Angular sites?

Yes, you can set browser.ignoreSynchronization = true in your tests to disable Angular-specific waiting.

Do I need a separate Selenium server?

No, Protractor can start a "mock" Selenium server automatically if you use directConnect: true in your configuration.

Conclusion

Installing Protractor is a straightforward process that opens the door to powerful end-to-end testing for Angular applications. While the industry is shifting toward newer tools like Playwright, understanding Protractor remains valuable for maintaining existing enterprise suites. By following this guide, you've set up a solid foundation for your UI automation journey.

📝 Summary & Key Takeaways

Protractor is a specialized E2E testing framework for Angular, leveraging WebDriverJS and Jasmine. Installation is handled via npm, with a strong recommendation for local project-based setup to ensure environment consistency. The process involves initializing a project, installing the protractor package, and using webdriver-manager to synchronize browser drivers. While Protractor offers unique advantages like automatic waiting and Angular-specific locators, developers should be aware of its deprecation status and consider modern alternatives for new projects. Adhering to best practices like local installation and regular driver updates ensures a stable and reliable testing environment.

Share it with your network and help others learn too!

Follow me on social media for more developer tips, tricks, and tutorials. Let's connect and build something great together!