目录
1.报告概要
2、测试环境
3、手动测试用例编写
4、自动化测试用例
测试对象:基于SSM项目的博客系统。
测试目的:检测博客项目是否符合预期,并且对测试知识进行练习和巩固。
测试点:主要针对常用的功能进行测试如:博客项目的注册、登录、博客列表页、博客编辑页、个人列表页、导航栏等进行测试。
测试结果及结论:测试的常用功能全部正常,测试注册页面的密码时,发现密码没有限制长度以及强度的限制。
硬件:Lenovo Y7000 2020
软件:Windows 、Google Chrome、IDEA
测试工具:自动化测试工具Selenium3
浏览器版本:Google Chrome 118.0.5993.118
用户注册页
用户登录页
个人列表页
博客列表页(主页)
博客详情页
博客编辑页
博客注册页面
package Blog;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;
//根据程序员指定的顺序执行
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class RegCases extends InitAndEnd{
/*
* 注册成功
* */
@Order(1)
@ParameterizedTest
@CsvSource({"zhangsan,12345,12345"})
void regSuccess(String username,String password,String conPassword ) throws InterruptedException {
//打开主页面
webDriver.get("http://154.8.139.1:48080/login.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//找到注册按钮
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
sleep(3000);
//输入注册的用户名,密码
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#password2")).sendKeys(conPassword);
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//点击提交
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(3000);
//处理弹窗
String test = webDriver.switchTo().alert().getText();
String except = "恭喜:注册成功!";
webDriver.switchTo().alert().accept();
Assertions.assertEquals(except,test);
}
/*
*
* 用户名为空注册失败
* */
@Order(2)
@ParameterizedTest
@CsvSource(value = {"'',123,123","'',123,456","'',546,''","'','',456","'','',''","'张三','',''","'张三','123',''","'张三','123','123'",})
void RegFail(String username,String password,String conPassword) throws InterruptedException {
//打开主页
webDriver.get("http://154.8.139.1:48080/login.html");
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
sleep(3000);
//找到注册点击
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//输入用户名、密码、确认密码
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#password2")).sendKeys(conPassword);
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//点击提交
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
sleep(3000);
//处理弹窗
//获取弹窗信息
String test = webDriver.switchTo().alert().getText();
webDriver.switchTo().alert().accept();
String except = "恭喜:注册成功!";
//断言
Assertions.assertNotEquals(except,test);
}
}
博客登录页面
import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd{
/*
* 输入正确的账号、密码登录成功
* */
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "LoginSuccess.csv")
void LoginSuccess(String username,String password,String blog_list_url) throws InterruptedException {
//打开博客登录页面
webDriver.get("http://154.8.139.1:48080/login.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//输入账号admin
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//输入密码123
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(3000);
//处理弹窗
//用于接受警告对话框(确认)
webDriver.switchTo().alert().accept();
//跳转到列表页
//获取到当前的页面的url
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//判断url是否和预期相等
Assertions.assertEquals(blog_list_url,cur_url);
}
/*
* 输入错误的密码和账号
* */
@Disabled
@Order(2)
@ParameterizedTest
@CsvSource(value = {"张帆,123","lisi,123","'',123456","张帆,''","'',''"})
void LoginFail(String username,String password) throws InterruptedException {
//打开博客登录页面
webDriver.get("http://154.8.139.1:48080/login.html");
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//输入账号和密码
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(3000);
//处理弹窗
//获取弹窗内容
String test = webDriver.switchTo().alert().getText();
String except = "恭喜,登录成功!";
//用于接受警告对话框(确认)
webDriver.switchTo().alert().accept();
//断言两个字符串的内容不相等,测试通过
Assertions.assertNotEquals(except,test);
System.out.println("登录不成功");
}
个人列表页
/*
* 博客列表页
* */
@Test
void BlogList(){
//打开博客列表页
webDriver.get("http://154.8.139.1:48080/myblog_list.html");
//获取页面上所有博客标题对应的元素
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
int title_num = webDriver.findElements(By.cssSelector("title")).size();
//如果元素数量不为0,测试通过
System.out.println(title_num);
Assertions.assertNotEquals(0,title_num);
}
博客详情页
/*
* 博客详情页校验
* url
* 博客标题
* 页面title是"博客详情页"
* 用户名
* 文章数
* */
@Order(4)
@ParameterizedTest
@MethodSource("Generator")
void BlogDetail(String expect_url,String expected_title,String expected_blog_title,String except_username ) throws InterruptedException {
//找到第一篇博客对应的查看全文按钮
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
webDriver.findElement(By.xpath("//*[@id=\"artListDiv\"]/div[1]/a[1]")).click();
//获取当前页面的URL
String cur_url = webDriver.getCurrentUrl();
//获取当前页面的title
String cur_title = webDriver.getTitle();
//获取博客标题
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
String cur_blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//获取用户名
String cur_username = webDriver.findElement(By.cssSelector("#username")).getText();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//获取文章数
String cur_blog_count = webDriver.findElement(By.cssSelector("#artcount")).getText();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
Assertions.assertEquals(except_username,cur_username);
Assertions.assertNotEquals(0,cur_blog_count);
if(cur_url.contains(expect_url)){
System.out.println("测试通过");
}else{
System.out.println(cur_url);
System.out.println("测试不通过");
}
Assertions.assertEquals(expected_title,cur_title);
Assertions.assertEquals(expected_blog_title, cur_blog_title);
}
public class BlogCases extends InitAndEnd{
public static Stream Generator() {
return
Stream.of(Arguments.arguments("http://154.8.139.1:48080/blog_content.html",
"博客正文","自动化测试","张帆"));
}
博客编辑页,文章发布完成之后进入博客列表页检测发布文章的标题和时间。
/*
* 博客编辑页
* */
@Order(5)
@Test
void EditBlog() throws InterruptedException {
//打开博客列表页
//webDriver.findElement(By.cssSelector("http://154.8.139.1:48080/myblog_list.html"));
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//找到写博客按钮,点击
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//通过JS进行输入
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
sleep(3000);
//点击发布
webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
sleep(3000);
String cur_text = webDriver.switchTo().alert().getText();
webDriver.switchTo().alert().dismiss();
String except_text = "恭喜:添加成功!是否继续添加文章?";
Assertions.assertEquals(except_text,cur_text);
}
/*
* 校验已发布博客标题
* 校验已发布博客时间
* */
@Order(6)
@Test
void BlogInfoChecked(){
webDriver.get("http://154.8.139.1:48080/blog_list.html");
//获取第一篇博客标题
String first_blog_title = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title")).getText();
//获取第一篇博客发布时间
String first_blog_time= webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.date")).getText();
Assertions.assertEquals("自动化测试",first_blog_title);
if(first_blog_time.contains("2023-11-02")){
System.out.println("测试通过");
}else{
System.out.println("文章发布时间是:"+first_blog_time);
System.out.println("测试不通过");
}
}
删除功能
/*
* 测试博客列表页上删除功能
* */
@Order(7)
@Test
void DeleteCases() throws InterruptedException {
//进入个人列表页
webDriver.get("http://154.8.139.1:48080/myblog_list.html");
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//点击删除按钮
webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(6)")).click();
sleep(3000);
String text = webDriver.switchTo().alert().getText();
webDriver.switchTo().alert().accept();
String except_text = "恭喜:删除成功";
sleep(3000);
String first_blog_title = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title")).getText();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
String except_title = "自动化测试";
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
Assertions.assertEquals(except_text,text);
Assertions.assertEquals(first_blog_title,except_title);
}
注销功能
/*
* 注销功能测试
* */
@Order(8)
@Test
void Logout(){
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//获取alert()文字
String text = webDriver.switchTo().alert().getText();
String except_text = "是否确定注销?";
webDriver.switchTo().alert().accept();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//校验URL(登录)
String cur_url = webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//校验提交按钮
WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
Assertions.assertEquals(except_text,text);
Assertions.assertEquals("http://154.8.139.1:48080/login.html",cur_url);
Assertions.assertNotNull(webElement);
}
InitAndEnd类
package Blog;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class InitAndEnd {
static WebDriver webDriver;
@BeforeAll
static void SetUp(){
webDriver = new ChromeDriver();
}
@AfterAll
static void TearDown(){
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.quit();
}
}
问题总结:
❓在测试程序执行报unexpected alert open: {Alert text : 恭喜:删除成功}这种异常,那么就是你没有将这个弹窗关闭掉
✨可以使用accept()方法接受或者dismiss()拒绝