1、个人博客测试用例
主要方面包括功能、界面、兼容性、性能、安全合法、易用性、网络、中断
2、重复代码
为减少冗余代码建的类,包含登录与退出网页
/**
* 这个class中只做前置打开网页,退出浏览
* 不写任何的测试用例
*/
public class InitAndEndDriver {
static WebDriver webDriver = new ChromeDriver();
@BeforeAll
static void OpenBlog() throws InterruptedException {
// 打开博客系统
webDriver.get("http://localhost:8080/login.html");
webDriver.findElement(By.cssSelector("#username")).sendKeys("fbh");
// 输入密码
webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
// 点击提交按钮
webDriver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
sleep(5000);
}
@AfterAll
static void CloseBrowser() {
webDriver.quit();
}
}
3、登录功能测试
系统的登录页面
测试用例代码
/**
* 登录测试
*/
@Order(1)
@Test
void Login() throws InterruptedException {
// 输入账号
webDriver.findElement(By.cssSelector("#username")).sendKeys("fbh");
// 输入密码
webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
// 点击提交按钮
webDriver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
sleep(5000);
// 找注销按钮
// 如果注销按钮存在,测试通过,否则测试不通过
WebElement logout_button = webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
Assertions.assertNotNull(logout_button);//断言:判断logout_button是否不为空
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
sleep(2000);
//获取弹出框对象
Alert alert = webDriver.switchTo().alert();
//接受对话弹出框
alert.accept();
sleep(5000);
}
4、注册功能测试
注册页面
测试用例代码
@Order(0)
@Test
void register() throws InterruptedException {
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
sleep(2000);
// 找到注册按钮,点击
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
// 输入账号
webDriver.findElement(By.cssSelector("#username")).sendKeys("ffbbbf");
// 输入密码
webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
// 确认密码
webDriver.findElement(By.cssSelector("#password2")).sendKeys("123456");
sleep(2000);
// 点击提交按钮
webDriver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
sleep(1000);
//获取弹出框对象
Alert alert = webDriver.switchTo().alert();
alert.accept();
sleep(2000);
//跳转到登录页面
// 输入账号
webDriver.findElement(By.cssSelector("#username")).sendKeys("ffbbbf");
// 输入密码
webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
sleep(1000);
// 点击提交按钮
webDriver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
sleep(2000);
// 找注销按钮
// 如果注销按钮存在,测试通过,否则测试不通过
WebElement logout_button = webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
Assertions.assertNotNull(logout_button);//断言:判断logout_button是否不为空
//注销
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
sleep(2000);
//接受对话弹出框
alert.accept();
sleep(5000);
}
5、查看博客功能测试
博客列表页面
测试用例代码
/**
* 查看博客测试
* 点击查看全文,成功跳转到博客详情页面
*/
@ParameterizedTest
@ValueSource(strings = {"http://localhost:8080/blog_content.html?id="})//参数化
@Order(2)
void ReviewBlog(String expected_url) throws InterruptedException {//expected_url就是strings的名字
// webDriver.findElement(By.cssSelector("#username")).sendKeys("fbh");
// // 输入密码
// webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
// // 点击提交按钮
// webDriver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
// sleep(5000);
// 找到所有的查看全按钮
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
List<WebElement> review_all_blog = webDriver.findElements(By.xpath("//a[text()=\"查看全文 >>\"]"));
// 点击第一个查看全文按钮123
review_all_blog.get(1).click();
// 检查是否跳转到博客详情页面
// 1、获取当前页面url(博客详情页)
// 2、如果跳转到博客详情页面,测试通过(当前页面url包含“http://localhost:8080/blog_content.html?id=”)
// 3、否则测试不通过
String blog_info_url = webDriver.getCurrentUrl();
sleep(5000);
if(blog_info_url.contains(expected_url)) {
System.out.println("测试通过");
} else {
System.out.println("测试不通过");
}
}
6、编写博客功能测试
编写博客页面
测试用例代码
/**
* 编写博客测试
* 填写博客标题,点击确认发布按钮,发布成功,是否继续添加选择取消,跳转到博客列表页面
*/
//博客列表地址
public static Stream<Arguments> GeneratorUrl() {
return Stream.of(Arguments.arguments("http://localhost:8080/myblog_list.html"));
}
//编辑博客地址
public static Stream<Arguments> GeneratoreditUrl() {
return Stream.of(Arguments.arguments("http://localhost:8080/blog_add.html"));
}
@Order(4)
@ParameterizedTest
@MethodSource(value = "GeneratorUrl")//
void EditBlog(String expected_url) throws InterruptedException {
// 点击编写博客按钮
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
sleep(3000);
// 输入标题
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试博客标标题22222222\"");
// 点击发布按钮
webDriver.findElement(By.xpath("/html/body/div[2]/div[1]/button")).click();
// 获取当前页面url,如果当前页面url=http://localhost:8080/myblog_list.html,此时测试通过,否则,测试不通过
//获取弹出框对象
Alert alert = webDriver.switchTo().alert();
//接受对话弹出框
alert.accept();
sleep(3000);
alert.dismiss();
sleep(3000);
String act_url = webDriver.getCurrentUrl();
Assertions.assertEquals(expected_url, act_url);//断言
}
/**
* 编写博客测试2
* 填写博客标题,点击确认发布按钮,发布成功,是否继续添加选择取消,跳转到博客列表页面
*/
@Order(4)
@ParameterizedTest
@MethodSource(value = "GeneratorUrl")//
void EditBlog(String expected_url) throws InterruptedException {
// 点击编写博客按钮
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
sleep(3000);
// 输入标题
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试博客标标题22222222\"");
// 点击发布按钮
webDriver.findElement(By.xpath("/html/body/div[2]/div[1]/button")).click();
// 获取当前页面url,如果当前页面url=http://localhost:8080/myblog_list.html,此时测试通过,否则,测试不通过
//获取弹出框对象
Alert alert = webDriver.switchTo().alert();
//接受对话弹出框
alert.accept();
sleep(3000);
alert.dismiss();
sleep(3000);
String act_url = webDriver.getCurrentUrl();
Assertions.assertEquals(expected_url, act_url);//断言
}
/**
* 查找刚刚发布博客
* 第一条博客标题展示的是刚刚发布成功的博客标题
*/
@Order(6)
@Test
void CheckBlogTitle() {
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Assertions.assertNotNull( webDriver.findElement(By.xpath("//div[text()=\"自动化测试博客标标题1212\"]")));
}
7、删除博客功能测试
删除博客按钮页面
测试用例代码
/**
* 删除博客
* 删除博客,删除成功
*/
@Order(7)
@Test
void DeleteBlog() throws IOException, InterruptedException {
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(11) > a:nth-child(6)")).click();
sleep(3000);
//获取弹出框对象
Alert alert = webDriver.switchTo().alert();
//接受对话弹出框
alert.accept();
sleep(3000);
//接受对话弹出框
alert.accept();
sleep(3000);
// //页面滑轮到最低端
// ((JavascriptExecutor)webDriver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
// sleep(3000);
// 截图 先强制类型转换再调用截图的API
File screen_shot = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
//拷贝到磁盘
FileUtils.copyFile(screen_shot, new File("D:\\删除博客成功.png"));
}
8、修改博客功能测试
修改博客页面
测试用例代码
/**
* 修改博客测试
* 填写博客标题,点击确认发布按钮,发布成功,是否继续添加选择取消,跳转到博客列表页面
*/
@Order(8)
@ParameterizedTest
@MethodSource(value = "GeneratorUrl")//
void ModifyBlog(String expected_url) throws InterruptedException {
// 点击修改博客按钮
webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > a:nth-child(5)")).click();
sleep(3000);
// 修改标题
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"修改后的自动化测试博客标标题3363636363\"");
// 点击修改文章按钮
webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
// 获取当前页面url,如果当前页面url=http://localhost:8080/myblog_list.html,此时测试通过,否则,测试不通过
//获取弹出框对象
sleep(3000);
Alert alert = webDriver.switchTo().alert();
//接受对话弹出框
alert.accept();
sleep(3000);
String act_url = webDriver.getCurrentUrl();
Assertions.assertEquals(expected_url, act_url);//断言
}
9、用户注销功能测试
测试用例代码
/**
* 退出注销
*/
@Order(9)
@Test
void Logout() throws IOException, InterruptedException {
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// 找到注销按钮,点击
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
//获取弹出框对象
Alert alert = webDriver.switchTo().alert();
//接受对话弹出框
alert.accept();
sleep(3000);
// 截图
File screen_shot = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screen_shot, new File("D:\\退出成功截图.png"));
}
如有建议与问题,欢迎评论区留言