4

I have two input text boxes and one label on my page. I want to get the numbers from all three elements, add the two text box numbers together, then check if the sum equals the label. How would I go about this?

This is what I have so far, but it's not been working for me. It seems that getAttribute returns a value with apostrophes around the number. If I try to use "parseInt" it returns "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);
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Kaizer5777
  • 55
  • 5

1 Answers1

6

You can actually solve it with 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]));
});

Or, you can also use q promise library and it's spread() syntactic sugar:

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

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

And, you can also replace protractor.promise with q, see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195