简体   繁体   中英

Selenium: How to use stored value in a javascript comparison

I've searched around for the answer to this and found lots of much more complicated questions, but none that gave me insight enough to figure this one out.
What I'm doing:
1- open a page with a number that will probably be large
2- get the X Path to where that number is and store it to a variable
3- do a javascript to compare the above stored variable to see if it is bigger than 10, if so, set a new variable to true; else false (because that is the default value)
4- verify the variable in #3 is true

Sounds simple enough, no?

Where it goes wrong:
At step 3, comparing the variable from step #2 to 10 isn't allowed, at least not the way I'm writing it.

Why?

Details:

<tr>
   <td>open</td>
   <td>http://www.google.com/search?hl=en&q=selenium+verifyEval</td>
   <td></td>
</tr>
<tr>
   <td>store</td>
   <td>/html/body/div[5]/div/p/b[3]</td>
   <td>resultCount</td>
</tr>
<tr>
   <td>storeEval</td>
   <td>var isMoreThan10 = new Boolean(); isMoreThan10 = (resultCount &gt; 10);</td>
   <td>isMoreThan10</td>
</tr>
<tr>
   <td>verifyExpression</td>
   <td>${isMoreThan10}</td>
   <td>true</td>
</tr>

I just thought of one possible workaround: Expand the javascript code to get the value there & assign it to a variable there so I'll be more likely to be able to use that variable in the javascript. Not sure exactly how that would be done- anyone wanna help with that?

But surely there is be a better way, isn't there? I must be able to assign a value to a variable in Selenium, then in the next line use that variable in a javascript, right?

Found the solution. Not only was I doing the storeEval wrong with the way I was setting isMoreThan10; I was setting resultCount wrong with the wrong store call (instead of storeText) and calling resultCount wrong in the storeEval.

Here is the correct way to do it:

<tr>
<td>open</td>
<td>http://www.google.com/search?hl=en&amp;q=selenium+verifyEval</td>
<td></td>
</tr>
<tr>
<td>storeText</td>
<td>//p[@id='resultStats']/b[3]</td>
<td>resultCount</td>
</tr>
<tr>
<td>storeEval</td>
<td>(storedVars['resultCount'] &gt; 10) ? true : false</td>
<td>isMoreThan10</td>
</tr>
<tr>
<td>verifyExpression</td>
<td>${isMoreThan10}</td>
<td>true</td>
</tr>

Thanks to AutomatedTester for suggesting a ternary assignment in storeEval

This is a simple thing to solve

<tr>
   <td>storeEval</td>
   <td>var isMoreThan10 = new Boolean(); isMoreThan10 = (resultCount &gt; 10);isMoreThan10 ;</td>
   <td>isMoreThan10</td>
</tr>

It requires that you have the result that you want at the end. If you were to use a ternary it would be better since its not storing the result in a variable in the javascript you are passing through.

eg

  <tr>
       <td>storeEval</td>
       <td>(resultCount > 10) ? true : false</td>
       <td>isMoreThan10</td>
    </tr>

This type of thing is really using TestPlan (which can run with both Selenium and HTMLUnit as a backend)

GotoURL http://www.google.com/search?hl=en&q=selenium+verifyEval
set %Count% (response //html/body/div[5]/div/p/b[3])
Notice Got Value %Count%
Checkpoint numComp %Count% > 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM