简体   繁体   English

如何编写Selenium IDE代码以正确使用正则表达式?

[英]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. 当我使用下面的正则表达式测试响应时,我得到了一个错误的“假”条件,我确实希望它是“ true”。例如,当我运行代码并用数字3回答问题时,Selenium告诉我如果我将正则表达式更改为大于或等于这种类型的表达式,那么它是错误的* javascript {storedVars ['userAnswer'] <= 11;} * Selenium说该脚本是真实的,就像人们期望的那样。

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. 我将变量设置为数字5。 Then I process 5 comparisons that I expect all to return "true". 然后,我处理5个比较,希望所有比较都返回“ true”。 But, only the first two tests return true and the last three return false. 但是,只有前两个测试返回true,而后三个返回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. 首先,我必须从Selenium 2.9.0升级到Selenium 2.9.1。 That gave me access to the JavaScript function "test". 这使我可以访问JavaScript函数“ test”。 Then I could code the following that works. 然后,我可以编写以下代码。

  1. Prompt the user for a number from 1-11. 提示用户输入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. 最后,使用JavaScript的“测试”功能产生一个布尔响应,并将其放在“结果”变量中。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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