The function for the Protractor promise is not defined

UPDATE: After some troubleshooting, I believe I have identified the issue causing it not to work. It seems that I am unable to pass arguments along when calling flow.execute(getSpendermeldung). Does anyone have a better solution than wrapping the ApiCall in another function and using "global" variables? My intention is to have this function in a separate file and use require when necessary.

Original Message: In my Protractor tests, I am trying to retrieve test data from a database. I achieved this using the nodejs http module, which has been successful so far. However, while testing with this data, I encountered synchronization issues. I managed to circumvent them with Jasmine's callback solution. Nevertheless, I find this approach too rigid and would prefer to utilize promises and the control flow. Unfortunately, whenever I run my code, I encounter an Error: fn is not a function buried deep within webdriver/lib/promise.js

Below is the function in question:

function getSpendermeldung(kennnummer) {
var http = require('http');

var defer = protractor.promise.defer();

var options = {
    host: 'localhost',
    path: '/blabla/' + kennnummer
};

var callback = function (response) {
    var body = '';
    response.on('data',
        function (chunk) {
            body += chunk;
        });
    response.on('end',
        function () {
            spendermeldung = JSON.parse(body);
            defer.fulfill('json parsed');
        });
};

http.get(options, callback).end();

return defer.promise;
}

I invoke it like this:

 var flow = browser.controlFlow();
 flow.execute(getSpendermeldung('D0000001'));

If anyone has any insights on what might be missing or improving, please let me know. Thank you.

PS: I would like to provide the stacktrace, but I am having trouble formatting it correctly.

Answer №1

Instead of passing the function as an argument, you are directly calling it - getSpendermeldung. It is possible to achieve this without relying on global variables and by using a require from another file.

For more information on the arguments needed for flow.execute, please visit here

var apiCall = require('the-file-that-containsthe-function')
var flow = browser.controlFlow();
flow.execute(function(){return getSpendermeldung('D0000001')});

Answer №2

Make sure to return the deferred object defer and avoid returning defer.promise.

Additionally, it seems you are fulfilling the promise before completing the request. It may be better to adjust your code like so:

http.get(options, callback).end(function fulfillPromise() {
   defer.fulfill('json parsed');
});

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

Step-by-step guide on using Selenium and Java to retrieve and store comments

I am working on a Java application that utilizes Selenium Web Driver to extract data from Google Play Store apps. I currently have 30 links to different applications, and I am facing an issue with gathering ALL comments from each app. For instance, when ...

Challenges with the efficiency of the Material UI Datagrid

I am currently using MUI Datagrid to display my records, but I am experiencing delays with my modal and drawer components. Even after attempting to optimize with useMemo for my columns, I have not been able to achieve satisfactory performance. https://i.st ...

insert a gap between two elements in the identical line

Is there a way to create spacing between two text fields in the same row? I have tried using margins, paddings, and display flex in the css file but haven't been successful. import "./styles.css"; import TextField from "@material-ui/cor ...

Can someone please explain the distinction between $http.get() and axios.get() when used in vue.js?

I'm feeling a little bit puzzled trying to differentiate between $http.get() and axios.get(). I've searched through various sources but haven't found any answers that fully satisfy me. Can someone please offer some assistance? ...

I need assistance with a feature on my website where a new form is added every time the invite button is clicked, and the form is deleted when the delete button is

Invite.js This invite component includes an invite button outside the form and a delete button inside the form. The goal is to delete the form when the delete button is clicked. I have utilized useState and sourced this form from material-ui. Can anyone ...

What is the best way to assign a unique name to a dynamically generated text box using jQuery

In an effort to name a dynamically created textbox based on a specific event, I attempted the following code. The function GenerateTextBox was intended to assign a name and value of "" to the textbox upon generation, however, the name did not get assigned ...

There are no attributes currently present in the Document Object Model for the Tooltip

Can anyone assist me with an issue I'm having regarding tooltips? Some tooltips are missing certain attributes like title, and I need help identifying the tooltip shown in the image below. Just to note, there is no attribute related to the tooltip pr ...

How can we format a number to match the Brazilian number system while still maintaining its ability to be used in calculations?

Is there a known method to convert the number 123,123,214.93 into Brazilian currency format (123.123.214,93) for display purposes only? I attempted to achieve this conversion using a JavaScript function where I added periods after every 3 numbers and repl ...

Having trouble including a YouTube iframe code within the document ready function

I am having trouble getting the youtube iframe API code to work properly within my $(document).ready() function. When I try to add the code inside the function, the player does not load. However, when I move the code outside of the document.ready, the play ...

Discover the art of concurrently listening to both an event and a timer in JavaScript

Upon loading the page, a timer with an unpredictable duration initiates. I aim to activate certain actions when the countdown ends and the user presses a button. Note: The action will only commence if both conditions are met simultaneously. Note 2: To cl ...

the angular-ui-tinymce directive allows for seamless image uploads using a file browser

Utilizing jQuery, we have the ability to incorporate local images into the tinymce editor similar to what is demonstrated in this jsfiddle, by following the guidelines provided in the documentation. The Angular module for tinymce can be found at angular-u ...

Preventing form submission with JavaScript by implementing multiple validation checks

Preventing a form from submitting when validation returns false is possible. Here's an example: <form name="registration" action="registration.php" onsubmit="return validate()"> <!-- some inputs like: --> <input type="text" id="us ...

Differences Between Data Captured from Form Submissions and Data Obtained Through Ajax

When attempting to incorporate reCAPTCHA into my MVC site, I encountered an issue where it would not validate unless submitted from a form. Here is the current implementation: @using(Html.BeginForm("VerifyCaptcha", "Signup") ) { @ReCaptch ...

"Ways to retrieve an array of dates within a specified range of date and time

I am working with date fields in my project { tripScheduleStartDate: '2018-12-05T18:30:00.000Z', tripScheduleEndDate: '2018-12-07T18:30:00.000Z', } Is there a way to generate a datetime array from the start date to the end date, lik ...

Angular is unable to eliminate the focus outline from a custom checkbox created with Boostrap

Have you noticed that Angular doesn't blur out the clicked element for some strange reason? Well, I came up with a little 'fix' for it: *:focus { outline: none !important; } It's not a perfect solution because ideally every element sh ...

A comprehensive guide on displaying data in Angular using an API

I have encountered an issue while trying to display data from an API in the 'home.component.html'. Although my 'home.component.ts' successfully fetches the data from the service, I'm facing difficulty rendering it in 'home.com ...

Insert a record at the start of a data grid - JavaScript - jQuery

What is the most effective way to use jQuery for adding a new row at the top of a table? Here is an example table structure: <table id="mytable" cellpadding="0" cellspacing="0"> <tr> <td>col1</td> ...

Jest may come across test suites, but it discreetly disregards the individual tests

Having encountered an issue with Jest testing in a Nuxt/Vue v2 project, I found that after making some changes, the tests were no longer running. The unit tests were either passing or failing initially, but suddenly stopped running altogether. ----------|- ...

Determine the availability of a distant website through AJAX requests

My website runs PHP's cURL to check the online status of 5 different URLs, however, this process tends to slow down the page load time significantly, especially if one of the sites being checked is not working. Someone suggested using jQuery's a ...

Having issues with Vue.js when using Vue-strap Radio Buttons

While developing my web application with vue.js, I encountered an issue with radio buttons when I switched to using bootstrap style. I understand that I need to use vue-strap for proper data binding with bootstrap styled radio buttons in vue.js, but I am s ...