一、项目展示
1.博客登录页
2.博客用户主页
3.编辑博客
4.博客内容展示
二、博客自动化测试用例
三、自动化测试
1.测试创建项目工程
引入 Maven 依赖, 导入 selenium 和 junit
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.10.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19.4</version>
</dependency>
</dependencies>
2.初始化浏览器驱动
public class IniAndEnd {
static WebDriver webDriver;
@BeforeAll
static void SetUp(){
webDriver = new ChromeDriver();
}
// 关闭浏览器
@AfterAll
static void TearDown(){
webDriver.quit(); // 退出浏览器操作
}
}
3.登录页的测试
1)测试成功
@Order(1)
@ParameterizedTest //通过参数化的方式来获取
@CsvFileSource(resources = "LoginSuccess.csv")
void LoginSuccess(String username, String password,String list_url) throws InterruptedException {
// 打开博客登录页面
webDriver.get("http://127.0.0.1:8080/blog_login.html");
// 智能等待3秒钟
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入账号
// webDriver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 输入密码
// webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 点击提交按钮
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 点击提交之后, 页面跳转到列表页
// 获取到当前页面的 url
String url = webDriver.getCurrentUrl();
// 如果对应的 url 等于对应的值 http://127.0.0.1:8080/blog_list.html 就是测试通过, 否则就是测试不通过
Assertions.assertEquals(list_url,url); //用 断言进行比较
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 列表页展示的信息是 登录者的信息
// 如果用户名展示的是 登录者信息那就测试通过, 否则就不通过
String name = webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
// getText(); 同过这个方法获取文本
// By.cssSelector 里面传入的是 前端页面登陆者信息的h3标签的 CopySelector
// 接下来进行对比
Assertions.assertEquals(username,name);
}
2)输入错误的用户密码
这里使用 CSV 的方法来传递参数
提示用户不存在
4.博客列表页测试
校验博客列表页的数量
@Order(2)
@Test
void BlogList() {
// 打开博客列表页
webDriver.get("http://127.0.0.1:8080/blog_list.html");
// 获取页面上所有博客标题 对应的元素
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS); //加上 智能等待
// 防止 还没有加载出来就去判断
int title_num = webDriver.findElements(By.cssSelector(".title")).size();
// 如果元素的数量不为零 , 测试通过
Assertions.assertNotEquals(0,title_num);
}
使用 css选择器来获取 文章标题数, 这里判断的是如果文章标题不为空就测试成功
5.博客详情页测试
public static Stream<Arguments> Generator() {
return Stream.of(
Arguments.arguments("http://127.0.0.1:8080/blog_detail.html",
"博客详情页",
"自动化测试"));
}
@Order(4)
@ParameterizedTest
@MethodSource("Generator")
void BlogDetail(String expected_url, String expected_title,String expected_blog_title){
// 先定位博客, 用 Xpath 来去定位就可以了
// 找到第一篇对应博客查看全文的按钮
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
// 来到了详情页
// 获取当前页面的 url
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
String url = webDriver.getCurrentUrl();
// 获取当前页面的 title
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
String title = webDriver.getTitle();
// 获取博客详情页的标题
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
String blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
// Assertions.assertEquals(expected_url,url); //这里需要更改
Assertions.assertEquals(expected_title,title);
Assertions.assertEquals(expected_blog_title,blog_title);
if(url.contains(expected_title)){
System.out.println("测试通过");
}else {
System.out.println(url);
System.out.println("测试不通过");
}
}
注意点:当CSS选择器定位不到div元素中的标题时,找到标题在div盒子的具体路径,分别列出div路径
使用这种定位方式:body > div.container > div.right > div > div.title
6.博客编辑页测试
1)测试写博客
@Order(3)
@Test
void EditBlog() throws InterruptedException {
// 找到写博客按钮,点击
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
// 找到输入框,输入博客标题
// 用js来输入
// webDriver.findElement(By.cssSelector("#title"))
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
sleep(3000);
// 点击发布
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(3000);
// 进行校验
// 获取当前页面 url (因为提交之后需要跳转页面)
String url = webDriver.getCurrentUrl();
Assertions.assertEquals("http://127.0.0.1:8080/blog_list.html",url);
}
2)校验已发布的博客标题和时间
@Test
void BlogInfoChecked(){
webDriver.get("http://127.0.0.1:8080/blog_list.html");
// 获取第一篇博客标题
String title = webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
// 获取第一篇博客的发布时间
String time = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]")).getText();
// 判断
// 校验博客标题是不是自动化测试
Assertions.assertEquals("自动化测试",title);
// 判断时间
// 如果 时间是 2024-3-18号发布的就通过
if(title.contains("2024-03-18")){
System.out.println("测试通过");
}else {
System.out.println("当前时间: "+time);
System.out.println("测试不通过");
}
}
7.删除博客
@Test
void DeletBlog() throws InterruptedException {
// 打开博客列表页
webDriver.get("http://127.0.0.1:8080/blog_list.html");
// 点击查看全文按钮
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
//webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
// 点击删除按钮
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();
// webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[4]/button[2]")).click();
// webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
sleep(3000);
// 校验 博客列表页 第一篇博客标题不是自动化测试了
String first_title = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]")).getText();
// 校验标题是否相等
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
Assertions.assertNotEquals(first_title,"自动测试");
}
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);
// 接下来进行校验
// 1.校验 url 跳转到登录的页面
String url = webDriver.getCurrentUrl();
Assertions.assertEquals("http://127.0.0.1:8080/blog_login.html",url);
// 2. 校验提交按钮
WebElement element = webDriver.findElement(By.cssSelector("#submit"));
Assertions.assertNotNull(element);
}