目录
一、软件准备:
-
IntelliJ IDEA:IntelliJ IDEA – the Leading Java and Kotlin IDE
-
Maven repository: Maven Repository: Search/Browse/Explore (mvnrepository.com)
-
Chromedriver:http://chromedriver.storage.googleapis.com/index.html
二、安装步骤:
-
IntelliJ IDEA下载
-
点击download
-
选择community版下载
-
安装
-
-
下载安装JDK
-
点击product
-
点击Java
-
点击download
-
选择jdk版本,最新版本选择Java downloads,历史版本选择Java archive
-
常用的jdk1.8版本
-
选择系统版本
-
配置jdk环境变量
-
验证Java环境是否安装成功
-
java -version:查看JDK版
- java: 运行Java程序
- javac: javac是Java语言编程编译器。并将java源代码编译成字节代码的.class文件
-
-
Maven下载安装
-
下载
-
安装配置环境变量
-
验证maven是否安装成功
-
打开cmd,输入mvn -v
-
-
-
打开IDEA,创建Maven项目
-
创建项目
-
创建好后会有一个Maven包管理文件:pom.xml
-
配置依赖,进入Maven repository搜索并复制到pom.xml
-
selenium:
-
搜索selenium
-
选择版本
-
复制到idea
-
-
Cucumber
-
搜索cucumber
-
选择版本
-
复制到idea的pom.xml文件
-
下载cucumber插件
-
-
TestNG
-
搜索testng
-
选择版本
-
复制到idea的pom.xml文件
-
-
Allure
-
搜索Allure
-
选择版本
-
复制到idea的pom.xml文件
-
-
完成
<dependencies> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.10.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>7.12.0</version> </dependency> <!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng --> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-testng</artifactId> <version>2.22.2</version> </dependency> </dependencies>
-
-
Chromedriver
-
查看Chrome version
-
下载对应版本的Chromedriver
-
-
三、打开百度搜索实例:
-
创建java文件
-
把Chromedriver放进src目录
-
测试是否运行成功
package setest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Openbaidu {
public WebDriver driver;
@Test
public void openBaidu() throws InterruptedException {
Path pd = Paths.get("src","drivers","chromedriver.exe");
System.setProperty("webdriver.chrome.driver",pd.toAbsolutePath().toString());
// 1.创建浏览器实例
driver = new ChromeDriver();
// 2.打开百度首页
driver.get("https://www.baidu.com");
// 3.搜索selenium
driver.findElement(By.xpath("//input[@id='kw']")).sendKeys("selenium");
Thread.sleep(3000);
// 4.退出浏览器
driver.quit();
}
}