项目介绍
用户在登录后可以发表、删除、修改自己的博客,可以查看所有的博客。
项目功能:
1)用户登录、注册,注销
2)编辑、发布、查看、修改、删除博客
测试用例
测试过程
针对主要界面和功能进行测试
界面测试
主要针对个人博客页和主页界面进行测试,检查按钮是否正常,显示的文章信息进行测试
个人博客界面:
/*
界面测试
个人博客页
*/
@Test
void myListPage() throws InterruptedException {
sleep(3000);
//按钮是否正常
String readArt=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(4)")).getText();
Assertions.assertEquals("查看全文 >>",readArt);
String updateArt=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(5)")).getText();
Assertions.assertEquals("修改 >>",updateArt);
String delArt=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(6)")).getText();
Assertions.assertEquals("删除 >>",delArt);
sleep(3000);
//文章信息
WebElement title=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title"));
Assertions.assertNotNull(title);
WebElement time=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.date"));
Assertions.assertNotNull(time);
//
WebElement content=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.desc"));
Assertions.assertNotNull(content);
}
主页界面:
/*
主页测试
*/
@Test
void listPage() throws InterruptedException {
//body > div.nav > a:nth-child(4)
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
//按钮是否正常
String readArt=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a")).getText();
Assertions.assertEquals("查看全文 >>",readArt);
//
String first=webDriver.findElement(By.cssSelector("body > div.container > div > div.blog-pagnation-wrapper > button:nth-child(1)")).getText();
Assertions.assertEquals("首页",first);
String last=webDriver.findElement(By.cssSelector("body > div.container > div > div.blog-pagnation-wrapper > button:nth-child(4)")).getText();
Assertions.assertEquals("末页",last);
String up=webDriver.findElement(By.cssSelector("body > div.container > div > div.blog-pagnation-wrapper > button:nth-child(2)")).getText();
Assertions.assertEquals("上一页",up);
String next=webDriver.findElement(By.cssSelector("body > div.container > div > div.blog-pagnation-wrapper > button:nth-child(3)")).getText();
Assertions.assertEquals("下一页",next);
//文章信息
sleep(3000);
WebElement title=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title"));
Assertions.assertNotNull(title);
WebElement time=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.date"));
Assertions.assertNotNull(time);
//
WebElement content=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.desc"));
Assertions.assertNotNull(content);
sleep(3000);
//显示页数
WebElement pageNum=webDriver.findElement(By.cssSelector("#pszie"));
Assertions.assertNotNull(pageNum);
WebElement curPage=webDriver.findElement(By.cssSelector("#pindex"));
Assertions.assertNotNull(curPage);
}
登录页测试
登录失败测试:
/*
登录失败
1.账号:zhangsan,密码:空字符串
请先输入密码!
2.账号:空字符串,密码:123
请先输入用户名!
3.账号:zhangsan,密码:456(密码和账号不匹配)
抱歉,登录失败!用户或密码错误!
4.账号:Zhangsan,密码:123(用户名和密码是否大小写敏感)
*/
@ParameterizedTest
@CsvSource({"zhangsan,''","'',123","zhangsan,4567","Zhangsan,123"})
void loginFailed(String username,String password) throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://211.159.169.174:8081/login.html");
sleep(3000);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
sleep(3000);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
sleep(3000);
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(3000);
String actual= webDriver.switchTo().alert().getText();
sleep(3000);
if(actual.equals("请先输入密码!")||actual.equals("请先输入用户名!")||actual.equals("抱歉,登录失败!用户或密码错误!")){
System.out.println("测试成功");
System.out.println(actual);
}else{
System.out.println("测试失败");
System.out.println(actual);
}
// webDriver.navigate().back();
}
测试结果:
用户名和密码不能为空,没有实现用户名和密码是否大小写敏感
登录成功测试:
@ParameterizedTest
@CsvSource({"zhangsan,123"})
void loginSucc(String username,String password) throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://211.159.169.174:8081/login.html");
//输入登录信息,并提交
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(3000);
webDriver.switchTo().alert().accept();
//判断登录成功后是否跳转到主页
String url=webDriver.getCurrentUrl();
//断言
Assertions.assertEquals("http://211.159.169.174:8081/myblog_list.html",url);
}
注册页测试
注册失败:
/*
1.账号:zhangsan,密码:空字符串,确认密码:空字符串
请先输入密码!
2.账号:空字符串,密码:123 ,确认密码:空字符串
请先输入用户名!
3.账号:zhangsan,密码:456 ,确认密码:空字符串
请先输入确认密码!
4.账号:zhangsan,密码:456 ,确认密码:123
5.账号:空字符串,密码:空字符串,确认密码:空字符串
*/
@ParameterizedTest
@CsvSource({"wanwan,'',''","'',wanwan,''","'','',''","zhangsan,123,''","zhangsan,456,123"})
void regFailed(String username,String password,String repassword) throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://211.159.169.174:8081/reg.html");
//
sleep(3000);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#password2")).sendKeys(repassword);
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(3000);
String actual= webDriver.switchTo().alert().getText();
sleep(3000);
//两次密码不一致,请先检查!
if(actual.equals("请先输入密码!")||actual.equals("请先输入用户名!")||actual.equals("请先输入确认密码!")||actual.equals("两次密码不一致,请先检查!")){
System.out.println("regFailed测试成功");
System.out.println(actual);
}else{
System.out.println("regFailed测试失败");
System.out.println(actual);
}
//alert框,提示信息,并关闭浏览器
webDriver.switchTo().alert().accept();
webDriver.quit();
}
注册成功:
注册成功后会跳转到登录界面,需要验证是否跳到了登录页面
@ParameterizedTest
@CsvSource({"momo,mmkkkk,mmkkkk"})
void regSucc(String username,String password,String repassword) throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://211.159.169.174:8081/reg.html");
//
sleep(3000);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.findElement(By.cssSelector("#password2")).sendKeys(repassword);
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(3000);
webDriver.switchTo().alert().accept();
sleep(3000);
String url=webDriver.getCurrentUrl();
Assertions.assertEquals("http://211.159.169.174:8081/login.html",url);
}
文章详情页测试
@ParameterizedTest
@MethodSource("Generator")
void artDetail(String expected_url, String expected_title, String expected_blog_title) throws InterruptedException {
String list_title=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title")).getText();
webDriver.findElement(By.xpath("//*[@id=\"artListDiv\"]/div[1]/a[1]")).click();
sleep(3000);
String cur_url = webDriver.getCurrentUrl();
sleep(3000);
String title = webDriver.getTitle();
sleep(3000);
String blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
sleep(3000);
Assertions.assertEquals(list_title, blog_title);
Assertions.assertEquals(expected_title, title);
Assertions.assertEquals(expected_blog_title, blog_title);
if (cur_url.contains(expected_url)) {
System.out.println("artDetail测试通过");
} else {
System.out.println("artDetail测试不通过");
}
}
文章编辑页测试
@Test
void artEditorSucc() throws InterruptedException {
//找到写博客按钮
webDriver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
sleep(3000);
//找到标题输入框,通过js
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试111\"");
webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
sleep(3000);
// 提示alert弹窗取消
webDriver.switchTo().alert().dismiss();
sleep(3000);
String cur_url= webDriver.getCurrentUrl();
//
Assertions.assertEquals("http://211.159.169.174:8081/myblog_list.html",cur_url);
}
文章删除测试
@ParameterizedTest
@CsvSource({"自动化测试111"})
void delArt(String title) throws InterruptedException {
//#artListDiv > div:nth-child(1) > a:nth-child(6)
webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(6)")).click();
sleep(3000);
webDriver.switchTo().alert().accept();
sleep(3000);
String actual=webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title")).getText();
// webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.date")).getText();
Assertions.assertNotEquals(title,actual);
}
主页测试
当处于首页时,点击上一页,会提示已经在首页了,无需跳转!
@Test
void firstPage() throws InterruptedException {
//主页页面
webDriver.get("http://211.159.169.174:8081/blog_list.html");
//此时处于首页,点击上一页
webDriver.findElement(By.cssSelector("body > div.container > div > div.blog-pagnation-wrapper > button:nth-child(2)")).click();
sleep(3000);
String text=webDriver.switchTo().alert().getText();
webDriver.switchTo().alert().accept();
if(text.equals("已经在首页了,无需跳转!")){
System.out.println("firstPage测试成功");
System.out.println(text);
}else {
System.out.println("firstPage测试失败");
System.out.println(text);
}
}
当处于末页时,点击下一页,会提示已经在末页了,无需跳转!
@Test
void lastPage() throws InterruptedException {
//主页页面
webDriver.get("http://211.159.169.174:8081/blog_list.html");
//
webDriver.findElement(By.cssSelector("body > div.container > div > div.blog-pagnation-wrapper > button:nth-child(4)")).click();
sleep(3000);
webDriver.findElement(By.cssSelector("body > div.container > div > div.blog-pagnation-wrapper > button:nth-child(3)")).click();
sleep(3000);
String text=webDriver.switchTo().alert().getText();
webDriver.switchTo().alert().accept();
if(text.equals("已经在末页了,无需跳转!")){
System.out.println("lastPage测试成功");
System.out.println(text);
}else {
System.out.println("lastPage测试失败");
System.out.println(text);
}
}