一,Web自动化测试
1,自动化测试的定义
Web自动化测试是指使用自动化工具和脚本对Web应用程序进行测试的过程。这些测试工具可以模拟用户在浏览器中的行为,如点击、输入文本、提交表单等,来检查应用程序是否符合预期的行为。这些测试工具可以快速、准确地执行测试,并且可以在短时间内执行大量的测试用例。
常用的Web自动化测试工具和框架包括Selenium、Cypress、Playwright、TestCafe等。
2,自动化测试流程
- 测试需求分析:确定要测试的功能和场景,编写测试用例。
- 环境配置:设置测试环境,包括安装所需的浏览器、Web服务器和数据库等。
- 编写测试脚本:使用选定的测试工具和框架编写自动化测试脚本。
- 执行测试:运行测试脚本,收集和分析测试结果。
- 结果报告:生成测试报告,记录测试的通过和失败情况。
- 维护测试:根据应用程序的变更更新测试脚本,确保测试的有效性。
3,自动化测试的好处
- 可以快速执行大量的测试用例,提高测试效率;
- 可以准确地模拟用户的行为,发现应用程序中的各种问题;
- 可以在不同的浏览器和操作系统上执行测试,确保应用程序的兼容性;
- 可以节省人力成本,减少人为测试过程中的错误和重复性工作;
- 相同的测试脚本可以重复使用,确保测试结果的一致性和准确性。
二,项目功能测试用例
三,自动化测试脚本编写
1,初始化浏览器驱动
在运行每个自动化测试用例之前都需要进行创建驱动,而造运行所有的测试方法结束之后来需要释放浏览器驱动,于是此时创建一个类来初始化浏览器驱动和释放浏览器。
public class InitAndEnd {
static WebDriver webDriver;
@BeforeAll
static void init(){
webDriver = new ChromeDriver();
}
@AfterAll
static void End(){
webDriver.quit(); // 关闭当前浏览器()
}
}
2,用户登陆功能
2.1,输入错误的用户名或密码,提示用户登录失败并不跳转个人博客列表页
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "WrongUser.csv")
void LoginFail(String username,String password){
webDriver.get("http://localhost:8080/login.html");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
webDriver.manage().window().maximize();
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
WebElement element = webDriver.findElement(By.cssSelector(".image"));
webDriver.findElement(By.cssSelector("#verificationCodeInput")).sendKeys(element.getAttribute("id"));
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
String alert_text = webDriver.switchTo().alert().getText();
// System.out.println(alert_text);
Assertions.assertNotEquals("用户登录成功!",alert_text);
webDriver.switchTo().alert().accept();
}
@Order(2)
@Test
void MyBlogListFail(){
String cur_title = webDriver.getTitle();
Assertions.assertNotEquals("个人博客列表页",cur_title);
}
2.2,输入正确的用户名和密码,提示登录成功并跳转个人博客列表页
@Order(3)
@ParameterizedTest
@CsvSource("admin,123")
void LoginSuccess(String username,String password) throws InterruptedException {
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
WebElement element = webDriver.findElement(By.cssSelector(".image"));
webDriver.findElement(By.cssSelector("#verificationCodeInput")).sendKeys(element.getAttribute("id"));
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
String alert_text = webDriver.switchTo().alert().getText();
// System.out.println(alert_text);
Assertions.assertEquals("用户登录成功!",alert_text);
webDriver.switchTo().alert().accept();
}
@Order(4)
@Test
void MyBlogListSuccess(){
String cur_tittle = webDriver.getTitle();
Assertions.assertEquals("个人博客列表页",cur_tittle);
}
3,博客编辑功能
3.1,点击写博客标签,能够成功跳转至博客编辑页
@Order(5)
@ParameterizedTest
@MethodSource("Generator")
void addBlogButton(String title,String url){
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
String cur_title = webDriver.getTitle();
String cur_url = webDriver.getCurrentUrl();
Assertions.assertEquals(title,cur_title);
Assertions.assertEquals(url,cur_url);
}
3.2,输入文章标题为空,点击发布文章,提示请输文章标题
@Order(6)
@Test
void addBlogFail() throws InterruptedException {
webDriver.findElement(By.cssSelector("#title")).sendKeys("");
webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
webDriver.switchTo().alert().accept();
sleep(2000);
webDriver.switchTo().alert().accept();
sleep(2000);
String cur_title = webDriver.getTitle();
System.out.println(cur_title);
Assertions.assertEquals("博客编辑",cur_title);
}
3.3,输入文章标题,内容系统默认,点击发布文章,页面跳转至个人博客列表页
@Order(7)
@Test
void addBlogSuccess1(){
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
webDriver.findElement(By.cssSelector("#title")).sendKeys("自动化测试");
webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
webDriver.switchTo().alert().accept();
String cur_tittle = webDriver.getTitle();
String cur_url = webDriver.getCurrentUrl();
Assertions.assertEquals("个人博客列表页",cur_tittle);
Assertions.assertEquals("http://localhost:8080/myblog_list.html",cur_url);
}
3.4,成功发布后,博客列表页中第一个博客信息与刚刚发布的博客信息相同
@Order(8)
@Test
void addBlogSuccess2(){
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
String blog_tittle = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.title")).getText();
int blog_count = webDriver.findElements(By.cssSelector("#artlist > div")).size();
Assertions.assertEquals(3,blog_count);
Assertions.assertEquals("自动化测试",blog_tittle);
}
4,博客详情功能
4.1,点击博客的查看全文页面,能够进行跳转至博客详情页
@Order(9)
@Test
void BlogContentSuccess1(){
webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(4)")).click();
String cur_title = webDriver.getTitle();
String cur_url = webDriver.getCurrentUrl();
Assertions.assertEquals("博客正文",cur_title);
if(cur_url.contains("http://localhost:8080/blog_content.html?id=")){
System.out.println("成功跳转至博客详情页!测试通过!");
}else{
System.out.println("未成功跳转至博客详情页!测试不通过!");
}
}
4.2,博客详情页的标题与点击博客的博客标题是一致相同的
@Order(10)
@ParameterizedTest
@CsvSource("自动化测试")
void CheckBlogContent(String blog_title){
String cur_blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
Assertions.assertEquals(blog_title,cur_blog_title);
}
4.3,页面刷新,博客的阅读量和作者的访问量进行加1操作
@Order(11)
@Test
void CheckBlogReadCount(){
int readCount = Integer.parseInt(webDriver.findElement(By.cssSelector("#rcount")).getText());
webDriver.navigate().refresh();
int cur_readCount = Integer.parseInt(webDriver.findElement(By.cssSelector("#rcount")).getText());
Assertions.assertEquals(readCount+1,cur_readCount);
}
5,博客更新功能
5.1,在个人博客列表页点击博客修改,能够成功跳转至博客修改页
@Order(12)
@Test
void updateBlog(){
webDriver.get("http://localhost:8080/myblog_list.html");
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(5)")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
String cur_title = webDriver.getTitle();
String cur_url = webDriver.getCurrentUrl();
Assertions.assertEquals("博客编辑",cur_title);
if(cur_url.contains("http://localhost:8080/blog_edit.html?id=")){
System.out.println("成功跳转至博客编辑页!测试通过!");
}else{
System.out.println("未成功跳转至博客编辑页!测试不通过!");
}
}
5.2,成功跳转后,页面显示的博客信息与点击的博客信息是匹配的
@Order(13)
@ParameterizedTest
@CsvFileSource(resources = "BlogInfo.csv")
void CheckUpdateBlog(String blog_title) throws InterruptedException {
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
String cur_blog_tittle = webDriver.findElement(By.cssSelector("#title")).getAttribute("value");
System.out.println(cur_blog_tittle);
sleep(3000);
Assertions.assertEquals(blog_title,cur_blog_tittle);
}
5.3,修改文章标题,点击发布文章,提示修改成功,页面跳转至博客列表页
@Order(14)
@ParameterizedTest
@CsvFileSource(resources = "doUpdateBlog.csv")
void doUpdateBlog(String new_title,String alert_text,String title,String url) throws InterruptedException {
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
webDriver.findElement(By.cssSelector("#title")).sendKeys(Keys.CONTROL,"A");
webDriver.findElement(By.cssSelector("#title")).sendKeys(Keys.CONTROL,"X");
webDriver.findElement(By.cssSelector("#title")).sendKeys(new_title);
webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
sleep(2000);
String cur_alert_text = webDriver.switchTo().alert().getText();
Assertions.assertEquals(alert_text,cur_alert_text);
webDriver.switchTo().alert().accept();
String cur_title = webDriver.getTitle();
String cur_url = webDriver.getCurrentUrl();
Assertions.assertEquals(title,cur_title);
Assertions.assertEquals(url,cur_url);
}
5.5,成功修改后,博客列表页中第一个博客信息与刚刚修改的博客信息相同
@Order(15)
@ParameterizedTest
@CsvSource("自动化测试修改")
void CheckUpdateIsSuccess(String blog_title) throws InterruptedException {
sleep(2000);
String cur_blog_title = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.title")).getText();
sleep(2000);
Assertions.assertEquals(blog_title,cur_blog_title);
}
6,博客删除功能
6.1,点击博客删除,在提示框点击取消,不提示成功删除,博客列表页不变
@Order(16)
@Test
void DeleteBlogFail() throws InterruptedException {
sleep(2000);
int blog_count = webDriver.findElements(By.cssSelector("#artlist > div")).size();
System.out.println(blog_count);
webDriver.findElement(By.cssSelector("#artlist > div > a:nth-child(6)")).click();
webDriver.switchTo().alert().dismiss();
int cur_blog_count = webDriver.findElements(By.cssSelector("#artlist > div")).size();
Assertions.assertEquals(blog_count,cur_blog_count);
}
6.2,点击博客删除,在提示框点击确认,提示成功删除
@Order(17)
@Test
void DeleteBlogSuccess() throws InterruptedException {
sleep(2000);
webDriver.findElement(By.cssSelector("#artlist > div > a:nth-child(6)")).click();
webDriver.switchTo().alert().accept();
sleep(2000);
String text = webDriver.switchTo().alert().getText();
sleep(2000);
webDriver.switchTo().alert().accept();
Assertions.assertEquals("恭喜:文章删除成功!",text);
}
7,用户注销功能
7.1,点击注销标签,弹出提示弹框,点击取消,注销失败
@Order(18)
@Test
void logoutFail(){
String cur_url = webDriver.getCurrentUrl();
String cur_title = webDriver.getTitle();
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
webDriver.switchTo().alert().dismiss();
String new_cur = webDriver.getCurrentUrl();
String new_title = webDriver.getTitle();
Assertions.assertEquals(cur_url,new_cur);
Assertions.assertEquals(cur_title,new_title);
}
7.2,点击注销标签,弹出弹框,点击确定,提示执行注销操作,注销成功
@Order(19)
@Test
void logoutSuccess() throws InterruptedException {
String cur_url = webDriver.getCurrentUrl();
String cur_title = webDriver.getTitle();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
webDriver.switchTo().alert().accept();
webDriver.switchTo().alert().accept();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
String new_cur = webDriver.getCurrentUrl();
String new_title = webDriver.getTitle();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
Assertions.assertNotEquals(cur_url,new_cur);
Assertions.assertNotEquals(cur_title,new_title);
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
}