
使用selenium4 + Junit5单元测试框架,来进行简单的自动化测试。
1. 准备工作
(1)引入依赖,此时的pom.xml文件:
- "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.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>myblog-seleniumartifactId>
- <version>1.0-SNAPSHOTversion>
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>org.seleniumhq.seleniumgroupId>
- <artifactId>selenium-javaartifactId>
- <version>4.0.0version>
- dependency>
-
- <dependency>
- <groupId>commons-iogroupId>
- <artifactId>commons-ioartifactId>
- <version>2.6version>
- dependency>
- <dependency>
- <groupId>org.junit.jupitergroupId>
- <artifactId>junit-jupiterartifactId>
- <version>5.8.2version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.junit.platformgroupId>
- <artifactId>junit-platform-commonsartifactId>
- <version>1.8.2version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.junit.platformgroupId>
- <artifactId>junit-platform-reportingartifactId>
- <version>1.8.2version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.junit.platformgroupId>
- <artifactId>junit-platform-suiteartifactId>
- <version>1.8.2version>
- <scope>testscope>
- dependency>
- dependencies>
-
- project>
(2)创建公共类
创建common包,存放公共类。首先创建CommonDriver类来获取驱动。
- package com.webAutoTest.common;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.chrome.ChromeOptions;
- import java.time.Duration;
-
- /**
- * Created with IntelliJ IDEA.
- * Description: 创建驱动对象,并返回该对象
- * User: WangWZ
- * Date: 2023-09-05
- * Time: 9:48
- */
- public class CommonDriver {
- //使用单例模式创建驱动
- private static ChromeOptions options = new ChromeOptions();
-
- public static ChromeDriver driver = null;
-
- public static ChromeDriver getDriver(){
- if (driver == null) {
- options.addArguments("--remote-allow-origins=*");
- driver = new ChromeDriver(options);
- //创建驱动的时候就加上隐式等待
- //整个测试就都会隐式等待了
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- }
- return driver;
- }
-
- }
如果代码中使用到了进行截图、存储文件的操作以及使用了参数化实现数据来源时,也可以在创建公共类。方便使用。
2. 注册页面
后续使用Juit中的 Suit套件 进行执行,所以关闭驱动的方法可以单独一个类。使用@SelectClasses注解执行。
- package com.webAutoTest.tests;
-
- import com.webAutoTest.common.CommonDriver;
- import org.junit.jupiter.api.Assertions;
- import org.junit.jupiter.api.BeforeAll;
- import org.junit.jupiter.api.Test;
- import org.junit.jupiter.params.ParameterizedTest;
- import org.junit.jupiter.params.provider.CsvSource;
- import org.openqa.selenium.Alert;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
-
- import java.time.Duration;
-
- /**
- * Created with IntelliJ IDEA.
- * Description: 注册页面测试
- * User: WangWZ
- * Date: 2023-09-05
- * Time: 9:48
- */
- public class RegTest {
- //获取驱动对象
- static ChromeDriver driver = CommonDriver.getDriver();
-
- @BeforeAll
- public static void getURL() {
- driver.get("http://58.87.89.71:8009/reg.html");
- //使用隐式等待渲染页面完成
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- }
-
- /**
- * 用户名已存在
- * @param username
- * @param password1
- * @param password2
- */
- @ParameterizedTest
- @CsvSource({"王文哲", "456", "456"})
- public void RegNameExist(String username, String password1, String password2) {
- driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
- driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password1);
- driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
- Alert alert = driver.switchTo().alert();
- String str =alert.getText();
- Assertions.assertEquals("该用户名已被使用,请重新输入", str);
- alert.accept();
- }
-
- /**
- * 用户名为空
- * @param username
- * @param password1
- * @param password2
- */
- @ParameterizedTest
- @CsvSource({"", "456", "456"})
- public void RegNameNull(String username, String password1, String password2) {
- driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
- driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password1);
- driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
- Alert alert = driver.switchTo().alert();
- String str =alert.getText();
- Assertions.assertEquals("请先输入用户名", str);
- alert.accept();
- }
-
- /**
- * 密码为空
- * @param username
- * @param password1
- * @param password2
- */
- @ParameterizedTest
- @CsvSource({"王王文哲", "", ""})
- public void RegPasswordNull(String username, String password1, String password2) {
- driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
- driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password1);
- driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
- Alert alert = driver.switchTo().alert();
- String str =alert.getText();
- Assertions.assertEquals("请先输入密码", str);
- alert.accept();
- }
-
- /**
- * 确认密码和密码不一致
- * @param username
- * @param password1
- * @param password2
- */
- @ParameterizedTest
- @CsvSource({"汪汪", "456", "1456"})
- public void RegPasswordDe(String username, String password1, String password2) {
- driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
- driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password1);
- driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
- Alert alert = driver.switchTo().alert();
- String str =alert.getText();
- Assertions.assertEquals("两次密码输入的不一致,请先检查", str);
- alert.accept();
- }
-
- /**
- * 用户名或密码中存在特殊字符
- * @param username
- * @param password1
- * @param password2
- */
- @ParameterizedTest
- @CsvSource({"@w王1", "45@6", "45@6"})
- public void RegExistSpecial(String username, String password1, String password2) {
- driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
- driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password1);
- driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
- Alert alert = driver.switchTo().alert();
- String str =alert.getText();
- Assertions.assertEquals("恭喜,注册成功", str);
- alert.accept();
- }
-
- /**
- * 注册标题
- */
- @Test
- public void RegTitle() {
- String str = driver.findElement(By.xpath("/html/body/div[2]/div/h3")).getText();
- Assertions.assertEquals("注册",str);
- }
-
- /**
- * 输入框显示
- */
- @Test
- public void RegNameInput(){
- WebElement element = driver.findElement(By.xpath("//*[@id=\"username\"]"));
- Assertions.assertNotNull(element);
- }
- /**
- * 输入框文字
- */
- @Test
- public void RegUName() {
- String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[1]/span")).getText();
- Assertions.assertEquals("用户名",str);
- }
-
- /**
- * 密码框显示
- */
- @Test
- public void RegPasswordInput(){
- WebElement element = driver.findElement(By.xpath("//*[@id=\"password\"]"));
- Assertions.assertNotNull(element);
- }
- /**
- * 密码框文字
- */
- @Test
- public void RegPassword() {
- String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/span")).getText();
- Assertions.assertEquals("密码",str);
- }
-
- /**
- * 确认密码框显示
- */
- @Test
- public void RegPassword2Input(){
- WebElement element = driver.findElement(By.xpath("//*[@id=\"password2\"]"));
- Assertions.assertNotNull(element);
- }
- /**
- * 确认密码框文字
- */
- @Test
- public void RegPassword2() {
- String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[3]/span")).getText();
- Assertions.assertEquals("确认密码",str);
- }
-
- /**
- * 验证码图片显示
- */
- @Test
- public void RegPhoto(){
- WebElement element = driver.findElement(By.xpath("//*[@id=\"codeimg\"]"));
- Assertions.assertNotNull(element);
- }
- /**
- * 验证码文字
- */
- @Test
- public void RegDraft() {
- String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[4]/span")).getText();
- Assertions.assertEquals("验证码",str);
- }
-
-
- }
3. 登录页面
- package com.webAutoTest.tests;
-
- import com.webAutoTest.common.CommonDriver;
- import org.junit.jupiter.api.Assertions;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.junit.jupiter.params.ParameterizedTest;
- import org.junit.jupiter.params.provider.CsvSource;
- import org.openqa.selenium.Alert;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
-
- import java.time.Duration;
-
- /**
- * Created with IntelliJ IDEA.
- * Description: 登录页面
- * User: WangWZ
- * Date: 2023-09-05
- * Time: 9:49
- */
- public class LoginTest {
- ChromeDriver driver = CommonDriver.getDriver();
-
- @BeforeEach
- public void getUrl(){
- driver.get("http://58.87.89.71:8009/login.html");
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- }
-
- /**
- * 正确的用户名和密码
- */
- @ParameterizedTest
- @CsvSource({"王文哲","123"})
- public void LoginTrue(String username, String password) {
- driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
- driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
- driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
- String str = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/myblog_list.html",str);
- driver.navigate();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- }
-
- /**
- * 错误的用户名和密码
- */
- @ParameterizedTest
- @CsvSource({"王文哲","1234"})
- public void LoginFalse(String username, String password) {
- driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
- driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
- driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert = driver.switchTo().alert();
- String str = alert.getText();
- Assertions.assertEquals("抱歉登录失败,用户名或密码输入错误,请重试!",str);
- alert.accept();
- }
-
- /**
- * 登录密码为空
- */
- @ParameterizedTest
- @CsvSource({"王文哲",""})
- public void LoginPasswordNull(String username, String password) {
- driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
- driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
- driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert = driver.switchTo().alert();
- String str = alert.getText();
- Assertions.assertEquals("请先输入密码!",str);
- alert.accept();
- }
-
- /**
- * 用户名为空
- */
- @ParameterizedTest
- @CsvSource({"","123"})
- public void LoginNameNull(String username, String password) {
- driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
- driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
- driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert = driver.switchTo().alert();
- String str = alert.getText();
- Assertions.assertEquals("请先输入用户名!",str);
- alert.accept();
- }
-
- /**
- * 密码或用户名有特殊字符
- */
- @ParameterizedTest
- @CsvSource({"@w王1", "45@6"})
- public void Login(String username, String password) {
- driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
- driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
- driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- String str = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/myblog_list.html",str);
- driver.navigate();
- }
-
- /**
- * 注册按钮功能
- */
- @Test
- public void LoginToReg() {
- driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- String str = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/reg.html",str);
- driver.navigate();
- }
-
- /**
- * 登录标题
- */
- @Test
- public void LoginTitle() {
- String str = driver.findElement(By.xpath("/html/body/div[2]/div/h3")).getText();
- Assertions.assertEquals("登录",str);
- }
-
- /**
- * 输入框显示
- */
- @Test
- public void LoginNameInput(){
- WebElement element = driver.findElement(By.xpath("//*[@id=\"username\"]"));
- Assertions.assertNotNull(element);
- }
- /**
- * 输入框文字
- */
- @Test
- public void LoginUName() {
- String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[1]/span")).getText();
- Assertions.assertEquals("用户名",str);
- }
-
- /**
- * 密码框显示
- */
- @Test
- public void LoginPasswordInput(){
- WebElement element = driver.findElement(By.xpath("//*[@id=\"password\"]"));
- Assertions.assertNotNull(element);
- }
- /**
- * 密码框文字
- */
- @Test
- public void LoginPassword() {
- String str = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/span")).getText();
- Assertions.assertEquals("密码",str);
- }
-
- /**
- * 登录按钮
- */
- @Test
- public void LoginSub() {
- String str = driver.findElement(By.xpath("//*[@id=\"submit\"]")).getText();
- Assertions.assertEquals("提交",str);
- }
-
-
-
-
-
- }
4. 列表页面
- package com.webAutoTest.tests;
-
- import com.webAutoTest.common.CommonDriver;
- import org.junit.jupiter.api.Assertions;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.openqa.selenium.Alert;
- import org.openqa.selenium.By;
- import org.openqa.selenium.chrome.ChromeDriver;
-
- import java.time.Duration;
-
- /**
- * Created with IntelliJ IDEA.
- * Description: 列表页面
- * User: WangWZ
- * Date: 2023-09-05
- * Time: 9:49
- */
- public class ListTest {
- ChromeDriver driver = CommonDriver.getDriver();
-
- @BeforeEach
- public void getUrl(){
- driver.get("http://58.87.89.71:8009/login.html");
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- }
-
- /**
- * 主页按钮
- */
- @Test
- public void ListToL() {
- driver.findElement(By.xpath("/html/body/div[1]/a[1]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- String str = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/login.html",str);
- driver.navigate();
- }
-
- /**
- * 写博客按钮
- */
- @Test
- public void ListToEdit() {
- driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- String str = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/blog_add.html",str);
- driver.navigate();
- }
-
- /**
- * 我的主页按钮
- */
- @Test
- public void ListToMyL() {
- driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- String str = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/myblog_list.html",str);
- driver.navigate();
- }
-
- /**
- * 分页功能,第一页
- */
- @Test
- public void ListByPage() {
- driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/button[2]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert = driver.switchTo().alert();
- String str = alert.getText();
- Assertions.assertEquals("当前已经在首页了",str);
- alert.accept();
- }
-
-
-
- }
5. 写博客页面
- package com.webAutoTest.tests;
-
- import com.webAutoTest.common.CommonDriver;
- import org.junit.jupiter.api.Assertions;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.params.ParameterizedTest;
- import org.junit.jupiter.params.provider.CsvSource;
- import org.openqa.selenium.Alert;
- import org.openqa.selenium.By;
- import org.openqa.selenium.chrome.ChromeDriver;
-
- import java.time.Duration;
-
- /**
- * Created with IntelliJ IDEA.
- * Description:写博客页面
- * User: WangWZ
- * Date: 2023-09-05
- * Time: 9:50
- */
- public class EditTest {
- ChromeDriver driver = CommonDriver.getDriver();
-
- @BeforeEach
- public void getUrl(){
- driver.get("http://58.87.89.71:8009/blog_add.html");
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- }
-
- /**
- * 标题为空
- */
- @ParameterizedTest
- @CsvSource({"","111111"})
- public void EditTitleNull(String title,String content) {
- driver.findElement(By.xpath("//*[@id=\"title\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys(title);
- driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).clear();
- driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).sendKeys(content);
- driver.findElement(By.xpath("/html/body/div[2]/div[1]/button[1]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert = driver.switchTo().alert();
- alert.accept();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert2 = driver.switchTo().alert();
- String str2 =alert.getText();
- Assertions.assertEquals("请先输入标题!", str2);
- alert.accept();
- }
-
- /**
- * 内容为空
- */
- @ParameterizedTest
- @CsvSource({"222",""})
- public void EditContentNull(String title,String content) {
- driver.findElement(By.xpath("//*[@id=\"title\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys(title);
- driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).clear();
- driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).sendKeys(content);
- driver.findElement(By.xpath("/html/body/div[2]/div[1]/button[1]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert = driver.switchTo().alert();
- alert.accept();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert2 = driver.switchTo().alert();
- String str2 =alert.getText();
- Assertions.assertEquals("请先输入文章内容!", str2);
- alert.accept();
- }
-
- /**
- * 发布文章
- */
- @ParameterizedTest
- @CsvSource({"Java精选","数据类型"})
- public void EditSub(String title,String content) {
- driver.findElement(By.xpath("//*[@id=\"title\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys(title);
- driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).clear();
- driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).sendKeys(content);
- driver.findElement(By.xpath("/html/body/div[2]/div[1]/button[1]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert = driver.switchTo().alert();
- alert.accept();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert2 = driver.switchTo().alert();
- String str2 =alert.getText();
- Assertions.assertEquals("恭喜:文章添加成功!是否继续添加文章?", str2);
- alert.dismiss();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- String url = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/myblog_list.html", url);
- driver.navigate();
- }
-
- /**
- * 存为草稿
- */
- @ParameterizedTest
- @CsvSource({"Java精选2","数据类型2"})
- public void EditSubDraft(String title,String content) {
- driver.findElement(By.xpath("//*[@id=\"title\"]")).clear();
- driver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys(title);
- driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).clear();
- driver.findElement(By.xpath("//*[@id=\"editorDiv\"]/div[1]/div[6]")).sendKeys(content);
- driver.findElement(By.xpath("/html/body/div[2]/div[1]/button[2]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- Alert alert = driver.switchTo().alert();
- String str =alert.getText();
- Assertions.assertEquals("恭喜:保存草稿成功!", str);
- alert.accept();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
-
- String url = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/mydraftblog_list.html", url);
- driver.navigate();
- }
-
- }
6. 草稿箱页面
- package com.webAutoTest.tests;
-
- import com.webAutoTest.common.CommonDriver;
- import org.junit.jupiter.api.Assertions;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.openqa.selenium.Alert;
- import org.openqa.selenium.By;
- import org.openqa.selenium.chrome.ChromeDriver;
-
- import java.time.Duration;
-
- /**
- * Created with IntelliJ IDEA.
- * Description: 草稿箱页面
- * User: WangWZ
- * Date: 2023-09-05
- * Time: 9:49
- */
- public class DraftTest {
- ChromeDriver driver = CommonDriver.getDriver();
-
- @BeforeEach
- public void getUrl(){
- driver.get("http://58.87.89.71:8009/login.html");
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- }
-
- /**
- * 主页按钮
- */
- @Test
- public void ListToL() {
- driver.findElement(By.xpath("/html/body/div[1]/a[1]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- String str = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/login.html",str);
- driver.navigate();
- }
-
- /**
- * 写博客按钮
- */
- @Test
- public void ListToEdit() {
- driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- String str = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/blog_add.html",str);
- driver.navigate();
- }
-
- /**
- * 我的主页按钮
- */
- @Test
- public void ListToMyL() {
- driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
- driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
- String str = driver.getCurrentUrl();
- Assertions.assertEquals("http://58.87.89.71:8009/myblog_list.html",str);
- driver.navigate();
- }
-
-
- }
7. 文章详情页面
- package com.webAutoTest.tests;
-
- import com.webAutoTest.common.CommonDriver;
- import org.junit.jupiter.api.Assertions;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.openqa.selenium.By;
- import org.openqa.selenium.chrome.ChromeDriver;
-
-
- /**
- * Created with IntelliJ IDEA.
- * Description:
- * User: WangWZ
- * Date: 2023-09-05
- * Time: 9:49
- */
- public class DetailTest {
- static ChromeDriver driver = CommonDriver.getDriver();
- @BeforeEach
- public void getUrl(){
- driver.get("http://58.87.89.71:8009/mydraftblog_list.html");
-
- }
- /*
- * 文章文字是否正确
- * */
- @Test
- public void testWz(){
- String text = driver.findElement(By.cssSelector("/html/body/div[2]/div[1]/div/div[1]/span[1]")).getText();
- Assertions.assertEquals("文章",text);
- }
- /*
- * 分类文字是否正确
- * */
- @Test
- public void testFl(){
- String text = driver.findElement(By.cssSelector("/html/body/div[2]/div[1]/div/div[1]/span[2]")).getText();
- Assertions.assertEquals("分类",text);
- }
-
- }
具体Junit注解:Junit 单元测试框架(简单使用)
- package com.webAutoTest.tests;
-
- import org.junit.platform.suite.api.SelectClasses;
- import org.junit.platform.suite.api.Suite;
-
- /**
- * Created with IntelliJ IDEA.
- * Description:
- * User: WangWZ
- * Date: 2023-09-05
- * Time: 16:54
- */
- @Suite
- @SelectClasses({LoginTest.class,RegTest.class,LoginTest.class,ListTest.class,EditTest.class,DetailTest.class,DriverQuitTest.class})
- public class RunSuit {
- }