博客系统代码:spring-blog · taotao/Studying JavaEE Advanced - 码云 - 开源中国 (gitee.com)
自动化脚本代码:BlogAutoTest · taotao/Studying JavaEE Advanced - 码云 - 开源中国 (gitee.com)
上线项目地址:博客登陆页
目录
一、博客系统项目的介绍
二、编写Web测试用例
三、自动化测试脚本开发
1、引入依赖
2、设计框架
3、编写代码
(1)LoginPage
(2)ListPage
(3)DetailPage
(4)EditPage
(5)RunTest类,包含main方法,用来运行上述接口
四、测试报告
一、博客系统项目的介绍
在学习技术的过程中,总是学一点忘一点,如果把笔记记录一个网站上,自己在学习技术的时候就能访问该网站,进行回顾、复习,回顾大学学习技术这里,我发现不管当时记忆多么深刻,到一定时间后,都会网段一干二净。所以我打算写一个Web项目,记录学习笔记,像CSDN这样的博客网站,用于复习学过的知识,同时也能锻炼自己的业务代码能力。
该项目主要有以下几个接口:登录、博客列表页、博客详情页、博客编辑页、个人信息栏、注销接口
登录:
博客列表:
博客详情页:
博客编辑页:
个人信息栏、注销接口:
接下来,就根据这几个接口,进行设计自动化测试。
二、编写Web测试用例
如图:
三、自动化测试脚本开发
1、引入依赖
//Selenium驱动
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
//webdrivermanager
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.8.0</version>
</dependency>
//屏幕截图
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
2、设计框架
common文件夹:存放创建的驱动对象、等待、屏幕截图
tests文件夹:存放测试用例
image文件夹:存放出现异常的屏幕截图
包的路径如下:
3、编写代码
前言:编写Java代码如果要进行断言的话,要进行设置一下,如图:
输入:-ea -Dfile.encoding=UTF-8,保存退出。
(1)LoginPage
import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
import java.time.Duration;
public class LoginPage extends Utils {
public static String url = "http://120.79.61.184:8080/blog_login.html";
public LoginPage() {
super(url);
}
//检查登录页面
public void checkLoginPage() {
//检查logo图片元素
driver.findElement(By.cssSelector("body > div.nav > img"));
//检查“博客系统”字样
driver.findElement(By.cssSelector("body > div.nav > span"));
//检查是否有输入框、提交按钮
driver.findElement(By.cssSelector("#username"));//用户名框
driver.findElement(By.cssSelector("#password"));//密码框
driver.findElement(By.cssSelector("#submit"));//提交按钮
}
//1、正常登录
public void RightLogin() {
String[] useName = {"zhangsan", "lisi", "admin"};
String password = "123456";
//进行正确登录
for(String x : useName) {
driver.findElement(By.cssSelector("#username")).sendKeys(x);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
//登录成功后注销
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
}
}
//2、异常登录
public void FalseLogin() throws InterruptedException, IOException {
//1、账号/密码可能为空
isEmpty();
//2、账号/密码不为空
notEmpty();
}
private void isEmpty() {
/**
* 1、只有账户为空
*/
driver.findElement(By.cssSelector("#password")).sendKeys("123456");//正确密码
driver.findElement(By.cssSelector("#submit")).click();
// 显式等待,等待Alert出现
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
wait.until(ExpectedConditions.alertIsPresent()); // 等待Alert出现
//出现弹窗,点击确定
Alert alert = driver.switchTo().alert();
alert.accept();//点击确定
//清空输入框的内容
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#password")).sendKeys("123");//错误密码
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());
//出现弹窗,点击确定
alert = driver.switchTo().alert();
alert.accept();//点击确定
//清空输入框的内容
driver.findElement(By.cssSelector("#password")).clear();
/**
* 2、只有密码为空
*/
driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");//正确账户
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());
//出现弹窗,点击确定
alert = driver.switchTo().alert();
alert.accept();
//清除文本信息
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys("bite");//错误账户
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());//显示等待弹窗
//点击确定
alert = driver.switchTo().alert();
alert.accept();
/**
* 3、账户、密码都为空
*/
//清除文本信息
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());//显示等待弹窗
//点击确定
alert = driver.switchTo().alert();
alert.accept();
driver.navigate().refresh();
}
private void notEmpty() {
/**
* 1、输入正确账户
*/
driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
//显式等待
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.alertIsPresent());
//出现错误密码弹窗
Alert alert = driver.switchTo().alert();
alert.accept();
driver.navigate().refresh();//刷新
/**
* 输入错误账户
*
*/
//输入正确密码
driver.findElement(By.cssSelector("#username")).sendKeys("bite");
driver.findElement(By.cssSelector("#password")).sendKeys("123456");
driver.findElement(By.cssSelector("#submit")).click();
//出现弹窗
wait.until(ExpectedConditions.alertIsPresent());//显式等待
alert = driver.switchTo().alert();
alert.accept();
//刷新
driver.navigate().refresh();
//输入错误密码
driver.findElement(By.cssSelector("#username")).sendKeys("bite");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
//出现弹窗
wait.until(ExpectedConditions.alertIsPresent());//显式等待
alert = driver.switchTo().alert();
alert.accept();//点击确定
driver.navigate().refresh();//刷新页面
}
}
(2)ListPage
public class ListPage extends Utils {
public static String url = "http://120.79.61.184:8080/blog_list.html";
public ListPage() {
super(url);
}
/**
* 列表页
*/
public void checkList() throws InterruptedException {
//未登录状态下
notLogin();
//登录状态下
rightLogin();
//个人信息栏
checkUserInfo();
//注销接口
checkQuit();
}
private void notLogin() throws InterruptedException {
//等待弹窗加载出来再点击确定
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
//是否返回到了登录页面
String title = driver.getTitle();
//断言一下,判断是否未博客登录页
assert title.equals("博客登陆页");
}
private void rightLogin() throws InterruptedException {
//先登录,进入登录状态的列表页
driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
driver.findElement(By.cssSelector("#password")).sendKeys("123456");
driver.findElement(By.cssSelector("#submit")).click();
//1、检查是否有博客系统和log图标
driver.findElement(By.cssSelector("body > div.nav > span"));
driver.findElement(By.cssSelector("body > div.nav > img"));
//2、检查是否有第一个博客标题和查看全文按钮
driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title"));
driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a"));
//3、检查是否有主页按钮、写博客按钮
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)"));
}
private void checkUserInfo() {
//检查是否有个人信息(头像和用户名),GitHub地址
driver.findElement(By.cssSelector("body > div.container > div.left > div > img"));
driver.findElement(By.cssSelector("body > div.container > div.left > div > h3"));
String url = driver.findElement(By.cssSelector("body > div.container > div.left > div > a")).getAttribute("href");
assert url.equals("https://gitee.com/cool_tao6");
}
private void checkQuit() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
driver.findElement(By.cssSelector("body > div.container-login > div > h3"));
}
}
(3)DetailPage
public class DetailPage extends Utils {
public static String url = "http://120.79.61.184:8080/blog_detail.html";
public DetailPage() {
super(url);
}
public void checkDetailPage() throws InterruptedException {
//用户未登录
notLogin();
//用户登录
rightLogin();
}
private void rightLogin() throws InterruptedException {
//先登录,进入登录状态的列表页
driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
driver.findElement(By.cssSelector("#password")).sendKeys("123456");
driver.findElement(By.cssSelector("#submit")).click();
//点击查看全文按钮
driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
//1、检查是否有“博客系统” 和 log图标
driver.findElement(By.cssSelector("body > div.nav > span"));
driver.findElement(By.cssSelector("body > div.nav > img"));
//2、个人信息栏,检查是否有作者信息(头像和用户名),GitHub地址是否可以点击,以及跳转到该连接
driver.findElement(By.cssSelector("body > div.container > div.left > div > img"));
driver.findElement(By.cssSelector("body > div.container > div.left > div > h3"));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
Thread.sleep(500);
String url = driver.findElement(By.cssSelector("body > div.container > div.left > div > a")).getAttribute("href");
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("body > div.container > div.left > div > a")));
assert url.equals("https://gitee.com/cool_tao6");
//3、检查是否有博客标题、博客内容、博客发布时间
driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title"));
driver.findElement(By.cssSelector("#detail > p"));
driver.findElement(By.cssSelector("body > div.container > div.right > div > div.date"));
//4、检查是否有主页按钮、博客按钮、注销按钮
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)"));
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
//5、是当前登录用户,有编辑按钮,不是就没有
driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)"));
//注销
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
}
private void notLogin() {
//等待弹窗加载出来再点击确定
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
//检查当前是否是登录页面
driver.findElement(By.cssSelector("body > div.container-login > div"));
}
}
(4)EditPage
public class EditPage extends Utils {
public static String url = "http://120.79.61.184:8080/blog_edit.html";
public EditPage() {
super(url);
}
public void checkEditPage() throws InterruptedException {
//1、点击博客详情页跳转到编辑页面
detailToEdit();
//2、点击写博客跳转到编辑页面
writeToEdit();
}
private void detailToEdit() throws InterruptedException {
/**
* 1、未登录状态下
*/
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.alertIsPresent());
//点击弹窗的确定
Alert alert = driver.switchTo().alert();
alert.accept();
//跳转到登录页面,检查标题是否是博客登录页
String title = driver.getTitle();
//断言一下,判断是否未博客登录页
assert title.equals("博客登陆页");
/**
* 2、先登录
*/
driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
driver.findElement(By.cssSelector("#password")).sendKeys("123456");
driver.findElement(By.cssSelector("#submit")).click();
//点击列表页中第一篇文章的查看全文,然后再点击编辑
driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)")).click();
//1、检查是否有“博客系统” 和 log图标
driver.findElement(By.cssSelector("body > div.nav > span"));
driver.findElement(By.cssSelector("body > div.nav > img"));
//2、检查是否有主页按钮、博客按钮
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)"));
//3、输入框是否有内容,是否有发布文章按钮
driver.findElement(By.cssSelector("#submit"));
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#editor > div.editormd-preview > div > p")));
String text = driver.findElement(By.cssSelector("#editor > div.editormd-preview > div > p")).getText();
assert !text.isEmpty();
//4、在标题中输入自动化测试01(先清空原本的内容),点击更新文章
driver.findElement(By.cssSelector("#title")).clear();
driver.findElement(By.cssSelector("#title")).sendKeys("自动化测试01");
driver.findElement(By.cssSelector("#submit")).click();
//返回主页
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
}
private void writeToEdit() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[1]/a[2]")));
Thread.sleep(30);
//点击写博客
driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
//1、检查是否有博客系统和log图标
driver.findElement(By.cssSelector("body > div.nav > span"));
driver.findElement(By.cssSelector("body > div.nav > img"));
//2、检查是否有主页按钮、博客按钮
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)"));
//3、是否有以下输入框,以及发布文章按钮
driver.findElement(By.cssSelector("#title"));
driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span"));
driver.findElement(By.cssSelector("#submit"));
//4、在标题中输入自动化测试,点击更新文章
driver.findElement(By.cssSelector("#title")).sendKeys("自动化测试02");
driver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(100);
//点击注销返回登录页面再刷新一下
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[1]/a[3]")));
driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
driver.navigate().refresh();
//退出驱动
driver.quit();
}
}
(5)RunTest类,包含main方法,用来运行上述接口
public class RunTest {
public static void main(String[] args) throws InterruptedException, IOException {
/**
* 登录页面
*/
LoginPage login = new LoginPage();
//检查登录页面
login.checkLoginPage();
//异常登录
login.FalseLogin();
//正常登录
login.RightLogin();
/**
* 列表页面
*/
ListPage listPage = new ListPage();
listPage.checkList();
/**
* 详情页面
*/
DetailPage detailPage = new DetailPage();
detailPage.checkDetailPage();
/**
* 编辑页面
*/
EditPage editPage = new EditPage();
editPage.checkEditPage();
}
}
运行一下程序:
没有报错情况。