4 Protractors

Do I Need to Use Protractor?

Do I Need to Use Protractor?
A protractor is an official library to use for writing End-to-End (e2e) test suites with an Angular app. It is nothing but a wrapper over the Selenium WebDriverJS APIs.

If you have been using Angular CLI, you might know that by default, it comes shipped with two frameworks for testing. They are:
1.      unit tests using Jasmine and Karma
2.      end-to-end tests using Protractor

The apparent difference between the two is that the former is used to test the logic of the components and services, while the latter is used to ensure that the high-level functionality of the application works as expected.

Protractor configuration file is - protractor.conf.js and it look like this.
//Protractor configuration file
const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
  capabilities: {
    'browserName': 'chrome'
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  onPrepare() {
      project: 'e2e/tsconfig.e2e.json'
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));

What Is Test Function?
After installing everything as per your project requirements, CREATE your project.
The following Steps –
·             ng new YourTestProject
·             ng install
·             ng serve/ng test

Note – If you are going to development then type “ng server” command and if you want to test your project, you should type “ng test” command.  After type “ng test” command and press enter. It’s taking some time to installing everything in your project for a test.

Test functions–
1.         describe – Test suit (just a function)
2.         it  - The spec or test
3.         expect -  Expected outcome.

Triple Rule of Testing –
1.         Arrange - Create and Initialize the Components
2.         Act - Invoke the Methods/Functions of Components
3.         Assert - Assert the expected outcome/behavior

Best Practices - The quick list of best practices.
1.         Use beforeEach() to Initialize the context for your tests.
2.         Make sure the string descriptions you put in describe () and it () make sense as output
3.         Use after () and afterEach () to clean-up your tests if there is any state that may bleed over.
4.         If any one test is over 15 lines of code, you may need to refactor the test

Example -
import { TestBedasync } from '@angular/core/testing';
import { AppComponent } from './app.component';

//describe – Test suit (just a function)
describe('AppComponent', () => {
  beforeEach(async(() => {
      declarations: [AppComponent]

  //it - The spec or test
  it('should have hello property'function() {
  const fixture = TestBed.createComponent(AppComponent);
  const app = fixture.debugElement.componentInstance;

  //expect – this is expected outcome.
   expect(app.hello).toBe('Hello, Anil!');


Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Do I Need to Use Protractor? Do I Need to Use Protractor? Reviewed by Anil Singh on 10:18 PM Rating: (5)
www.code-sample.com/. Powered by Blogger.