Selenium 简明教程

Selenium IDE - Script Debugging

Selenium IDE 通过多个脚本调试选项提供帮助,我们可以用这些选项对故障脚本进行故障排除,或出于任何其他目的。脚本调试通过调查和解决错误(如果有)来确保脚本按预期的方式运行。

Selenium IDE provides multiple script debugging options with the help of which we can troubleshoot scripts on failures, or for any other purposes. A debugging of a script is done to ensure that the script should run as it is intended to do by investigating and solving errors, if any.

Selenium IDE 中有多种调试技术和功能可用。

There are multiple debugging techniques and features available in Selenium IDE.

Step-By-Step Execution

Step over current command 按钮允许你逐步运行测试并检查是否有问题。

The Step over current command button allows you to run the test step by step and examine if something is not working.

selenium script debugging 1

重复单击此按钮,我们可以查看任何测试步骤是否未按预期工作。在下图中,在单击 Step over current command 按钮后,步骤 1 执行成功。然后 Paused in debugger 显示在要执行的下一步(这里为步骤 2)旁边,然后变为黄色。

On clicking on this button repeatedly, we can see if any of the test steps are not working as expected. In the below image, the step 1 executed successfully, after we clicked the Step over current command button. Then Paused in debugger appeared along with the next step to be executed(here the step2) became yellow.

selenium script debugging 3

在下图中,它现在显示另一个按钮 Resume test execution 。我们还可以单击该按钮并重新触发执行。

In the below image now it shows another button Resume test execution. We can also click on it and re-trigger the execution.

selenium script debugging 2

再次单击 Step over current command 按钮后,将执行步骤 2,依此类推。

Once again when the Step over current command button is clicked, the step2 will be executed, and so on.

selenium script debugging 4

由于执行已完成,我们将收到结果消息。如果测试通过,则我们将在 Selenium IDE 底部的日志选项卡中收到消息 - <TEST NAME> 已成功完成。在这里,我们的测试名称是 Test4,因此收到了消息 - Test4 completed successfully

As the execution has been completed, we would get the message of the result. In case the test passed, we would get the message - <TEST NAME> completed successfully under the Log tab at the bottom of the Selenium IDE. Here, our test name is Test4, hence received the message - Test4 completed successfully.

除此之外,所有的测试步骤(包括测试用例名称)都变为绿色,表示所有步骤均 PASS。所有在日志选项卡下显示的步骤均显示绿色 OK。

Apart from that, all the test steps, along with the test case name turned green, reflecting a PASS for all the steps. All the steps which reflected under the Log tab, showed an OK in green.

Adding Breakpoints

我们可以单击测试中的行号,以便在 Selenium IDE 中添加断点,对测试步骤进行调试。在下图中,我们已将断点添加到行号 2 和 3(在 Selenium IDE 中以蓝色突出显示)。

We can click on the line numbers in the test to add breakpoints in order to debug test steps in Selenium IDE. In the below image, we have added breakpoints to line numbers 2, and 3 (highlighted in blue in Selenium IDE).

selenium script debugging 5

然后,当我们运行测试时,执行过程将首先自动停在第 2 行。然后, Paused in debugger 出现,连同要执行的下一步(此处为 step2)变为黄色。

After which, when we run the test, the execution will first automatically stop in line 2. Then the Paused in debugger appeared along with the next step to be executed (here the step2) becomes yellow.

我们将再次单击 Step over current command 按钮,以便帮助我们继续执行下一步。

We will click again on the Step over current command button which will help us to continue with the next step of execution.

selenium script debugging 6

当我们移动到下一步时,将重复同一流程。

The same process will be repeated, once we move to the next step.

selenium script debugging 7

执行完成后,我们将收到结果消息。如果测试通过,我们将收到消息 - 在 Selenium IDE 底部的日志选项卡下成功完成 <TEST NAME>。此处,我们的测试名称为 Test4,因此收到了消息 - Test4 completed successfully

Once the execution would be completed, we would get the message of the result. In case the test passed, we would get the message - <TEST NAME> completed successfully under the Log tab at the bottom of the Selenium IDE. Here, our test name is Test4, hence received the message - Test4 completed successfully.

除此之外,所有的测试步骤(包括测试用例名称)都变为绿色,表示所有步骤均 PASS。所有在日志选项卡下显示的步骤均显示绿色 OK。

Apart from that, all the test steps, along with the test case name turned green, reflecting a PASS for all the steps. All the steps which reflected under the Log tab, showed an OK in green.

selenium script debugging 8

我们可以通过单击 Disable breakpoints 按钮禁用所有断点。再次单击同一按钮将启用所有断点。

We can disable all the breakpoints by clicking the Disable breakpoints button. All breakpoints would get enabled on clicking the same button again.

selenium script debugging 9

还可以通过右键单击任何步骤并选择选项 Toggle Breakpoint 来删除/添加到步骤中的断点。

The breakpoints can also be removed/added to steps by right clicking on any step, and selecting the option Toggle Breakpoint.

selenium script debugging 10

Debugger Command

我们可以使用 JavaScript 命令 debugger 编程地暂停 Selenium IDE 测试。我们需要右键单击一个步骤并选择选项插入新命令。

We can programmatically pause Selenium IDE tests by using the JavaScript command debugger. We would need to right click on a step and select the option Insert new command.

selenium script debugging 11

在命令字段中输入调试器以暂停执行。

Enter debugger in the Command field to pause the execution.

selenium script debugging 12

一旦触发测试执行,控件将停止在调试器步骤,我们需要使用 Resume test execution 按钮恢复执行。然后, Paused in debugger 连同测试名称将变为黄色。

Once the test execution is triggered, the control will stop in the debugger step and we will need to resume execution using the Resume test execution button. Then the Paused in debugger along with the test name would become yellow.

selenium script debugging 13

执行完成后,我们将收到结果消息。如果测试通过,我们将收到消息 - 在 Selenium IDE 底部的日志选项卡下成功完成 <TEST NAME>。此处,我们的测试名称为 Test5,因此收到了消息 - Test5 completed successfully

Once the execution would be completed, we would get the message of the result. In case the test passed, we would get the message - <TEST NAME> completed successfully under the Log tab at the bottom of the Selenium IDE. Here, our test name is Test5, hence received the message - Test5 completed successfully.

除此之外,所有的测试步骤(包括测试用例名称)都变为绿色,表示所有步骤均 PASS。所有在日志选项卡下显示的步骤均显示绿色 OK。

Apart from that, all the test steps, along with the test case name turned green, reflecting a PASS for all the steps. All the steps which reflected under the Log tab, showed an OK in green.

selenium script debugging 14

Pause on Exceptions

Selenium IDE 中的异常暂停功能有助于在遇到错误时暂停测试异常。暂停后,我们可以改正步骤并恢复执行。

The Pause on Exception feature in Selenium IDE helps to pause the exception of a test in case an error is encountered. Once paused, we can rectify the step and resume execution.

让我们来看一下前面讨论的示例,在该测试用例中,我们有意让一个步骤失败,方法是在第 2 行中更改定位器 id 值。此处,我们已将定位器值更改为 id=names。

Let us take the example discussed previously, and intentionally we would fail a step in that test case, by changing the locator id value in line 2. Here, we have changed the locator value to id=names.

selenium script debugging 15

执行完成后,我们将收到失败的测试,并且在失败步骤之后的其余测试步骤将不会执行。

Once the execution is completed, we would get the failed test and the rest of the test steps after the failed step would not get executed.

selenium script debugging 16

但是,为了防止执行中止,我们需要单击 Pause on exceptions 按钮,然后再次运行测试。

However, to prevent the execution from being aborted, we would need to click on the Pause on exceptions button, and then run the test again.

selenium script debugging 17

在下图中,由于不正确的定位器值,执行在第 2 行停止( Paused in debugger ),并且测试名称变为黄色。

In the below image, the execution stopped at line 2 (due to incorrect locator value) with the Paused in debugger along with the test name turning yellow.

selenium script debugging 20

我们将定位器值从 id=names 更正为 id=name,然后再次单击 Resume test execution 按钮。

We will correct the locator value from id=names to id=name and again click on the Resume test execution button.

selenium script debugging 18

执行完成后,我们将收到结果消息。如果测试通过,我们将收到消息 - 在 Selenium IDE 底部的日志选项卡下成功完成 <TEST NAME>。此处,我们的测试名称为 Test5,因此收到了消息 - Test5 completed successfully

Once the execution would be completed, we would get the message of the result. In case the test passed, we would get the message - <TEST NAME> completed successfully under the Log tab at the bottom of the Selenium IDE. Here, our test name is Test5, hence received the message - Test5 completed successfully.

除此之外,所有的测试步骤(包括测试用例名称)都变为绿色,表示所有步骤均 PASS。所有在日志选项卡下显示的步骤均显示绿色 OK。

Apart from that, all the test steps, along with the test case name turned green, reflecting a PASS for all the steps. All the steps which reflected under the Log tab, showed an OK in green.

selenium script debugging 19

这结束了我们对 Selenium IDE 教程:脚本调试的全面探究。我们从描述一个示例开始,然后逐步讲解了不同的调试策略,如逐行执行、添加断点、调试器命令、暂停异常,并说明如何与 Selenium 一起使用它们。这让你深入了解了 Selenium IDE 中的调试策略。明智的做法是持续实践你学到的内容,并探索与 Selenium 相关的其他内容,以加深理解并拓宽视野。

This concludes our comprehensive take on the tutorial on Selenium IDE - Script Debugging. We’ve started with describing an example and walked through the different debugging strategies like the step-by-step execution, adding breakpoints, debugger command, Pause on exceptions, and illustrating how to use them along with Selenium. This equips you with in-depth knowledge of the debugging strategies 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.