简体   繁体   中英

How do I write Selenium IDE code to properly use Regular Expressions?

I am trying to use regular expressions to test for a proper answer from a prompt. When I test the response using the regular expression below, I get a bad "false' condition that I really expect to be "true". For example when I run the code and answer the question with the number 3, Selenium tells me that the script is false not true. If I change the regular expression to a greater than or equal to type expression like this * javascript{storedVars['userAnswer'] <=11;} * Selenium says the script is true as one would expect.

ADDITIONAL INFO: I first suspected a data type issue (ie, string vs. number). So I wrote code to ensure the variable that I was comparing to the regular expression was a Number. That did not help either. Here is the code. What am I missing???

<tr>
    <td>showPrompt<\td>
    <td>Pick a number between 1-11 only!!!&nbsp;&nbsp;&nbsp;<br /> *1<\td>
    <td>userAnswer<\td>
<\tr>
<tr>
    <td>storeEval<\td>
    <td>javascript{storedVars['userAnswer'] == ('^[1-9][0-1]?$);}\td>
    <td>results<\td>
<\tr>
<tr>
    <td>echo<\td>
    <td>The results is = ${results}.<\td>
    <td><\td>
<\tr>

Here is another approach I took to understand the same problem. I set a variable to the number 5 as a numeric. Then I process 5 comparisons that I expect all to return "true". But, only the first two tests return true and the last three return false. Here they are. What am I missing or not understanding?

<tr>
    <td>storeEval<\td>
    <td>javascript{new Number(5);}<\td>
    <td>nbrAnswer<\td>
<\tr>
<tr>
   <td>echo<\td>
   <td>The nbrAnswer value is = ${nbrAnswer}.<\td>
   <td><\td>
<\tr>
<tr>
   <td>storeEval<\td>
   <td>javascript{storedVars['nbrAnswer'] == 5;}<\td>
   <td>results<\td>
<\tr>
<tr>
   <td>storeEval<\td>
   <td>javascript{storedVars['nbrAnswer'] == [5];}<\td>
   <td>results<\td>
<\tr>
<tr>
   <td>storeEval<\td>
   <td>javascript{storedVars['nbrAnswer'] == [1-9];}<\td>
   <td>results<\td>
<\tr>
<tr>
   <td>storeEval<\td>
   <td>javascript{storedVars['nbrAnswer'] == ('^[1-9]');}<\td>
   <td>results<\td>
<\tr>
<tr>
   <td>storeEval<\td>
   <td>javascript{storedVars['nbrAnswer'] == ('^[1-9][1-2]?$');}<\td>
   <td>results<\td>
<\tr>

Hello Selenium Developers

I found the answers to my own problem. First, I had to upgrade from Selenium 2.9.0 to Selenium 2.9.1. That gave me access to the JavaScript function "test". Then I could code the following that works.

  1. Prompt the user for a number from 1-11.
  2. Place the prompt variable into a number object. (Not sure if this is necessary?)
  3. Define the regular expression into a variable.
  4. Lastly, use the JavaScript "test" function to produce a Boolean response placed in the "results" variable.

Here is the code.

<tr>
    <td>showPrompt</td>
    <td>Please select a number between 1-11 only!!!</td>
    <td>userAnswer</td>
<\tr>
<tr>
    <td>storeEval</td>
    <td>new Number(storedVars['userAnswer'])<\td>
    <td>nbrAnswer</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>javascript{var regExpTester = /^[1-9][0-1]?$/; regExpTester.test(storedVars.nbrAnswer);}</td>
    <td>results</td>
<\tr>

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