What is the best way to have Protractor pause until a specific promise is resolved?

I am having an issue with my website where clicking a button triggers an angular service that returns some data to be displayed on the page. I need to write an angular test that clicks the button, waits for the promise to resolve, and verifies that the data is displayed. However, the test always completes before the promise is resolved. I tried using browser.sleep(), but it didn't work because the promise was never resolved. Most of the suggestions related to promises in protractor tests don't apply here since the promise is set up by the page itself, not within the test.

Below is the test code:

it('should roll a d2', function () {
    element(by.id('d2Button')).click();

    // Code to wait for promise resolution

    expect(element(by.binding('rolls.d2')).getText()).not.toEqual('0');
});

Here is the HTML snippet:

<button id="d2Button" type="button" class="btn btn-success" ng-click="rollD2()">Roll</button>
<span><b>{{rolls.d2 | number}}</b></span>

Summary of the angular code triggered by the click event:

$scope.rollD2 = function () {
    diceService.getD2Roll($scope.quantities.d2).then(function (data) {
        $scope.rolls.d2 = data.roll;
    });
};

And here is the relevant service call:

function getD2Roll(quantity) {
    var url = "Dice/D2/" + quantity;
    return getPromise(url);
}

function getPromise(url) {
    var deferred = $q.defer();
    $http.get(url).success(deferred.resolve).error(deferred.reject);
    return deferred.promise;
}

Can someone help me figure out how to make the test wait for the promise to resolve correctly?

Answer №1

The solution provided is to implement the built-in browser.waitForAngular() feature after clicking the button. I have created a generic method for all tests to utilize, which is as follows:

app.clickButtonAndWaitForResponse = function (button) {
    button.click();
    browser.waitForAngular();
};

Subsequently, any test requiring button clicks that trigger an asynchronous request can simply invoke this method and provide the button element as input.

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

The issue arises with loading data from the server due to lack of definition of Mongo

I am experiencing a console bug and need assistance. It seems that there is an issue with loading data from the server as I am getting an error stating "Mongo is not defined." C:\Users\Robert\Desktop\FINISH\node_modules\mongod ...

What is the best way to include several IDs in a discord.js verification process?

I am attempting to create a basic script that verifies if the user's ID matches the one specified in the code. However, I'm struggling to understand how to achieve this. Any assistance from you all would be greatly appreciated. if(message.autho ...

New patch request received in Google Sheets, replacing the existing post request

I am transferring 12 cell values from a Google Sheet to a MongoDB database. The purpose behind this action is to merge the 12 cells, perform certain data transformations, and display the output on a frontend interface later on. Moreover, I'm faced wit ...

Creating an array of JSON objects by fetching data from an external server using JSONP Ajax

I'm new to working with JS/JQuery and I have a code snippet that looks like this: ids = ["1", "2", "3"] var imageData = []; for (i=0; i<ids.length; i++) { $.ajax({ url: 'http://my.images.edu/' + ids[i] + '/info.json&apos ...

Checking with jQuery Validate: displaying an error message if input matches a certain value

I spent 6 hours trying to figure out a solution to my issue. Currently, I am utilizing the jQuery Validation plugin. In my form, there is a password input field. When an AJAX request detects an incorrect password, another input field is set to a value of ...

"Uncovering the problem: Challenges with AngularJS videos

Currently, I am diving into the world of AngularJS by exploring the content provided on the website . I have downloaded the videos and am diligently working through the examples on my personal computer. Everything was progressing smoothly until I encounte ...

What is the best way to utilize the $http service when a state change event occurs in AngularJS?

I am working with AngularJS and utilizing Ui-Router. In order to execute the $http service on each state change, I'm encountering an issue. When attempting to inject the $http service within the statechange event, a circular dependency error is thrown ...

Utilizing props and state within a function declaration in a class component: a guide

I am facing an issue with a class component that contains a function called printData(). I need to utilize the states and props variables of the class within this function. How can I achieve this? Here is a snippet of my code - class ReadData extends Comp ...

VueJS error: 'Property or method is not defined on the instance' message appears despite the method being defined on the instance

Is there a way in Vue.js to dynamically assign the class 'adjust' based on the value of a computed property called "isTrueSet"? I keep encountering the following error message: [Vue warn]: Property or method "itTrueSet" is not defined on the inst ...

What are the methods for choosing various boxes using the UP/DOWN/RIGHT/LEFT arrow keys?

This code snippet displays a 3x3 matrix where boxes can be hovered over and selected. The goal is to navigate around with keys and select boxes using ENTER. Can anyone provide guidance on how to achieve this functionality? <link rel="stylesheet" href ...

The significance of Buffer in node.js

I see the importance of using Buffer, but I'm a bit unclear on how to properly use buffer syntax. In the example provided below, const parsedBody = Buffer.concat(body).toString(); const message = parsedBody.split('=')[1]; ...

Using jQuery to update a variable value based on user selection from a dropdown menu populated with database values

I am currently stuck on a project where I need to dynamically change the content of a DIV based on the selection made in a dropdown using jQuery. For example, here is what I want to achieve: <select name=""> <option value="10">10 clicks</o ...

What is the best way to trigger a JavaScript function in an iframe from a child window within the main window?

In my main file, index.php, there is an iframe named main: <iframe src="main.php" id="main_frame" name="main_frame"> </iframe> The main.php file contains two iframes: middle_frame and top_frame. <iframe src="middle.php" id="middle_frame" ...

Calculate the total rows within a specified date range

Within my database, there is a column named IsStaff, which returns a value as a bit. This signifies whether a staff member in a company has an illness (1) or not (0). How can I construct an SQL query to count the occurrences of both 1's and 0's w ...

unable to retrieve element values using class names

I have created multiple h1 elements with the same class names listed below. <h1 class="h1">One</h1> <h1 class="h1">Two</h1> <h1 class="h1">Three</h1> <h1 class="h1">Four</h1> I have also added a button that ...

Express JS backend causing CSS file to fail to load in React project

After doing some research on Stack Overflow, I came across a few solutions but none of them seemed to work for my specific situation. I am currently attempting to serve my React website from an Express backend server. Here is the layout of my folders: cli ...

Leverage the Express JS .all() function to identify the specific HTTP method that was utilized

My next task involves creating an endpoint at /api that will blindly proxy requests and responses to a legacy RESTful API system built in Ruby and hosted on a different domain. This is just a temporary step to transition smoothly, so it needs to work seam ...

Having trouble obtaining the serialized Array from a Kendo UI Form

I am working on a basic form that consists of one input field and a button. Whenever the button is clicked, I attempt to retrieve the form data using the following code: var fData = $("#test").serializeArray(); Unfortunately, I am facing an issue where I ...

Identify changes in attributes on elements that are updated automatically

On my webpage, I want to have two select boxes that are connected so their values always match. Here's an example: Imagine a page with two selects: <select class="myselector"> <option value='1'>1 <br> < ...

When the page is refreshed, the variables in AngularJS are cleared, yet the token remains active

As a newcomer to AngularJS, I have been using JSON Web Tokens for authentication on the login page. After successfully authenticating, it redirects to the homepage. However, upon refreshing the page, all variables are cleared and nothing is displayed. The ...