Selenium 简明教程

Selenium with Robot Framework Tutorial

Selenium 可以与 Robot API 一起使用。Robot Framework 用于创建用于接受测试驱动开发的自动化框架。这种接受测试驱动开发方法基于关键字驱动的测试。Selenium 被广泛用于 Web 自动化测试。Selenium 是一款用于测试 Web 应用程序的开源且可移植的自动化软件测试工具。

Selenium can be used with Robot API. A Robot Framework is used for creating automation framework used for acceptance test driven development. This acceptance test driven development approach is based on keyword driven testing. Selenium is used extensively for web automation testing. Selenium is an open-source and a portable automated software testing tool for testing web applications.

What is a Robot Framework?

Robot Framework 由大量使用 Java、Python 等编程语言开发的库组成。除了 Robot Framework 中现有的关键字之外,我们还可以使用现有关键字的语法在 Robot Framework 中添加新关键字,以开发新测试。只需基本的编程知识就足以使用 Robot Framework。

A Robot Framework comprises a large number of libraries developed using Java, Python, and so on. Apart from the existing keywords available in the Robot framework, we can add new keywords in the Robot Framework using the syntax of the existing ones for developing new tests. Only basic programming knowledge is enough for using a Robot Framework.

Prerequisites to Setup Robot Framework

Step 1 − 从链接下载并安装 Python − https://www.python.org/downloads/

Step 1 − Download and install Python from the link − https://www.python.org/downloads/.

要更详细地了解如何设置 Python,请参阅链接 − Python

To get a more detailed view on how set up Python, refer to the link − Python.

一旦成功安装了 Python,我们就可以通过从命令提示符运行命令 python –version 来确认其安装。执行的命令的输出将指向机器中安装的 Python 版本。

Once we have successfully installed Python, we can confirm its installation by running the command: python –version, from the command prompt. The output of the command executed would point to the Python version installed in the machine.

Step 2 − 从链接安装名为 PyCharm 的 Python 代码编辑器 − https://www.jetbrains.com/pycharm/

Step 2 − Install the Python code editor called the PyCharm from the link − https://www.jetbrains.com/pycharm/.

使用此编辑器,我们可以开始处理 Python 项目以启动我们的测试自动化。要更详细地了解如何设置 PyCharm,请参阅链接 − PyCharm

Using this editor, we can start working on a Python project to start our test automation. To get a more detailed view on how to set up PyCharm, refer to the link − PyCharm.

Step 3 − 从命令提示符运行命令 pip install selenium 。这样可以安装 Selenium。要确认机器中安装的 Selenium 版本,请运行命令 −

Step 3 − Run the command: pip install selenium from the command prompt. This is done to install the Selenium. To confirm the version of Selenium installed in the machine, run the command −

pip show selenium

此命令的输出将给出结果 −

The output of this command shall give the result −

Name: selenium
Version: 4.19.0
Summary: None
Home-page: https://www.selenium.dev
Author: None
Author-email: None
License: Apache 2.0.

Step 4 − 从命令提示符运行命令 pip install robotframework 。这样可以安装 Robot Framework。要确认机器中安装的 Robot Framework 版本,请运行命令 −

Step 4 − Run the command: pip install robotframework from the command prompt. This is done to install the Robot Framework. To confirm the version of Robot Framework installed in the machine, run the command −

pip show robotframework

此命令的输出给出了以下结果 −

The output of this command gave the below result −

Name: robotframework
Version: 7.0
Summary: Generic automation framework for acceptance
   testing and robotic process automation (RPA)
Home-page: https://robotframework.org
Author: Pekka Klärck
Author-email: peke@eliga.fi
License: Apache License 2.0
Location: /Users/opt/anaconda3/lib/python3.8/site-packages
Requires:
Required-by:

Step 5 − 从命令提示符运行命令 pip install requests 。这样可以安装有助于自动执行 Rest API 的请求库。

Step 5 − Run the command: pip install requests from the command prompt. This is done to install the request library which would help to automate the Rest APIs.

Step 6 − 从命令提示符运行命令 pip install robotframework-requests 。这样可以安装有助于自动执行 Rest API 的 Robot Framework 请求库。

Step 6 − Run the command: pip install robotframework-requests from the command prompt. This is done to install the Robot Framework request library which would help to automate the Rest APIs.

Step 7 − 从命令提示符运行命令 pip install -U robotframework-jsonlibrary 。这样可以验证 JSON 响应。

Step 7 − Run the command: pip install -U robotframework-jsonlibrary from the command prompt. This is done to verify the JSON responses.

Step 8 - 步骤 8 完成后,重启 PyCharm。

Step 8 − Restart PyCharm once Step 8 has been completed.

Step 9 - 打开 PyCharm 并通过导航至“文件”菜单,创建一个新项目。

Step 9 − Open PyCharm and create a New Project by navigating to the File menu.

selenium robot framework tutorial 1

在“位置”字段中输入项目名称和位置,然后点击“创建”按钮。

Enter the project name and location within the Location field, and then click on the Create button.

selenium robot framework tutorial 2

Step 10 - 从 PyCharm 编辑器的右下角,选择“解释器设置”选项。

Step 10 − From the right bottom corner of the PyCharm editor, select the option Interpreter Settings.

selenium robot framework tutorial 3

从左侧选择“Python 解释器”选项,然后点击“+”。

Select the option Python Interpreter from the left, then click on ‘+’.

selenium robot framework tutorial 4

Step 11 - 在“可用包”弹出窗口中,输入包搜索框中的 selenium,然后搜索结果会出现在右侧的“说明”旁边。“说明”中包含了要安装的 Selenium 包的版本信息。

Step 11 − Enter selenium in the package search box inside the Available Packages popup, then the search results appear along with the Description to the right. The Description contains information about the version of the Selenium package that shall be installed.

还可以在“指定版本”字段旁边选择安装 Selenium 包的特定版本。然后点击“安装包”按钮。成功安装后,应显示消息“已成功安装包 selenium”。

There is also an option to install a specific version of the Selenium package beside the Specify version field. Then click on the Install Package button. After successful installation, the message Package ‘selenium’ installed successfully should display.

selenium robot framework tutorial 5

Step 12 - 在“可用包”弹出窗口中,输入包搜索框中的 robotframework,并以相同的方式安装它。

Step 12 − Enter robotframework in the package search box inside the Available Packages popup, and install it in the same way.

Step 13 - 在“可用包”弹出窗口中,输入包搜索框中的 requests,并以相同的方式安装它。

Step 13 − Enter requests in the package search box inside the Available Packages popup, and install it in the same way.

Step 14 - 在“可用包”弹出窗口中,输入包搜索框中的 robotframework-jsonlibrary,并以相同的方式安装它。

Step 14 − Enter robotframework-jsonlibrary in the package search box inside the Available Packages popup, and install it in the same way.

Step 15 - 在“可用包”弹出窗口中,输入包搜索框中的 robotframework-seleniumlibrary,并以相同的方式安装它。

Step 15 − Enter robotframework-seleniumlibrary in the package search box inside the Available Packages popup, and install it in the same way.

退出“可用包”弹出窗口。

Exit out of the Available Packages popup.

Step 16 - 在步骤 11、12、13、14 和 15 中安装的所有包都应反映在“包”下面。点击“确定”按钮。重启 PyCharm。

Step 16 − All the packages installed in the Steps 11, 12, 13, 14, and 15 should reflect under the Package. Click on the OK button. Restart PyCharm.

selenium robot framework tutorial 6

Step 17 - 通过右键单击“项目”文件夹,创建第一个测试用例。在这里,我们已将项目名称指定为 SeleniumPython。然后右键单击新创建的项目,单击“新建”选项,最后选择“目录”。

Step 17 − Create the first test case, by right clicking on the Project folder. Here, we have given the project name as SeleniumPython. Then right click on the newly created project, click on the New option, and finally select Directory.

selenium robot framework tutorial 7

Step 18 - 输入目录名称,如 Robot,并点击“Enter”。项目 Robot 应显示在 SeleniumPython 项目文件夹左下方。

Step 18 − Enter a directory name, say Robot and click on Enter. The project Robot should appear in the left under the SeleniumPython project folder.

selenium robot framework tutorial 8

Step 19 - 导航至 PyCharm“设置”,并单击“插件”选项卡。

Step 19 − Navigate to the PyCharm Settings, and click on Plugins tab.

selenium robot framework tutorial 9

Step 20 - 在搜索框中输入 IntelliBotSelenium,并单击“安装”按钮。安装完此插件后,将重启 PyCharm。这样做是为了让 PyCharm 编辑器能够识别所有具有扩展名为 .robot 的机器人相关测试用例。

Step 20 − Enter IntelliBotSelenium in the search box, and click on the Install button. After installing this plugin, restart the PyCharm. This is done so that all the robot related test cases having extension as .robot can be identified by the PyCharm editor.

selenium robot framework tutorial 10

Step 21 - 右键单击 directory Robot,并创建一个文件,如 GetRequest.robot。

Step 21 − Right click on the directory Robot, and create a file, say GetRequest.robot.

selenium robot framework tutorial 11

Step 22 - 添加下面的代码以触发 Get 请求

Step 22 − Add the below code to trigger a Get request

*** Settings ***
Library  RequestsLibrary

*** Variables ***
${baseUrl} https://api.restful-api.dev/objects
${object}  7

*** Test Cases ***
Get_Object
   create session firstSession ${baseUrl}
   ${response} = get request firstSession ${object}
   log to console ${response.status_code}
   log to console ${response.content}
   log to console ${response.header}

综上所述,我们全方位地了解了 Selenium - Robot API 的教程。我们从描述什么是 Robot Framework 以及如何使用 Python 和 Selenium 来设置 Robot Framework 开始。

This concludes our comprehensive take on the tutorial on Selenium - Robot API. We’ve started with describing what a Robot Framework is and how to set up a Robot Framework using Python and Selenium.

这让你对 Selenium - Robot API 有了更深入的认识。明智的做法是反复练习你学到的知识,并探索与 Selenium 相关的其他内容来加深你的理解,开阔你的视野。

This equips you with in-depth knowledge of the Selenium - Robot API. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.