Selenium 简明教程

Selenium IDE - Pattern Matching

Selenium IDE 可用于对测试进行模式匹配,特别是对于需要验证场景的测试。与定位器一样,模式是 Selenium 经常使用的一种参数类型。它允许用户借助特殊字符描述模式。很多时候,我们要验证的文本是动态的,在这种情况下,模式匹配非常有用。

Selenium IDE can be used for pattern matching for the tests especially for tests requiring verification scenarios. Like locators, patterns are a type of parameter frequently used by Selenium. It allows users to describe patterns with the help of special characters. Many times, the text that we would like to verify is dynamic, in that case, pattern matching is very useful.

模式匹配通常与所有验证点命令一起使用,如 verify text、verify not text、verify title、verify value、verify、assert text、assert alert、assert not text、assert prompt、assert selected value、assert title、assert not selected value、assert selected label 等。

Pattern matching is commonly used with all the verification point commands like verify text, verify not text, verify title, verify value, verify, assert text, assert alert, assert not text, assert prompt, assert selected value, assert title, assert not selected value, assert selected label, and so on.

Different Ways of Pattern Matching

  1. Globbing

  2. Regular Expressions

  3. Exact Matching

Globbing

Globbing 用于将字符串与 glob(即通配符表达式)相匹配。在 Linux 或 Windows 中搜索特定文件类型(如 .doc、.jpg 等)时,通常用于文件匹配模式。

Globbing is used to match a string with respect to a glob(which is the wild character expression). It is commonly used for file matching patterns in Linux or Windows while searching for a certain file type like *.doc, *.jpg, and so on.

在 Selenium 中有多种使用 Globbing 的方法,例如 * 用于匹配任何数量或顺序的字符,? 用于匹配一个独特的字符,[] 用于匹配括号内提供的任何一个字符。它是一个字符类。例如,[0-9] 将匹配任何数字。使用 Globbing 进行模式匹配默认情况下在 Selenium 中可用,并且可以选择在 Selenium IDE 中的测试步骤中提及关键字 glob。

There are multiple ways to use globbing in Selenium like * which is used to match any count or order of characters ? which is used to match a unique character, and [] which is used to match any one character provided within the brackets. It is a character class. For example, [0-9] will match any digit. The pattern matching with globbing is by default available in Selenium and it is optional to mention the keyword glob in the test step in the Selenium IDE.

让我们以如下页面的示例为例,我们将使用 Globbing 模式匹配来验证文本警报和标题 Selenium - 自动化实践。

Let us take the example of the below page, where we would verify the text Alerts and header Selenium - Automation Practice using the globbing pattern matching.

selenium ide pattern matching 1

要遵循的步骤如下所示 -

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.

Step 3 − 单击新建项目链接,然后输入一个项目名称,比如 Please provide a name for your new project 下的 Test7。最后,我们将单击确定按钮。

Step 3 − Click on the Create a new project link, then enter a project name, say, Test7 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,在目标字段中输入 https://www.tutorialspoint.com/selenium/ 以启动应用程序。

Step 5 − Enter open in the Command field, and https://www.tutorialspoint.com/selenium/ in the Target field to launch the application.

Step 6 - 在“命令”字段中输入断言文本,在“目标”字段中输入 xpath=/html/body/main/div/div/div[2]/h1,在“值”字段中输入 glob: Alert*。请注意,xpath=/html/body/main/div/div/div[2]/h1 是文本,glob: Alert* 是要匹配的模式的值。

Step 6 − Enter assert text in the Command field, xpath=/html/body/main/div/div/div[2]/h1 in the Target field, and glob: Alert* in the Value field. Please note, xpath=/html/body/main/div/div/div[2]/h1 is the text and glob: Alert* is the value to be a matched pattern.

Step 7 - 在“命令”字段中输入断言文本,在“目标”字段中输入 xpath=/html/body/div/header/div[2]/h1,在“值”字段中输入 glob: Selenium - Automation*。请注意,xpath=/html/body/div/header/div[2]/h1 是标题,glob: Selenium - Automation* 是要匹配的模式的值。

Step 7 − Enter assert text in the Command field, xpath=/html/body/div/header/div[2]/h1 in the Target field, and glob: Selenium - Automation* in the Value field. Please note, xpath=/html/body/div/header/div[2]/h1 is the header and glob: Selenium - Automation* is the value to be a matched pattern.

Step 8 − 在命令字段中输入 close 以关闭浏览器窗口。

Step 8 − Enter close in the Command field to close the browser window.

selenium ide pattern matching 2

Step 9 - 在左窗格中单击出现的三个点,然后选择“重命名”选项。

Step 9 − Click on the three dots appearing at the left pane, then select the Rename option.

Step 10 - 在“重命名测试用例”字段下输入名称(例如 Test14),然后单击“重命名”。输入的名称将显示在 Selenium IDE 的左侧。

Step 10 − Enter a name, say Test14 under the Rename Test case field, then click on Rename. The entered name would appear on the left of Selenium IDE.

Step 11 - 从顶部单击“全部运行测试”,然后等到测试执行完成。在我们的示例中,我们会看到“运行:1”、“失败:0”,以及一个绿色条表示测试成功运行且无任何失败。而且,消息“Test14 完成成功”显示在“日志”下方。

Step 11 − 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 Test14 completed successfully appeared under the Log.

在上述示例中,我们通过 globbing 技术验证了文本。

In the example above, we had verified the texts with the help of the globbing.

Exact Matching

精确匹配用于按字符串原样匹配。例如,如果需要与字符串值精确匹配,可以使用精确模式。在精确匹配中,运算符 * 作为普通字符工作,而不是作为模式匹配通配符。让我们使用上面讨论过的相同示例并使用精确模式技术。

Exact matching is used to match a string as it is present. For example, if an exact match with the string value is required, the exact pattern can be used. In exact matching, the operator * works as a normal character rather than for pattern matching wildcard characters. Let us take the same example as discussed above and use exact pattern technique.

要遵循的步骤如下所示 -

The steps to be followed are listed below −

Step 1 - 按照前一个示例中的步骤 1 到 5 操作。

Step 1 − Follow Steps 1 to 5 of the previous example.

Step 2 - 在“命令”字段中输入断言文本,在“目标”字段中输入 xpath=/html/body/main/div/div/div[2]/h1,在“值”字段中输入 exact: Alerts。请注意,xpath=/html/body/main/div/div/div[2]/h1 是文本,exact: Alerts 是要精确匹配的值。

Step 2 − Enter assert text in the Command field, xpath=/html/body/main/div/div/div[2]/h1 in the Target field, and exact: Alerts in the Value field. Please note, xpath=/html/body/main/div/div/div[2]/h1 is the text and exact: Alerts is the value to be matched exactly.

Step 3 - 在“命令”字段中输入断言文本,在“目标”字段中输入 xpath=/html/body/div/header/div[2]/h1,在“值”字段中输入 exact: Selenium - Automation Practice。请注意,xpath=/html/body/div/header/div[2]/h1 是标题,exact: Selenium - Automation Practice 是要精确匹配的值。

Step 3 − Enter assert text in the Command field, xpath=/html/body/div/header/div[2]/h1 in the Target field, and exact: Selenium - Automation Practice in the Value field. Please note, xpath=/html/body/div/header/div[2]/h1 is the header and exact: Selenium - Automation Practice is the value to be matched exactly.

Step 4 - 按照前一个示例中的步骤 8 到 11 操作。

Step 4 − Follow Steps 8 to 11 of the previous example.

Regular Expressions

正则表达式在可用的模式匹配技术中最为有用。Selenium 支持 Javascript 支持的整套正则表达式模式。因此,用户不再受 *、?和 [] globbing 模式的限制。

Regular expressions are the most useful among the pattern matching techniques available. Selenium supports the complete set of regular expression patterns that Javascript supports. Hence the users are no longer limited by *, ? and [] globbing patterns.

要使用 RegEx 模式,我们需要用“regexp:”或“regexpi:”作前缀。“regexpi”前缀不区分大小写。glob: 和 exact: 模式是正则表达式模式的子集。使用 glob: 或 exact: 可以完成的所有操作也可以通过 RegExp 的帮助来完成。

To use RegEx patterns, we need to prefix with either "regexp:" or "regexpi:". The prefix "regexpi" is case-insensitive. The glob: and the exact: patterns are the subsets of the Regular Expression patterns. Everything that is done with glob: or exact: can be accomplished with the help of RegExp.

可在以下链接找到有关正则表达式的更多信息 -

There is more information on regular expressions available in the below link −

这结束了我们对 Selenium IDE - 模式匹配教程的全面介绍。我们首先描述了模式匹配的不同方法,例如 globbing、正则表达式和精确匹配,以及如何使用 Selenium。

This concludes our comprehensive take on the tutorial on Selenium IDE - Pattern Matching. We’ve started with describing different ways of Pattern Matching like globbing, regular expression, and exact match and how to use them with Selenium.

这让你掌握了 Selenium IDE 中模式匹配的深入知识。明智的做法是继续实践你学到的东西并探索与 Selenium 相关的其他内容,以加深你的理解并拓宽你的视野。

This equips you with in-depth knowledge of the Pattern Matching 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