Selenium 简明教程
Selenium with Github Tutorial
用 Selenium 编写的测试可以保留在 Github 中以实现版本控制。版本控制系统是一款应用程序,支持开发人员协同工作、共同协作和维护其工作的历史记录。Git 的云版本称为 Github 。
The tests written in Selenium can be maintained in Github for the version control. The version control system is an application which enables the developers to coordinate, work together, and maintain the history of their work. The cloud version of Git is called Github.
Prerequisites to Setup Selenium Tests in Github
Step 1 - 通过链接 https://github.com/signup 注册并创建一个 Github 帐户
Step 1 − Sign up and create a Github account using the link https://github.com/signup
Step 2 - 通过以下链接下载并安装 Java -
Step 2 − Download and install Java from the below link −
要更详细地了解如何设置 Java,请参阅以下链接 −
To get a more detailed view on how set up Java, refer to the below link −
一旦成功安装 Java,我们可通过从命令提示符运行命令 java 来确认其安装。
Once we have successfully installed Java, we can confirm its installation by running the command: java, from the command prompt.
C:\java
Step 3 − 确认 Java 版本,方法是运行命令 −
Step 3 − Confirm the version of the Java installed by running the command −
java –version
它将显示以下输出 −
It will show the following output −
openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment Homebrew (build 17.0.9+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.9+0, mixed mode, sharing)
执行的命令的输出表示系统中安装的 Java 版本是 17.0.9。
The output of the command executed signified that the java version installed in the system is 17.0.9.
Step 4 − 使用链接 https://maven.apache.org/download.cgi 在我们的系统中安装 Maven。
Step 4 − Install Maven in our system using the link https://maven.apache.org/download.cgi.
通过运行命令确认 Maven 的版本 −
Confirm the version of the Maven installed by running the command −
mvn –version
它将显示以下输出 −
It will show the following output −
Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)
Maven home: /opt/homebrew/Cellar/maven/3.9.6/libexec
Java version: 21.0.1, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk/21.0.1/libexec/openjdk.jdk/Contents/Home
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "14.0", arch: "aarch64", family: "mac"
Step 5 − 从以下链接下载并安装代码编辑器 IntelliJ 以编写和运行 Selenium 测试 −
Step 5 − Download and install the code editor IntelliJ from the below link to write and run the Selenium test −
Step 6 − 从链接 https://git-scm.com/ 下载并安装 Git。
Step 6 − Download and install Git from the link https://git-scm.com/.
Step 7 − 通过运行以下命令确认 Git 的版本 −
Step 7 − Confirm the version of the Git installed by running the below command −
git –version
执行的命令的输出表示系统中安装的 Git 版本。
The output of the command executed signified that the Git version installed in the system.
Step 8 − 启动 IntelliJ,IntelliJ IDEA 欢迎页面应出现。点击 New Project 按钮。
Step 8 − Launch IntelliJ Welcome to IntelliJ IDEA should appear. Clicked on the New Project button.
Step 9 − 在 Name: 字段中输入名称。选择 Language 为 Java,Build System 为 Maven,然后选择 JDK 版本,然后单击 Create 按钮。
Step 9 − Enter a name under Name: field. Selected Language as Java, Build System as Maven, and JDK version, then click on the Create button.
Step 10 − 输入一个 ArtifactId,然后单击 Create。
Step 10 − Enter an ArtifactId and then click on Create.
Step 11 − IntelliJ 编辑器设置应成功完成。
Step 11 − IntelliJ editor setup should be completed successfully.
Step 12 − 从链接 https://mvnrepository.com/artifact/ 添加 Selenium Maven 依赖项。
Step 12 − Add the Selenium Maven dependencies from the link https://mvnrepository.com/artifact/.
Step 13 − 在 IntelliJ 工作区中创建的 Maven 项目中,粘贴第 12 步中复制的依赖项,将其粘贴到 pom.xml 文件中。
Step 13 − Paste the dependency copied in the Step12 in the pom.xml file(available under the Maven Project created in the IntelliJ workspace).
Step 14 − 使用所有依赖项保存 pom.xml,并更新 Maven 项目。
Step 14 − Save the pom.xml with all the dependencies and update the maven project.
Step 15 - 在测试文件夹内的Maven项目SeleniumGit中,右键单击java文件夹,并创建一个包,例如TestCases。
Step 15 − Within the Maven project SeleniumGit, right click on the java folder within the test folder, and create a package, say TestCases.
Step 16 - 右键单击TestCases包,并选择新建菜单,然后单击Java类选项。
Step 16 − Right click on the TestCases package, and select the New menu, then click on the Java Class option.
Step 17 - 在新建Java类文件字段中输入一个文件名,例如MyGitTest,然后按Enter键。
Step 17 − Enter a filename, say MyGitTest within the New Java Class File field and press Enter.
Step 18 - 在MyGitTest.java文件中添加以下Selenium代码。
Step 18 − Add the below Selenium code in the MyGitTest.java file.
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;
public class MyGitTest {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage where we will identify an element
driver.get("https://www.tutorialspoint.com/selenium/practice/links.php");
// identify link with link text locator then click
WebElement l = driver.findElement(By.linkText("Created"));
l.click();
// identify text locator
WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[1]"));
System.out.println("Text appeared is: " + t.getText());
// Closing browser
driver.quit();
}
}
在 pom.xml 文件中添加的总依赖项−
Overall dependencies added in the pom.xml file −
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SeleniumGit</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.19.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
本例中遵循的项目结构 -
Project Structure followed in this example −
Step 19 - 运行测试,并等待运行完成。
Step 19 − Run the test and wait till the run is completed.
它将显示以下输出 −
It will show the following output −
Text appeared is: Link has responded with status 201 and status text Created
Process finished with exit code 0
在上述示例中,在带有消息的链接 Created 上执行单击后获得的文本为 Link has responded with status 201 and status text Created 。
In the above example, the text obtained after performing the click on the link Created with a message was Link has responded with status 201 and status text Created.
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Step 20 - 重新启动IntelliJ应用程序。
Step 20 − Restart IntelliJ application.
How to Push the Selenium Code to GitHub?
将Selenium代码推送到GitHub的步骤如下 -
The steps to push the Selenium code to GitHub are listed below −
Step 1 - 登录在先决条件部分中之前提到的第1步中在GitHub中设置的帐户。然后单击新建按钮以创建一个存储库。
Step 1 − Login to the account set up in GitHub as per Step1 mentioned previously in the Prerequisites section. Then click on the New button to create a repository.
Step 2 - 输入一个存储库名称,例如SeleniumGit,选择将存储库设为公开或私有,然后单击新建按钮。
Step 2 − Enter a Repository name, say SeleniumGit and select the repository as Public or Private and then click on the Create button.
Step 3 - 记录此存储库的HTTPs地址,如下面的图片所示 -
Step 3 − Record the HTTPs address of this repository as shown in the below image −
Step 4 - 在IntelliJ中打开之前创建的SeleniumGit项目,并单击VCS菜单。然后单击创建Git存储库按钮。
Step 4 − Open the SeleniumGit project in the IntelliJ created previously and click on the VCS menu. Then click on the Create Git Repository button.
Step 5 - 单击Git菜单,并选择提交选项
Step 5 − Click on the Git menu, and select the option Commit
Step 6 - 单击查看文件选项,选择要推送的文件,然后单击添加按钮。
Step 6 − Click on the View Files option, select the files to be pushed then click on the Add button.
Step 7 - 选中更改复选框,然后单击提交并推送按钮。输入GitHub用户名和电子邮件地址,并单击设置和提交按钮。
Step 7 − Check the Changes checkbox, and click on the Commit and Push button. Enter the GitHub username and Email address, and click on the Set and Commit button.
Step 8 - 单击定义远程链接,输入在第3步中记录的HTTP地址,然后单击确定按钮。
Step 8 − Click on the Define remote link, enter the HTTP address recorded in Step3, then click on the OK button.
Step 9 - 在登录GitHub中选择登录经由GitHub或使用令牌选项。
Step 9 − Select either the option Log In Via GitHub or Use Token within the Log In to GitHub.
Step 10 - 我们将在浏览器中导航至另一个页面,该页面带有在GitHub中授权链接。此外,消息 - 仅当页面是从JetBrains IDE打开时才继续显示。单击在GitHub中授权链接。
Step 10 − We would be navigated to another page in the browser with the Authorize in GitHub link. Also, the message - Please continue only if the page is opened from a JetBrains IDE should display. Click on the link Authorize in GitHub.
Step 11 − GitHub 和 IntelliJ 的成功集成应已建立。单击“授权 JetBrains”按钮。
Step 11 − The successful integration of GitHub and IntelliJ should be established. Click on the Authorize JetBrains button.
Step 12 − 消息 - 您已成功授权进入 GitHub。您可以关闭此页应显示。
Step 12 − The message - You have been successfully authorized in GitHub. You can close the page should be displayed.
Step 13 − 转到 IntelliJ 并单击“推送”按钮。
Step 13 − Move to the IntelliJ and click on the Push button.
Step 14 − 代码应通过消息 - 将 master 推送到新分支 origin/master 和提交的文件数,从 IntelliJ 推送到 GitHub。
Step 14 − Code should be pushed to the GitHub from IntelliJ with the message - Pushed master to new branch origin/master and the number of files committed.
Step 15 − 刷新在 GitHub 中创建的新存储库,并且从 IntelliJ 推送的代码将体现出来。
Step 15 − Refresh the new repository created in GitHub and pushed code from the IntelliJ would reflect.
How to Push updates and add new Selenium Code to GitHub?
在下面列出了将更新推送并添加新的 Selenium 代码到 GitHub 的步骤 −
The steps to push updates and add new Selenium code to the GitHub are listed below −
Step 1 − 对之前编写的 MyGitTest.java 文件进行一些更改。然后单击“Git”菜单,并选择“提交”选项。
Step 1 − Make some changes in the MyGitTest.java file written previously. Then click on the Git menu, and select the option Commit.
Step 2 − 选择“更改”部分下已更新的 MyGitTest.java 文件,添加一些提交消息,然后单击“提交并推送”按钮。
Step 2 − Select the MyGitTest.java file that was updated under the Changes section, add some commit message, then click on the Commit and Push button.
Step 3 − 单击“推送”按钮。
Step 3 − Click on the Push button.
Step 4 − 过 некоторое время后,代码从 IntelliJ 推送至 GitHub,并附带消息 - 将 1 个提交从 master 推送至 origin/master,以及提交的文件数量。
Step 4 − After some time, the code should be pushed to the GitHub from IntelliJ with the messages - Pushed 1 commit to master to origin/master and and the number of files committed.
Step 5 − 更改的文件 MyGitTest.java 将在 GitHub 存储库中体现出来,同时还要包含文件更新后的经过时间。
Step 5 − Changed file MyGitTest.java would reflect in the GitHub repository along with time passed after the file had been updated.
Step 6 − 转到 IntelliJ,并添加另一个文件 MyGitTest1.java。然后单击“将文件添加到 Git”弹出窗口中的“添加”按钮。
Step 6 − Move to IntelliJ and add another file MyGitTest1.java. Then click on the Add button within the Add File to Git popup.
Step 7 − 单击“Git”菜单。然后单击“提交”选项。
Step 7 − Click on the Git menu. Then click on the Commit option.
Step 8 − 单击“推送”按钮。
Step 8 − Click on the Push button.
Step 9 − 在“更改”部分下选择文件 MyGitTest1.java。添加提交消息,然后单击“提交并推送”按钮。
Step 9 − Select the file MyGitTest1.java under the Changes section. Add a commit message and then click on the Commit and Push button.
Step 10 − 过 некоторое время后,代码从 IntelliJ 推送至 GitHub,并附带消息 - 将 1 个提交从 master 推送至 origin/master,以及提交的文件数量:添加了一个文件 - MyGitTest1.java。
Step 10 − After sometime, the code should be pushed to the GitHub from IntelliJ with the messages - Pushed 1 commit to master to origin/master and the number of files committed: Added one more file - MyGitTest1.java.
Step 11 − 更改的文件 MyGitTest1.java 将在 GitHub 存储库中体现出来,同时还要包含文件更新后的经过时间。
Step 11 − Changed file MyGitTest1.java would reflect in the GitHub repository along with time passed after the file had been updated.
How to Clone a Selenium Repository From the GitHub?
在下面列出了从 GitHub 克隆 Selenium 存储库的步骤 −
The steps to clone a Selenium repository from the GitHub are listed below −
Step 1 − 打开 IntelliJ,并单击“从 VCS 获取”按钮。
Step 1 − Open the IntelliJ, and click on the Get from VCS button.
Step 2 − 单击 GitHub 选项卡,选择项目,比如说 SeleniumGit,再选择一个“目录”,然后单击“克隆”按钮。
Step 2 − Click on the GitHub tab, select a project say SeleniumGit, and choose a Directory, then click on the Clone button.
Step 3 − 等待一段时间,然后单击“信任项目”按钮。
Step 3 − Wait for sometime, and then click on the Trust Project button.
Step 4 − 从 GitHub 的代码应该克隆到本地并在 IntelliJ 中打开。
Step 4 − The code from the GitHub should be cloned to the local and opened in the IntelliJ.
Conclusion
Selenium GitHub 教程的全面讲解到此结束。我们从介绍在 GitHub 中设置 Selenium 测试的 prerequisite 开始,然后介绍如何将 Selenium 代码推送到 GitHub、如何推送更新以及将新的 Selenium 代码添加到 GitHub,以及如何从 GitHub 克隆 Selenium 存储库。这样便为你配备了关于 Selenium - GitHub 的深入知识。明智的做法是不断实践你所学的内容,并探索与 Selenium 相关的其他内容,以加深你的理解并拓宽你的视野。
This concludes our comprehensive take on the tutorial on Selenium GitHub. We’ve started with describing prerequisites to set up Selenium tests in Github, how to push the Selenium Code to GitHub, how to push updates and add new Selenium code to GitHub, and how to clone a Selenium repository from the GitHub. This equips you with in-depth knowledge of the Selenium - GitHub. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.