How can I initiate an Ajax request in a beforeAll function for Angular 2+ Protractor end-to-end tests?

I'm facing a challenge in Angular 6 e2e tests where I need to execute a POST request to my API within the beforeAll block. Despite trying various methods, I haven't been successful so far. Below is a description of my situation.

To start, create a new Angular 6 project using the CLI with ng new foobar and navigate to the project folder. Then, open the app.e2e-spec.ts file and insert the following code snippet inside the describe function:

let id: string;

beforeAll((done) => {
  fetch('https://example.org/my-api', { method: 'POST' })
    .then(r => r.json())
    .then(r => {
      id = r;
      done();
    })
    .catch(err => done(err));
});

The reason for this action is to add a test subject to the database via the API so that the returned ID can be utilized in subsequent e2e tests.

Unfortunately, an error occurs during execution:

Failed: fetch is not defined

Alternative attempts were made as well. Initially, I tried utilizing native xhr, but encountered the error:

Failed: XMLHttpRequest is not defined

Next, I experimented with import { http } from 'https'; followed by calling the http.request(...) function which resulted in failure due to VSCode signaling that

[ts] Module '"https"' has no exported member 'http'.

Lastly, wrapping the fetch within a browser.executeAsyncScript(...) call proved ineffective as I couldn't access the result of the POST request outside the browser scope for use in tests.

At this juncture, I am uncertain of the best approach. What would be a recommended method to perform an Ajax POST to the API in the beforeAll section of an Angular 6 protractor end-to-end test?

Answer №1

After some thorough debugging, I successfully resolved the issue:

beforeAll((done) => {
  const request = require('request');

  const options = {
    url: 'https://example.org/my-api',
    method: 'POST',
    headers: { 'Content-Type': 'application/json', },
    json: true,
    body: { key: 'dummy value' },
  };

  const callback = (error, response, body) => {
    if (!error && response.statusCode > 200 && response.statusCode < 300) {
      id = body;
      done();
    } else {
      done(error);
    }
  };

  request(options, callback);
});

An important point to note is that this code runs in a Node.js environment, and the correct way to access a Node.js library for making Ajax calls is by using

const request = require('request');
(or something similar with http).

The usage of done(...) calls (and passing it as an argument in your lambda function) ensures that Jasmine and Protractor wait for the callback to be executed.

Initially, it appeared that this method was not effective, but upon further investigation, I discovered that the callback function was being triggered, yet my request was incorrect resulting in a 400 status code. If you encounter challenges, I recommend utilizing this VSCode e2e debugging setup to validate if everything is functioning properly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for transferring an excel file to a C# controller from Angular 4 within Visual Studio 2017

I'm working on a web application where I need to import an Excel file and send it to the server-side controller. On the server-side, I am utilizing EPPlus for this task. Can anyone provide guidance on how to accomplish this? I would greatly appreci ...

Troubleshooting: Why is my Datatables data not showing up with Angular 2/4 server side processing

Angular version 4.2.4 Angular-Datatables version 4.2.0 HTML Code Snippet <table datatable [dtOptions]="dtOptions"></table> Component Function ngOnInit() { this.dtOptions = { ajax: { url: "http://localhost:8880/nmets ...

Automatically assign the creation date and modification date to an entity in jhipster

I am currently working on automatically setting the creation date and date of the last change for an entity in JHipster, utilizing a MySQL Database. Below is my Java code snippet for the entity: @GeneratedValue(strategy = GenerationType.AUTO) @Column(nam ...

Encountering an issue with Angular where all parameters for NgZone cannot be resolved

Currently, I am in the process of learning Angular and experimenting with the Firebase Authentication services. However, every time I try to load the component that utilizes this service, I encounter an error. Error: Can't resolve all parameters for N ...

The error message "Unable to access property MyToast.java as it is not

I've been diligently following this tutorial on how to write Java code in NativeScript and use it directly in TypeScript. But, unfortunately, I encountered an error message stating: Cannot read property 'MyToast' of undefined app.component ...

What is the best way to send an array to the @input() of a separate component in Angular?

Successfully passed my array to the first @Input() and displayed its contents. Now, facing a challenge where I need to pass data from the first @Input() to the second @Input() and display it. Still navigating my way through Angular and learning the ins a ...

Timepicker component being used within an ngFor loop, with each instance having its inputs updated by the ngModel binding

Currently, I have implemented an ngb timepicker within an ngFor loop. Here is the HTML code for the timepicker: <tr class="d-flex" *ngFor="let ct of workingTimingList.controls; let i = index; [formGroup]="ct" [attr.id]="'tr'+i"> <td&g ...

Navigate between Angular components using [routerLink] in Angular router

How can I navigate from localhost:4200/home to localhost:4200/home#app=1111? I attempted the following: home.component.html <a class="app-card" [routerLink]="['/HOME']" [queryParams]="{'app':'1111'}"> However, nothin ...

In Typescript, the use of an array as a class member is not defined

I'm having trouble adding an array as a class member in my Typescript code. When I try to access it in a certain function, I keep getting an error saying it's undefined. What am I doing wrong? .html <button id="storePersonBtn"> .ts exp ...

What are the steps for integrating angular2 starter with express?

Can someone explain how to integrate the following Angular starter template into an Express framework? https://github.com/gdi2290/angular-starter I am able to start the webpack dev server without any issues, but I would like to utilize additional librari ...

The array is showing as empty with a length of 0, despite the fact that the debug console displays items

Something unusual is happening with my code. Most answers I've seen online talk about waiting for data, but that's not the issue here; the JSON source is within the HTML itself. An array of items mysteriously has a length of zero, causing the fo ...

Turn off the `noImplicitAny` TypeScript rule for a specific line of code

With "noImplicitAny": true enabled in my tsconfig.json, I encountered the TS7053: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' error within my code space ...

Compiled TypeScript files are missing require statements for imported components

Recently delved into Angular 2 and encountered an unusual issue. I kicked off using the Angular 2 Quickstart repository on GitHub and incorporated some components with templates. For example: import { Component } from '@angular/core'; import { ...

"TypeScript Static Classes: A Powerful Tool for Struct

In my TypeScript code, there is a static class with an async build method as shown below: export default class DbServiceInit { public static myProperty: string; public static build = async(): Promise<void> => { try { ...

Disabling the intellisense feature for locale suggestions in Monaco is recommended

Switch the keyboard language to a different one (in this case Japanese using alt + shift), and when typing in Monaco editor, an intellisense menu appears with options to remove and search. Monaco Editor Version: V0.33.0 https://i.stack.imgur.com/SIyeV.pn ...

Experiencing a timeout issue when attempting to retrieve the content of a toast notification

Here is the structure for a toast alert using DOM: <uib-alert id="alert-message-0" class="cs-toast-alert ng-binding ng-scope" ng-repeat="alert in headerModel.alerts()" type="success" close="" ng-style="{bottom: (((headerModel.alerts().length - $ind ...

Generics and Overloads in Typescript - Unable to locate specified Generics

My goal is to develop a utility function that enhances the fetch functionality within our application. One of the key aspects I want to incorporate is the ability to accept an optional Transform Function option for altering the data format returned. type T ...

Can you explain the step-by-step process of how an await/async program runs in TypeScript/JavaScript or Python?

As a C++ developer specializing in multithreading, I've been diving into the intricacies of async/await. It's been a challenge for me as these concepts differ from how C++ programs are typically executed. I grasp the concept of Promise objects, ...

Creating a specialized feature for saving form data with React Admin

Within my react-admin application, I am faced with a scenario where I have a list of items accompanied by two separate buttons: "Create using email" and simply "Create". The "create" button utilizes the functionality provided by the data provider, which is ...

What is the best way to connect my Angular 2 project to the "$wakanda" service in order to access and retrieve data efficiently?

Recently, I started a new project on the wakanda.io platform using angular2 and backend technologies. After creating some database entities, I now need to retrieve data from the database on the client side. To do this, I am looking for a way to import the ...