Selenium 简明教程
IntelliJ for Selenium
我们需要使用编辑器配置 Selenium 来执行自动化测试。有许多编辑器可用,例如:Eclipse、IntelliJ、Atom、Sublime 等。使用这些编辑器,我们可以开始一个 Java 项目来启动测试自动化。
We would need to configure Selenium with an editor to execute the automated tests. There are several editors available for example: Eclipse, IntelliJ, Atom, Sublime, and so on. Using these editors, we can begin working on a Java project to start test automation.
Prerequisites to set up IntelliJ for Selenium
使用以下链接下载并安装 Java(高于 17 的版本),并使用以下命令验证它是否可用:
Download and install Java (version above 17) with the below link, and validate if it is available with the following command −
java –version
要获取有关 Java 配置的更多信息,请参阅以下链接:
To get more about Java configuration, please refer to the below link −
使用以下链接下载并安装 maven,并使用以下命令验证它是否可用:
Download and install maven with the below link, and validate if it is available with the following command −
mvn -version
要获取有关 Maven 配置的更多信息,请参阅链接 Maven Environment Setup
To get more about Maven configuration, refer to the link Maven Environment Setup
Sets up IntelliJ for Selenium
Step 1 - 使用以下链接导航到 IntelliJ(Jetbrains 的产品)的官方网站,然后单击下载 -
Step 1 − Navigate to the official website of IntelliJ (which is a product of Jetbrains) using the below link and then click on Download −
Step 2 - 一旦我们导航到下一页,我们将可以选择在各种操作系统中下载 IntelliJ,如 Windows、macOS 和 Linux。根据当前操作系统单击选项卡。
Step 2 − Once we navigate to the next page, we will get the option to download IntelliJ in various operating systems, like Windows, macOS, and Linux. Click on the tab based on the current operating system.
IntelliJ 有两个版本,付费版和社区版(免费)。
IntelliJ comes in two versions, Paid and Community(which is free).
Step 3 - 我们将下载社区版。为此,我们将向下移动到 IntelliJ IDEA 社区版部分,然后单击下载。
Step 3 − We would download the Community version. For that we would move down to the IntelliJ IDEA Community Edition section and then click on Download.
Step 4 - IntelliJ 徽标应显示几秒钟,然后将出现 JETBRAINS 社区版条款。单击复选框接受条款和条件,然后单击继续。
Step 4 − IntelliJ logo should display for a few seconds, and next the JETBRAINS COMMUNITY EDITION TERMS should appear. Click the checkbox to accept the terms and conditions, then click on Continue.
Step 5 - 欢迎使用 IntelliJ IDEA 将出现。单击新建项目按钮。
Step 5 − Welcome to IntelliJ IDEA should appear. Click on the New Project button.
Step 6 - 在名称字段下输入名称。选择语言为 Java,构建系统为 Maven 和 JDK 版本,然后单击创建。
Step 6 − Enter a name under Name: field. Select Language as Java, Build System as Maven, and a JDK version, then click on Create.
Step 7 - 输入 ArtifactId,然后单击创建。
Step 7 − Enter an ArtifactId and then click on Create.
Step 8 - IntelliJ 编辑器设置应成功完成。
Step 8 − IntelliJ editor setup should be completed successfully.
Step 9 - 从链接 Selenium Java 将 Selenium Maven 依赖项添加到 pom.xml 文件
Step 9 − Add the Selenium Maven dependencies to the pom.xml file from the link Selenium Java
Step 10 - 在中央选项卡下选择并单击版本链接。我们导航到 Selenium Java >> <version> 页面。复制 Maven 选项卡下的依赖项。
Step 10 − Selected and clicked on a version link under the Central tab. We navigated to the Selenium Java >> <version> page. Copied the dependency under Maven tab.
依赖示例 −
Dependency example −
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
在 IntelliJ 工作区中创建的 Maven 项目(在 pom.xml 文件中)粘贴了步骤 9 中复制的依赖。
Step 11 − Pasted the dependency copied in the Step 9 in the pom.xml file (available under the Maven Project created in the IntelliJ workspace).
在 Main.java 文件中添加了以下代码。
Step 12 − Added the below code in the Main.java file.
Code Implementation
package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
System.out.println("Browser title: " + driver.getTitle());
}
}
在 pom.xml 文件中添加的依赖项:
Dependencies added in 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>SeleniumJava</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
</dependencies>
</project>
右键单击并选择“运行‘Main.main()’”选项。等待运行完成后。
Step 13 − Right click and select Run ‘Main.main()’ option. Wait till the run has completed.
启动了 Chrome 浏览器,我们在控制台中获取了输出,它带有时 exit code 0 结束的进程消息,表示代码已成功执行。
Step 14 − Chrome browser got launched, and we had got the output in the console- Browser Title: Google with the message Process finished with exit code 0, signifying successful execution of the code.
Chrome 浏览器启动时带有消息。
Along with that Chrome browser got launched with the message Chrome is being controlled by automated test software.
Conclusion
以下是我们在 Intellij Selenium 教程中全面总结的。我们从描述了为 Selenium 设置 IntelliJ 的前提条件开始,并逐步介绍了如何设置 IntelliJ 和 Selenium。这使你掌握了 Intellij Selenium 的深入知识。明智的做法是不断练习你学到的内容,并探索其他人与 Selenium 相关的知识,以加深你的理解并扩展你的视野。
This concludes our comprehensive take on the tutorial on Intellij Selenium. We’ve started with describing prerequisites to set up IntelliJ for Selenium, and walked through steps to set up IntelliJ along with Selenium. This equips you with in-depth knowledge of the Intellij Selenium. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.