0

I need to store two text value and use them as numbers for subtraction:

<a href="..."><span id="user-account-balance">593 455,07</span> $</a>

<a href="..."><span id="user-account-balance-points">12454</span> P</a>

I need to subtract both values, but it doesn't work for me:

<tr>
    <td>storeText</td>
    <td>//a/span[@id='user-account-balance']</td>
    <td>a</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>storedVars['a'].match(/^\d+/);</td>
    <td>one</td>
</tr>
<tr>
    <td>storeText</td>
    <td>//span[@id='user-account-balance-points']</td>
    <td>c</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>storedVars['c'].match(/^\d+/);</td>
    <td>two</td>
</tr>
<tr>
    <td>store</td>
    <td>javascript{storedVars['one']+storedVars['two']}</td>
    <td>r</td>
</tr>
<tr>
    <td>echo</td>
    <td>${r}</td>
    <td></td>
</tr>

The result is [info] echo: 59312454. So there are two problems, the first number is cut out after the space and it doesn't even subtract anyway

2 Answers 2

0

So first, your regex

/^\d+/

will only capture the uninterrupted sequence of numbers at the very beginning of the string. You will need to modify the first .match() regex to handle textual numbers with spaces as thousand separators and commas as decimal separators. Documentation on match says that a //g regex will return an array of strings, so you could probably do something like

storedVars['a'].match(/\d+(,\d\d)?/g).join('').replace(',', '.');

to capture the currency string and store it in a string-to-number convertible manner. Then your last JavaScript expression would be something like

storeEval
parseFloat(storedVars['one']) + parseInt(storedVars['two'])
0

This worked for me when total was stored as $xxx,xxx.xx:

<tr>
    <td>storeText</td>
    <td>xpath=.//*[@id='table-header']/tbody/tr[1]/td[5]/b</td>
    <td>Total1</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>storedVars['Total'].replace(',', '').replace('$', '');</td>
    <td>Amount1</td>
</tr>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.