Ask a Question

JScript - Addition behavior in scripted tests

SOLVED
dnb
New Contributor

JScript - Addition behavior in scripted tests

Hello,

 

I have a rookie question about behavior of addition operation.

 

I would like to take a value A (example: 100) from a text field and then add value B (example: 10) to value A and write sum of these into this text field.

    However, when i execute the code below i receive this (based on the values above): 10010 instead of 110.

 

var x = obj.wText;

  obj.SetText(x + 10)

 

  // i used aqConvert.StrToInt(x); but this didn't work

 

When i perform the subtraction operation it writes the correct value (90).

 

How can i resolve this probably easy problem?

3 REPLIES 3
tristaanogre
Esteemed Contributor

Not sure how you're using the convert... but you need to do it like this:

var x = obj.wText;
  obj.SetText(aqConvert.StrToInt(x) + 10)

This will convert the string to an integer value before doing the addition.

 

OR

 

var x = aqConvert.StrToInt(obj.wText);
  obj.SetText(x + 10)

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
karkadil
Valued Contributor

It happens because the 1st argument of the expression is string (the value taken from wText property), that's why JScript implicitly converts your 2nd argument (10) to string and then performs string concatenation.

In addition to Robert's solution, here is another one based on JScript only:

 

 

var x = obj.wText;
  obj.SetText(Number(x) + 10)

 

Thank you both for the help - it worked 🙂

cancel
Showing results for 
Search instead for 
Did you mean: