Selenium 简明教程
Selenium IDE - Control Flow
Selenium IDE 命令包含可将条件语句和循环添加到测试中的功能。这有助于仅在满足特定条件时运行一些步骤,或根据特定条件多次执行相同的步骤。
Selenium IDE commands contain features with which the conditional statements and looping can be added to the tests. This helps to run some steps only if specific conditions are satisfied or to execute the same steps multiple times depending on the particular criteria.
JavaScript Expressions
借助 JavaScript 表达式,可以验证待测应用程序中的条件。execute script 和 execute async script 命令用于在我们的测试中执行 JavaScript 代码并将结果保存到变量中以供将来使用。这些变量也可用于控制流命令。此外,JavaScript 表达式可与控制流命令一起使用。
The conditions in the application under test can be verified with the help of the JavaScript expressions. The commands execute script and execute async script are used to execute a JavaScript code within our test and save the result to a variable for future use. These variables can be used in control flow commands as well. Moreover, the JavaScript expressions can be utilized with the control flow commands.
Read More : Selenium IDE - JavaScript Functions
Read More:Selenium IDE - JavaScript Functions
Basic Control Flow Commands on Conditional Branching
基本控制流命令及其配套命令如下 -
The basic control flow commands along with their companions are listed below −
if
这是条件分支上的起始命令,可以与要评估的 JavaScript 命令或在 JavaScript 命令之前创建的变量一起使用。这是 if 命令的 Target 输入字段的一部分。
This is the starting command on conditional branching and can be used along with a JavaScript command to be evaluated or with the variables created before a JavaScript command. This is a part of the Target input field of the if command.
如果该表达式返回真值,则测试将运行 if 表达式之后的步骤,直到遇到下一个控制流命令,例如 else、else if 或 end。如果表达式返回假值,则它将跳转到以下条件命令,例如 else、else if 或 end。
If the expression returns a true value, then the test will run the steps after if expression till the next control flow commands like the else, else if, or end is encountered. If the expression returns a false value, it will jump to the following conditional commands like the else, else if, or end.
else if
该命令用于 if 命令块内。类似于 if 命令,else if 命令接受要评估的 JavaScript 命令。这是 else if 命令的 Target 输入字段的一部分。
This command is used inside an if command block. Similar to an if command, the else if command accepts a JavaScript command to be evaluated. This is a part of the Target input field of the else if command.
如果该表达式返回真值,则测试将运行 if 表达式之后的步骤,直到遇到下一个控制流命令,例如 else、else if 或 end。如果表达式返回假值,则它将跳转到以下条件命令,例如 else 或 end。
If the expression returns a true value, then the test will run the steps after if expression till the next control flow commands like the else, else if, or end is encountered. If the expression returns a false value, it will jump to the following conditional commands like the else, or end.
Basic Control Flow Commands on Looping
以下列出了关于循环(允许重复一组命令/命令执行)的基本控制流命令 -
The basic control flow commands on looping (that allows to repeat a group of command/commands execution) are listed below −
times
此命令用于定义需要执行的操作/动作的迭代次数。使用此命令将迭代次数输入到目标输入字段。times 命令应以 end 命令结束。
This command is used to define the count of iterations of an action/actions that need to be performed. The count of iterations is fed to the Target input field with this command. A times command should be ended with the end command.
do
此命令用于开始一个循环。在 do 命令之后,将声明要执行的步骤。do 命令应以 repeat if 命令结束。repeat if 接受将在目标输入字段内执行的 JavaScript 表达式。do 命令后面的命令将初始执行,然后执行 repeat if 命令中的操作。
This command is used to begin a loop. After the do command, the steps to be performed are declared. A do command should be ended with the repeat if command. The repeat if accepts the JavaScript expression that will be executed within the target input field. The commands post the do command will be executed initially followed by the action in the repeat if command.
如果表达式返回 true 值,那么测试执行将移至下一个 do 命令,并重新执行模式。这会一直持续到条件不再得到满足或者出现无限循环的情况。默认尝试次数为 1000。可以通过在 repeat if 命令的 Value 字段中设置一个数字来修改此默认次数。
If the expression returns a true value, then the test execution moves to the next do command and redo the pattern. This goes on till the condition is not satisfied anymore or in an event of infinite loop. The default number of attempts is 1000. This default number can be modified, by setting a number in the Value field of the repeat if command.
while
在此命令中,要在 Target 字段中提供要执行的 JavaScript 表达式。如果表达式返回 true 值,那么命令块将一直运行到 end 命令。同样,控件会跳回到 while 命令以重复相同的模式(每次检查 JavaScript 表达式是否产生 true 或 false 值)。while 命令应以 end 命令终止。
In this command, the JavaScript expression to be executed is provided in the Target field. If the expression returns a true value, the command block runs till the end command. Again, the control jumps back to the while command to repeat the same pattern(checking each time if a JavaScript expression yields a true or false value). A while command should terminate with the end command.
forEach
此命令用于循环浏览集合并在集合内引用各个项目。在 Target 字段中,应提及指向要迭代的数组的变量名。在 Value 字段中,迭代变量名称。
This command is used to loop through a collection and refer to individual items within a collection. In the Target field, the variable name that points to the array to be iterated is to be mentioned. Within the Value field, iterator variable name.
Example 1
我们以以下页面的示例来说明,我们将在其中使用条件分支控制流命令检查最上方出现的文本 - Welcome, Login In 。
Let us take an example of the below page, where we would check the text - Welcome, Login In appearing at the top using the conditional branching control flow commands.
要遵循的步骤如下所示 -
The steps to be followed are listed below −
Step 1 − 单击浏览器上在安装后显示的 Selenium IDE 扩展。
Step 1 − Click on the Selenium IDE extension visible on the browser after its installation.
Step 2 − Selenium IDE 应该与它一起启动,应该显示一个显示该工具版本的欢迎消息。在下图中,安装的版本为 3.17.2。
Step 2 − Selenium IDE should be launched along with it, a welcome message should be displayed with the version of the tool. In the below image, the version installed is 3.17.2.
除此之外,它还为我们提供了选择我们想使用该工具做什么的选项,例如在新项目中记录新测试、打开现有项目、创建新项目和关闭 Selenium IDE。
Along with this, it gives us the option to select what we would like to do with the tool, like Record a new test in a new project, Open an existing project, Create a new project, and Close Selenium IDE.
此外,还提供了一个显示文本 - Selenium IDE project page 的链接,点击该链接将转到 Selenium 集成开发环境文档页面。
Also, a link with the text - Selenium IDE project page is provided, which on clicking would land us to the Selenium Integrated Development Environment documentation page.
Step 3 − 单击创建新项目链接,然后输入项目名称,如 Please provide a name for your new project 下的 Test12。最后,我们将点击确定按钮。
Step 3 − Click on the Create a new project link, then enter a project name, say, Test12 under the Please provide a name for your new project. Finally, we would click on the OK button.
Step 4 − 我们现在在 Selenium IDE 中准备好了工作区。
Step 4 − We would have the workspace ready in the Selenium IDE now.
Step 5 − 在命令字段中输入 open,然后在目标字段中输入 selenium ,以启动应用程序。
Step 5 − Enter open in the Command field, and selenium in the Target field, to launch the application.
Step 6 − 在命令字段中输入存储文本,xpath=// [@id="signInForm"]/h1 in the Target field and val in the Value field. Please note, xpath=// [@id="signInForm"]/h1 是文本的定位器值,val 是我们用来存储文本的变量。
Step 6 − Enter store text in the Command field, xpath=//[@id="signInForm"]/h1 in the Target field and val in the Value field. Please note, xpath=//[@id="signInForm"]/h1 is the locator value of the text and val is the variable which we would use to store the text.
Step 7 - 输入在命令字段中,且 ${val} ==="欢迎,登录" 在目标字段中。
Step 7 − Enter if in the Command field and ${val}==="Welcome, Login In" in the Target field.
Step 8 - 输入 echo 在命令字段中,且验证正确 在目标字段中。
Step 8 − Enter echo in the Command field and Correct Verification in the Target field.
Step 9 - 输入如果其他 在命令字段中,且 ${val} ==="欢迎,注册" 在目标字段中。
Step 9 − Enter else if in the Command field and ${val}==="Welcome, Register" in the Target field.
Step 10 - 输入 echo 在命令字段中,且验证不正确 在目标字段中。
Step 10 − Enter echo in the Command field and InCorrect Verification in the Target field.
Step 11 - 输入其他命令字段。
Step 11 − Enter else Command field.
Step 12 - 输入 echo 在命令字段中,且无效测试 在目标字段中。
Step 12 − Enter echo in the Command field and InValid Test in the Target field.
Step 13 - 输入结束 在命令字段中。
Step 13 − Enter end in the Command field.
Step 14 - 输入关闭 在命令字段中,以关闭浏览器窗口。
Step 14 − Enter close in the Command field to close the browser window.
Step 15 - 单击顶部运行所有测试,并等待测试执行完成。在我们的示例中,我们将看到:运行:1,失败:0,以及一根绿条表示测试在无任何失败的情况下成功运行。另外,完成成功的消息出现在日志中。
Step 15 − Click on the Run all tests from the top, and wait for the test execution to complete. In our example, we would see Runs: 1, Failures: 0, and a green bar denoting the test ran successfully without any failures. Also, the message that the completed successfully appeared under the Log.
在上面的示例中,我们已经验证文本 - Welcome, Login In ,并在控制台中打印消息 - 验证正确。此外,我们获得了顶部的绿色勾形,表示该测试通过。
In the example above, we had verified the text - Welcome, Login In and printed the message in the console - Correct Verification. Besides, we got a green tick at the top, signifying a passed test.
Example 2
让我们举一个例子,我们将在其中使用一个叫做 while 的循环控制流命令。
Let us take an example where we would use one of the looping control flow commands called while.
要遵循的步骤如下所示 -
The steps to be followed are listed below −
Step 1 - 按照示例 1 中提到的执行步骤 1 到 4。
Step 1 − Follow steps 1 to 4 as mentioned in Example 1.
Step 2 - 输入执行脚本 在命令字段中,输入 1 在目标字段中,以及 val 在值字段中。
Step 2 − Enter execute script in the Command field, return 1 in the Target field, and val in the Value field.
Step 3 - 输入 while 在命令字段中,以及 ${val} < 3 在目标字段中。
Step 3 − Enter while in the Command field, and ${val} < 3 in the Target field.
Step 4 - 输入 echo 在命令字段中,以及 ${val} 在目标字段中。
Step 4 − Enter echo in the Command field, and ${val} in the Target field.
Step 5 - 输入执行脚本 在命令字段中,输入返回 ${val} + 1 在目标字段中,以及 val 在值字段中。
Step 5 − Enter execute script in the Command field, return ${val} + 1 in the Target field, and val in the Value field.
Step 6 - 输入结束 在命令字段中。
Step 6 − Enter end in the Command field.
Step 7 - 单击顶部运行所有测试,并等待测试执行完成。在我们的示例中,我们将看到:运行:1,失败:0,以及一根绿条表示测试在无任何失败的情况下成功运行。另外,完成成功的消息出现在日志中。
Step 7 − Click on the Run all tests from the top, and wait for the test execution to complete. In our example, we would see Runs: 1, Failures: 0, and a green bar denoting the test ran successfully without any failures. Also, the message that the completed successfully appeared under the Log.
Conclusion
这就总结了我们关于 Selenium IDE - 控制流教程的全面解读。我们已经开始描述了 JavaScript 表达式、条件分支上的基本控制流命令、循环上的基本控制流命令,以及示例以便引导如何使用这些命令以及 Selenium。
This concludes our comprehensive take on the tutorial on Selenium IDE - Control Flow. We’ve started with describing JavaScript Expressions, basic Control Flow Commands on Conditional Branching, Basic Control Flow Commands on Looping, and examples to walk through how to use these commands along with Selenium.
这使你掌握了 Selenium IDE 控制流程的深入知识。明智的做法是继续实践你学到的知识,并探索其他与 Selenium 相关的知识,以加深理解并拓展视野。
This equips you with in-depth knowledge of the Control Flow in Selenium IDE. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.