Selenium 简明教程

Selenium with Maven & Jenkins Tutorial

借助 Maven 编写的 Selenium 测试可以与 Jenkins 集成,以便可以直接从 Jenkins 等持续集成工具触发测试,而不仅仅依赖于从编辑器进行本地执行。

Selenium tests written with the help of Maven can be integrated with Jenkins so that tests can be triggered directly from the continuous integration tools like Jenkins, rather than relying only on local execution from an editor.

What is Maven?

Maven 是一种项目管理和理解工具,为开发人员提供了完整的构建生命周期框架。开发团队几乎可以在很短的时间内自动执行项目的构建基础设施,因为 Maven 使用了标准目录布局和默认构建生命周期。

Maven is a project management and comprehension tool that provides developers a complete build lifecycle framework. Development team can automate the project’s build infrastructure in almost no time as Maven uses a standard directory layout and a default build lifecycle.

在涉及到多个开发团队的情况下,Maven 可以设置方法,以便在很短的时间内按照标准工作。由于大多数项目设置简单且可重复利用,Maven 让开发人员在创建报告、检查、构建和测试自动化设置时变得更为轻松。

In the case of multiple development teams, Maven can set-up the way to work as per standards in a very short time. As most of the project setups are simple and reusable, Maven makes the developer easy while creating reports, checks, build and testing automation setups.

有关 Maven 的更多信息,请参阅链接 - Maven

To get more information on the Maven, please refer to the link − Maven.

What is Jenkins?

Jenkins 是一个功能强大的应用程序,允许持续集成和持续交付项目,无论你使用的是什么平台。它是一个可以处理任何类型的构建或持续集成的免费源。我们可以将 Jenkins 与多个测试和部署技术集成在一起。

Jenkins is a powerful application that allows continuous integration and continuous delivery of projects, regardless of the platform you are working on. It is a free source that can handle any kind of build or continuous integration. We can integrate Jenkins with a number of testing and deployment technologies.

Jenkins 是一款持续集成工具,它是开源的,广泛用于构建和测试产品。对于开发人员而言,它非常有帮助,因为可以轻松地将更改合并到项目中。

Jenkins is a continuous integration tool which is open source and used extensively for building and testing products. It is very helpful from the developers point of view since changes can be incorporated easily to the projects.

在 Jenkins 中,使用插件可以实现持续集成。在将 Maven 与 Jenkins 集成时,可以使用 Jenkins 中的 Maven 特定插件来实现集成。

In Jenkins the continuous integration is achieved with the help of plugins. While Maven is to be integrated with Jenkins, it can be achieved with the help of Maven specific plugins in Jenkins.

由于使用插件将 Maven 与 Jenkins 集成,因此可以实现自动化整个构建过程。安装插件后,需要进行某些配置和设置才能完成 Maven 和 Jenkins 的集成。Jenkins 服务器上可以运行任意数量的 Maven 作业。这还有助于构建 Java 构建的任何可用于 Git 存储库的 Selenium 测试。

As Maven is integrated with Jenkins using the plugins, it makes it possible to automate the complete build process. Once the plugins are installed, there are certain configurations and settings that should be done to complete the Maven and Jenkins integration. There can be any number of Maven jobs running on the Jenkins server. This also helps to build any Selenium tests built on java available in Git repositories.

如需获取有关 Jenkins 的更多信息,请参阅链接− Jenkins

To get more information on the Jenkins, please refer to the link − Jenkins.

How to Integrate Maven & Jenkin?

Step 1 −导航到链接并下载 Jenkins war 文件 − https://www.jenkins.io/download/

Step 1 − Navigate to the link and download the Jenkins war file − https://www.jenkins.io/download/.

Step 2 −根据平台,单击并下载适用于 Jenkins 的 Generic Java Package(.war 文件)。Jenkins 可用于多种平台,如 Windows、Mac、Ubuntu 等。

Step 2 − Based on the platform, click and download the Generic Java Package(.war file) for Jenkins. Jenkins can be used on multiple platforms like Windows, Mac, Ubuntu, and so on.

selenium maven jenkins 1

Step 3 −对于 MacOS,单击 macOS 链接,并运行命令: brew install jenkins

Step 3 − For MacOS, click on the macOS link, and run the command: brew install jenkins.

Step 4 −运行命令: brew services start jenkins 以开始 Jenkins 服务。

Step 4 − Run the command: brew services start jenkins to begin the Jenkins service.

Step 5 −运行以下命令−

Step 5 − Run the below command −

sudo cat /Users/$(whoami)/.jenkins/secrets/initialAdminPassword.

此命令将提供初始管理员密码。

This command shall give the initial admin password.

Step 6 −Jenkins 默认在 8080 端口上运行。打开一个新浏览器并输入: localhost:8080 。应当显示 Jenkins 主页,输入在步骤 5 中获得的密码,然后单击“继续”按钮。

Step 6 − The Jenkins by default runs on the 8080 port. Open a new browser and enter: localhost:8080. The Jenkins home page should be displayed and enter the password obtained in Step5 and then click on the Continue button.

selenium maven jenkins 2

Step 7 −单击 Install suggested Plugins ,所有建议的插件将开始安装。

Step 7 − Click on the Install suggested Plugins and all the suggested plugins will start installing.

selenium maven jenkins 3

Step 8 −一旦所有建议的插件都安装完毕,请使用所有详细信息创建一个第一个管理员用户。然后单击“保存并继续”按钮。

Step 8 − Once all the suggested plugins are installed, create a First Admin User with all the details. Then click on the Save and Continue button.

selenium maven jenkins 4

Step 9 −应当出现实例配置页面。单击“保存并继续”按钮。

Step 9 − Instance Configuration page should come. Click on the Save and Continue button.

selenium maven jenkins 5

Step 10 −Jenkins 现在已成功安装,且会显示“Jenkins 已就绪!”消息。单击“开始使用 Jenkins”。

Step 10 − Jenkins is now successfully installed with the message Jenkins is ready!. Click on Start using Jenkins.

selenium maven jenkins 6

Step 11 −单击 Jenkins 主页上提供的“管理 Jenkins”链接。

Step 11 − Click on the Manage Jenkins link available on the Jenkins Home Page.

selenium maven jenkins 7

Step 12 −单击“插件”链接。

Step 12 − Click on the Plugins link.

selenium maven jenkins 8

Step 13 −单击“可用插件”,然后在“搜索”框中输入 Maven。所有可用的 Maven 插件都应显示为候选选项。选择所有 Maven 相关插件后,单击“安装”按钮。

Step 13 − Click on the Available Plugins, then enter Maven in the Search box. All the available Maven plugins should be displayed for selection. After selection of all the Maven related plugins, click on the Install button.

selenium maven jenkins 9

Step 14 −所有插件都成功安装后,单击“当安装完成且没有正在运行的作业时重启 Jenkins”复选框。

Step 14 − Once all the plugins are installed successfully, click on the Restart Jenkins when installation is complete and no jobs are running checkbox.

selenium maven jenkins 10

Step 15 −再次使用凭据登录到 Jenkins。

Step 15 − Login in to Jenkins with the credentials again.

selenium maven jenkins 11

Step 16 −单击 Jenkins 主页上的“新建项”。

Step 16 − Click on the New Item on the Jenkins home page.

selenium maven jenkins 12

Step 17 −在“输入项目名称”标签下输入项目名称,例如 Hybrid,然后单击“Maven 项目”选项。

Step 17 − Input a project name, say Hybrid under the Enter an Item name label, then click on the option Maven Project.

selenium maven jenkins 13

Step 18 - 在“常规”标签内的“描述”字段中输入一些描述。

Step 18 − Enter some description under the Description field within the General Tab.

selenium maven jenkins 14

Step 19 - 为“源代码管理”选择“无”。

Step 19 − Select None for the Source Code Management.

selenium maven jenkins 15

Step 20 - 移至“构建”部分并单击工具配置链接。

Step 20 − Move to the Build section and click on the tool configuration link.

selenium maven jenkins 16

Step 21 - 移至“Maven 安装”部分,然后单击“添加 Maven”按钮。

Step 21 − Move to the Maven installations section, and then click on the Add Maven button.

selenium maven jenkins 17

Step 22 - 在“名称”下输入一个名称,然后从下拉菜单中选择最新版本的 Maven。然后单击“应用”和“保存”按钮。

Step 22 − Enter a name under Name and select the latest version Maven from the dropdown. Then click on the Apply and Save buttons.

selenium maven jenkins 18

Step 23 - 在“根 POM”字段下输入 Maven 项目的 pom.xml 路径,并在“目标和选项”下输入 clean install。然后选择“无论构建结果如何,都运行”选项。最后,单击“应用”和“保存”按钮。

Step 23 − Enter the path of the pom.xml of the Maven project under the Root POM field and enter clean install under the Goals and options. Then select the option Run regardless of build result. Finally, click on the Apply and Save buttons.

selenium maven jenkins 19

Step 24 - 移至 Jenkins 仪表板并单击“管理 Jenkins”链接。

Step 24 − Move to the Jenkins Dashboard and click on the Manage Jenkins link.

selenium maven jenkins 20

Step 25 - 导航至“插件”部分并安装 TestNG 结果插件。安装完成后重启 Jenkins。

Step 25 − Navigate to the Plugins section and install the TestNG Results plugin. Restart the Jenkins once installation is complete.

selenium maven jenkins 21

Step 26 - 在 Jenkins 仪表板中,单击之前创建的项目 - Hybrid。

Step 26 − In the Jenkins Dashboard, click on the project - Hybrid created before.

selenium maven jenkins 22

Step 27 - 单击左侧的“配置”链接。

Step 27 − Click on the Configure link at the left.

selenium maven jenkins 23

Step 28 - 移至“后置构建操作”部分。

Step 28 − Move to the Post-build Actions section.

selenium maven jenkins 24

Step 29 - “发布 TestNG 结果”应可在下拉菜单中找到。

Step 29 − Publish TestNG results should be available in the dropdown.

selenium maven jenkins 25

Step 30 - 选择“发布 TestNG 结果”选项,然后单击“应用”和“保存”按钮。

Step 30 − Select the option Publish TestNG results and click on the Apply and Save buttons.

Step 31 - 单击“立即构建”链接,从 Jenkins 运行测试。

Step 31 − Click on the Build Now link, to run the test from Jenkins.

selenium maven jenkins 26

Step 32 - 在“构建历史记录”下单击任意构建号。

Step 32 − Click on any build number under the Build History.

selenium maven jenkins 27

Step 33 - 单击左侧的“测试结果”。

Step 33 − Click on Test Result to the left.

selenium maven jenkins 28

Step 34 - 测试结果中捕获了类名称以及通过、失败和跳过的测试用例总数和测试持续时间。

Step 34 − The class name along with the total number of passed, failed, and skipped test cases, and test duration captured in the test result.

selenium maven jenkins 29

由此,我们完成了关于 Selenium - Maven & Jenkins 教程的全面讲解。我们从描述 Maven 是什么、Jenkins 的用途以及如何在 Selenium 中集成 Maven 和 Jenkins 开始。

This concludes our comprehensive take on the tutorial on Selenium - Maven & Jenkins. We’ve started with describing what is Maven, what is the use of Jenkins, how to integrate Maven and Jenkins in Selenium.

这为您提供了 Selenium - Maven & Jenkins 的深入知识。明智的做法是不断实践您学到的知识并探索与 Selenium 相关的其他知识,以加深您的理解并拓宽您的视野。

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