I'm writing e2e tests using protractor and I'd like to check if some values on the page are correctly calculated. Something very simple like this:
var var1 = element(by.binding('var1'));
var var2 = element(by.binding('var2'));
var var3 = element(by.binding('var3'));
expect(parseFloat(var1.getText())).toEqual(parseFloat(var2.getText()) +
parseFloat(var2.getText()));
However, because var1, var2 and var3 are all promises, this does not work.
Is there a way of converting the promise object to strings so that I can make the above work?
Since promises are asynchronous, you can't convert them to a float with a synchronous function. So, you'll need to resolve them. You could do this like:
element(by.binding('var1').getText().then(function(text1) {
element(by.binding('var2').getText().then(function (text2) {
element(by.binding('var3').getText().then(function (text3) {
expect(parseFloat(text1)).toEqual(parseFloat(text2) + parseFloat(text3));
});
});
});
I write my tests with the following pattern:
var data = {};
element(by.binding('var1')).getText().then(function(value) { data.var1text = value; });
element(by.binding('var2')).getText().then(function(value) { data.var2text = value; });
element(by.binding('var3')).getText().then(function(value) { data.var3text = value; });
expect(parseFloat(data.var1text)).toEqual(parseFloat(data.var2text) + parseFloat(data.var3text));
The pattern is rather short and clear, and all the values from the promises are available for further analysis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With