Summing up various results from promises [Protractor]

On my webpage, I have set up two input text boxes and a label. My aim is to extract the numbers from these elements, sum up the numbers in the text boxes, and then compare the total with the number in the label. Does anyone know how I can achieve this?

Here's what I've tried so far, but unfortunately, it's not yielding the desired outcome. It seems that the value returned by getAttribute method is enclosed in apostrophes which causes issues when trying to convert it using parseInt, resulting in "NaN".


element(by.id('textbox1')).getAttribute('value').then(function(value){
textbox1Value = value;
});

element(by.id('textbox2')).getAttribute('value').then(function(value){
textbox2Value = value;
});

element(by.id('label1')).getText().then(function(value){
label1Value = value;
});

expect(textbox1Value + textbox2Value).toEqual(label1Value);

Answer №1

If you're faced with this problem, rest assured that there are solutions available. One option is to utilize promise.all():

var first = element(by.id('textbox1')).getAttribute('value'),
    second = element(by.id('textbox2')).getAttribute('value'),
    total = element(by.id('label1')).getText();

protractor.promise.all([first, second, total]).then(function (values) {
    expect(parseInt(values[0]) + parseInt(values[1])).toEqual(parseInt(values[2]));
});

Alternatively, you can explore using the q promise library alongside its handy spread() function:

Q.all([first, second, total]).spread(function (first, second, total) {
    first = parseInt(first);
    second = parseInt(second);
    total = parseInt(total);

    expect(first + second).toEqual(total);
});

Moreover, it's worth noting that you have the option to substitute protractor.promise with q. Check out further information on this topic here:

  • Spreading promises in Protractor

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

Error message: Unspecified service injected

I am currently working with 2 separate javascript files for my project. One is being used as a controller, while the other serves as a service. However, when I attempt to inject the service into the controller and access its function, an error message pops ...

Disabling the global ajax datatype causes issues with Rails remote links

I have successfully developed a rails application that submits a form remotely and then displays form validation errors if the validation process fails. The form itself is working perfectly. However, I've come across an issue where the way I've ...

In Javascript, assign default values to an array and update them with new values upon the click of a

My goal is to create a quiz that populates an array. Initially, the quiz is empty but I aim to assign it a default value. This serves as my question navigation: /** * * @param {int} question * @returns {QuizPart} ...

Using the default theme in Material UI5

Could someone please explain how to pass in the default theme in Material UI5? In Material UI6, I used to do it like this: const useStyles = makeStyles((theme) => ({ home: { display: "flex", paddingTop: "8rem", width: ...

Is it possible to convert a blob to an image file using the FileReader in HTML

client side code <head> <script> var reader = new FileReader(); var objVal; var image = new Image(); reader.onload = function(e) { document.getElementById('propertyImg').setAttribute('src', e.target.result); }; fun ...

Jstree's select_node function is failing to trigger

I am having an issue with the select_node.jstree function not firing properly. Below is the code snippet: $(document).ready(function () { //$("#MySplitter").splitter(); setupSplitter(); $("#divJsTreeDemo").jstree({ "themes": { "theme": "d ...

Is there a way to pass locale data using props in VueJS Router?

To access hotel data, the URL path should be localhost:8080/hotel/:id (where id is equal to json.hoteID). For example, localhost:8080/hotel/101 This path should display the specific data for that hotel. In order to achieve this, we will utilize VueJS vu ...

Reducing file size through compression (gzip) on express js version 4.4.1

My express js app is functioning as a web server, but I am having trouble with serving unzipped static content (js and css files). I have tried using compression from https://github.com/expressjs/compression, but it doesn't seem to be working for me. ...

What is the process for verifying the size of file uploads using Multer in Express?

Currently, I am utilizing Multer as the `multipart/form-data` middleware in Express. My main query revolves around how to verify the size of uploaded files, especially during the uploading process. I understand that you can define limits in the options ob ...

Passing multiple arguments to a callback function in node-js without using promises

Within my small program, I am working on unshortening a URL and then verifying if the link adheres to a specific pattern. If it meets the criteria, I aim to carry out additional processing steps. However, I find it cumbersome to pass along all 3 paramete ...

What is the best way to transfer the http server variable between different layers in node.js without requiring it in a separate file?

I've developed a nodeJS application that involves creating a server in the file server.js. The code looks like this: http.createServer(app).listen(app.get('port'), function (err) { if (err) { console.error(err); } else { ...

What steps should be taken to avoid an event from occurring when an error message is encountered?

I have a dropdown list of cars where an error message is displayed if any of them becomes inactive. When the error message is shown, clicking on the Route Car button should prevent any event from occurring, i.e., no modal popup should be displayed. How ca ...

What exactly is the significance of the </< in THREE.Camera.prototype.lookAt</<()?

After experimenting with THREE.js for a while, I came across something peculiar: When using Firefox and opening the developer console to type camera.lookAt (assuming your camera is named camera), it displays function THREE.Camera.prototype.lookAt</< ...

Code in jQuery or JavaScript to retrieve precise node information for the currently selected form field, text, or image on a webpage

Looking to retrieve specific information about the item that has been clicked on a web page using jquery. The clickable item could be a form element (like a checkbox, text box, or text area) or a section of text within a paragraph, div, list, or image... ...

Reasons Behind the News Not Being Retrieved Using [XML JS Query]

Can you help me troubleshoot my code? <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>News Site</title> <script> window.document.onload = ...

Methods for extracting test data from Excel using Selenium

Currently, I am attempting to extract test data from an excel file for use in the sendkeys method. However, upon running my code, I encountered an error at this line: FileInputStream file = new FileInputStream (new File("C:\\Documents and Setting ...

An example of using quotes within quotes is an HTML tag embedded within JavaScript code

Currently, I'm working on a JavaScript code where clicking assigns the function getImage the source of an image to be displayed later on the page. The issue I'm facing revolves around dealing with quotation marks. <img src="bill.jpg" class=" ...

Can the ES2016 Proxy be used to intercept the "typeof" operation?

Can a handler property be defined to intercept typeof proxyObject? It is not mentioned in any of the traps listed on Mozilla. ...

Selenium operating within an Alpine 3.6 container

I am attempting to use Selenium on a Alpine 3.6 container (FROM alpine:3.6). This is what I'm trying in the container shell: apk update apk add python3 pip3 install -U selenium apk add chromium apk add chromium-driver Then, I run the following Pyth ...

Accessing an object's property within a mapped array in a Next.js application is possible

Currently, I am attempting to iterate through an array of objects and extract the link property as a <li></li> element. However, I am facing an issue where nothing is being returned: import { useState, useEffect } from "react"; import ...