第一步、创建maven工程
扩展步骤 添加阿里镜像
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
</settings>
第二步、添加依赖
中央仓库地址:https://mvnrepository.com/
<dependencies>
<!--Java的单元测试框架 -->
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
</dependency>
<!-- 自动化测试框架依赖-->
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.7.2</version>
</dependency>
</dependencies>
第三步、编写单元测试代码
需求:打开百度页面,输入"洛阳"关键字,然后点击 “百度搜索”按钮
package cn.lanqiao;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;
import org.testng.annotations.Test;
public class Demo {
/**
* 全局变量
*/
WebDriver driver;
/**
* 驱动只注册一次(全局)
*/
@BeforeClass
public void a(){
System.setProperty("webdriver.gecko.driver","C:/lanqiao/geckodriver.exe");
}
/**
* 每次都要打开浏览器
*/
@BeforeMethod
public void befor(){
driver =new FirefoxDriver();
}
/**
* 每次都要关闭浏览器
*/
@AfterMethod
public void after(){
driver.quit();
}
/**
* 可运行的单元测试方法
*/
@Test
public void testDemo1() throws InterruptedException {
driver.get("https://www.baidu.com/");
//通过百度的输入框编号,找到百度框框
WebElement baiduInputElement = driver.findElement(By.id("kw"));
//输入洛阳
baiduInputElement.sendKeys("洛阳");
//通过按钮的编号 找到按钮
WebElement baiduBtnElement = driver.findElement(By.id("su"));
//点击
baiduBtnElement.click();
Thread.sleep(10000);
}
@Test
public void demo5() throws InterruptedException {
driver.get("https://www.lanqiao.cn/");
//通过百度的输入框编号,找到百度框框
List<WebElement> baiduInputElement = driver.findElements(By.xpath("//ul[contains(@class,'category-list')]/li/child::div[@class='content-box']/a[@title='信息安全:']"));
System.out.println(baiduInputElement.size());
baiduInputElement.get(0).click();
Thread.sleep(5000);
}
}
TestBase
package cn.lanqiao;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
/**
* 父类
*/
public class TestBase {
/**
* 静态常量
*/
final static String BAIDU_URL ="https://www.baidu.com/";
final static String LANQIO_URL ="https://www.lanqiao.cn/";
final static String LNAJIA_URL ="https://lanjia.cn/";
final static String LOCALHOST_URL ="http://127.0.0.1:5500/index.html";
/**
* 全局变量
*/
WebDriver driver;
/**
* 驱动只注册一次(全局)
*/
@BeforeClass
public void a(){
System.setProperty("webdriver.gecko.driver","C:/lanqiao/geckodriver.exe");
}
/**
* 每次都要打开浏览器
*/
@BeforeMethod
public void befor(){
driver =new FirefoxDriver();
}
/**
* 每次都要关闭浏览器
*/
@AfterMethod
public void after(){
driver.quit();
}
}