Selenium 简明教程

Selenium IDE - JSON Data File

Selenium IDE 可用于处理 JSON 数据文件。JavaScript 对象表示法或 JSON 是一种数据交换格式,且具有轻量级性质。它具有易于读取的模式,机器可以快速分析和生成 JSON 数据。

Selenium IDE can be used to work with the JSON Data file. JavaScript Object Notation or JSON is a data interchange format and is of lightweight nature. It has an easy readable pattern and machines can analyze and produce JSON data quickly.

Selenium IDE 'store json' Command

Selenium IDE 可用于使用 store json 命令处理 JSON 数据文件。它是一个访问器命令,用于将 JSON 数据存储在变量中。

Selenium IDE can be used to work with the JSON data file using the store json command. It is an accessor command which is used to store the JSON data in a variable.

Store JSON Data to a Variable in Selenium IDE

让我们举一个例子,我们将使用 Selenium IDE 借助访问器命令 - store json 将 JSON 数据存储到变量中。

Let us take an example where we will store the JSON data to a variable using the Selenium IDE taking the help of the accessors command - store json.

在 Selenium IDE 中存储 json 数据和访问它们的步骤如下所列−

The steps to store json data and access them in Selenium IDE 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.

此外,还提供了带有文本 - the Selenium IDE project page 的链接,单击该链接将使我们进入 Selenium 集成开发环境文档页面。

Also, a link with the text - the 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 下输入项目名称。最后,我们将单击“确定”。

Step 3 − Click on the Create a new project link, then enter a project name under the Please provide a name for your new project. Finally, we would click OK.

Step 4 − Selenium IDE 中的工作区已准备就绪。

Step 4 − We would have the workspace ready in the Selenium IDE.

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 − 在命令字段中输入 store json,在目标字段中输入 {"Language":"Java","Name":"Selenium"},在值字段中输入 val。请注意,{"Language":"Java","Name":"Selenium"} 是我们要使用变量 val 存储的 JSON 数据。

Step 6 − Enter store json in the Command field, {"Language":"Java","Name":"Selenium"} in the Target field, and val in the Value field. Please note, {"Language":"Java","Name":"Selenium"} is the json data we would like to store using the variable val.

Step 7 − 在命令字段中输入 echo,在目标字段中输入 ${val}。请注意,这将打印我们在第 6 步中访问的 JSON 对象,位于 Selenium IDE 的日志下方。

Step 7 − Enter echo in the Command field, ${val} in the Target field. Please note, this would print the json object we accessed in Step6 under the Log in Selenium IDE.

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

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

selenium ide json data file 1

Step 9 − 单击顶部的全部运行测试,并等待测试执行完成。在示例中,我们将看到运行:1,失败:0,以及一个绿色条表示测试成功运行,没有失败。此外,测试成功完成的消息会显示在日志下方。

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

selenium ide json data file 2

在上面的示例中,我们获取了日志下的 JSON 数据。此外,在顶部用例旁边有一个绿色对勾,表示测试通过。

In the example above, we obtained the json data under the Log. Besides, we got a green tick beside the test case at the top, signifying a passed test.

除此之外,所有的测试步骤(包括测试用例名称)都变为绿色,表示所有步骤均 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 IDE 中,我们将在测试步骤级别获取有关测试结果的信息,并且仅当所有步骤都成功执行时,我们才将在测试用例级别获得 PASS。

Thus, in Selenium IDE, we would get the information about the test results both at the test step level and only if all the steps would be executed successfully, we would get a PASS at the test case level as well.

此外,我们可以推断出在一个步骤中使用的变量可以使用语法:${<variable name>} 在其他步骤中访问。在第 2 步中,我们在测试中遵循了这种方法。echo 命令用于获取日志选项卡下的 JSON 对象。

Moreover, we would infer that variables used in one step can be accessed further in other steps using the syntax: ${<variable name>}. In Step2 we followed this approach in our test. The echo command was used to get the JSON objects under the Log tab.

一旦测试执行,项目将保存在扩展名为 .side 的文件中。这里,我们使用名称 Test13.side 保存了该项目。在记事本、Notepad++ 或任何文本文件格式中打开此文件时,我们将以 JSON 格式获取测试的数据和其他信息。

Once the test was executed, the project would be saved in a file having .side extension. Here, we had saved the project with the name Test13.side. On opening this file in Notepad, Notepad++, or any text file format, we would get the data and other information of the test in JSON format.

selenium ide json data file 3

从测试 .side 文件接收到的完整 JSON 数据 −

The complete JSON data received from the test .side file −

{
   "id": "9da88821-edda-4f81-9e7b-57b89080eadc",
   "version": "2.0",
   "name": "Test13",
   "url": "",
   "tests": [{
      "id": "a87746b2-6478-47ce-b241-196675f0c91d",
      "name": "Untitled",
      "commands": [
         {
            "id": "86c94e67-93ba-44ba-89c5-170d1669f333",
            "comment": "",
            "command": "open",
            "target": "https://www.tutorialspoint.com/selenium/practice/register.php",
            "targets": [],
            "value": ""
         }, {
            "id": "62ed3587-a53b-4f10-ad98-90fc3223018a",
            "comment": "",
            "command": "storeJson",
            "target": "{\"Language\":\"Java\", \"Name\":\"Selenium\"}",
            "targets": [],
            "value": "val"
         }, {
            "id": "d5676067-fb60-46fa-9b02-cd7a0b2ae630",
            "comment": "",
            "command": "echo",
            "target": "${val}",
            "targets": [],
            "value": ""
         }, {
            "id": "3ce68f7d-2958-4e66-ad33-d9b9a4e5aa7f",
            "comment": "",
            "command": "close",
            "target": "",
            "targets": [],
            "value": ""
         }
      ]
   }],
   "suites": [{
      "id": "7f58de7c-b22c-4fd1-bd59-00e4644995a6",
      "name": "Default Suite",
      "persistSession": false,
      "parallel": false,
      "timeout": 300,
      "tests": ["a87746b2-6478-47ce-b241-196675f0c91d"]
   }],
   "urls": [],
   "plugins": []
}

在上面的示例中,我们有四个测试步骤,命令为 open、store json、echo 和 close。所有这些命令及其目标和值都已捕获在 JSON 文件中。测试中传递的 JSON 数据 {"Language":"Java", "Name":"Selenium"} 也已捕获在 JSON 文件中。

In the above example, we had four test steps, with the commands open, store json, echo, and close. All these commands along with their Target, and Value had been captured in the JSON file. The JSON data {"Language":"Java", "Name":"Selenium"} passed in the test had also been captured in the json file.

这就结束了我们在 Selenium IDE - JSON 数据文件的教程中的全面介绍。我们首先描述了如何使用 Selenium IDE 中的 store json 命令访问 JSON 数据,并通过一个示例来说明如何使用 store json 命令和具有 side 格式的文件来保存 Selenium IDE 测试,以及稍后访问同一文件以获取 JSON 格式的测试信息。

This concludes our comprehensive take on the tutorial on Selenium IDE - JSON Data File. We’ve started with describing how to access a json data using the store json command in Selenium IDE, and walked through an example to illustrate how to use the store json command and the file with side format to save the Selenium IDE test, and later access the same file to get the test information in a json format.

这让你对 Selenium IDE 中的 JSON 数据文件有了深入的了解。明智的做法是不断练习你所学到的知识,并探索与 Selenium 相关的内容以加深你的理解并拓展你的视野。

This equips you with in-depth knowledge of the JSON Data file 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.