作者:~小明学编程
文章专栏:测试开发
格言:热爱编程的,终将被编程所厚爱。
目录
博客界面
测试用例
界面测试
功能测试
性能测试
兼容性测试
易用性测试
安全性测试
编辑自动化测试
登录界面的测试
界面文字模块
测试窗口伸缩
测试错误登录案例
测试正确的登录案例
整体代码
博客列表页测试
界面文字模块的测试
测试对编辑页的跳转是否正常
测试对博客编辑页的跳转是否正常
测试注销按钮是否正常
整体代码
博客详情页的测试
界面文字模块的测试
对删除按键的显示与功能进行测试
测试能否成功跳转到博客编辑页
测试能否跳转到主页
测试能否注销登录
整体代码
博客编辑页的测试
测试界面文字
测试发布功能是否正常
测试跳转主页是否正常
测试退出登录是否正常
整体代码
通过 Suite 套件测试
博客界面
测试用例
界面测试
功能测试
性能测试
兼容性测试
易用性测试
安全性测试
自动化测试
下面我们就对上面的部分用例进行自动化的测试。
登录界面的测试
首先我们要先准备好驱动,这里我们采用懒汉模式,然后就是start()方法,该方法我们在所有的测试方法之前执行一次,然后还有close()方法用于关闭浏览器回收驱动,用于在所有的测试方法执行之前来执行。
界面文字模块
该模块用于测试我们的登录界面的一些文字信息是否正确。
@Test
@Order(1)
//先对我们登录界面的一些文字描述进行测试
public void appearance() {
start();
//检测系统名称是否正确
String str1 = driver.findElement(By.cssSelector("body > div.nav > span")).getText();
Assertions.assertEquals("我的博客系统",str1);
//主页,和写博客是否正常
String str2 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).getText();
Assertions.assertEquals("主页",str2);
//登录窗口的内容
String str3 = driver.findElement(By.cssSelector("body > div.login-container > form > div > h3")).getText();
Assertions.assertEquals("登录",str3);
String str4 = driver.findElement(By.cssSelector
("body > div.login-container > form > div > div:nth-child(2) > span")).getText();
Assertions.assertEquals("用户名",str4);
String str5 = driver.findElement(By.cssSelector("body > div.login-container > form > div > div:nth-child(3) > span")).getText();
Assertions.assertEquals("密码",str5);
String str6 = driver.findElement(By.cssSelector("#submit")).getAttribute("value");
Assertions.assertEquals("提交",str6);
}
测试窗口伸缩
@Test
@Order(2)
//测试窗口是否能正常的伸缩
public void windowSize() {
driver.manage().window().maximize();
driver.manage().window().minimize();
driver.manage().window().setSize(new Dimension(1000,800));//自定义大小
}
测试错误登录案例
测试错误登录案例这里分为两种一种是账号密码不为空但是不完全正确,还有一种就是账号或者密码存在空值的情况。
@Order(3)
@ParameterizedTest
@CsvSource(value = {"11,22","2334,4566","张三,李四","王五,123"})//分别针对错误账号密码,空账号和空密码进行验证
//验证我们的错误登录,我们输入几种错误的案例
public void falseLogin(String name,String password) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
//处理用户名为空的情况
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
String test = driver.findElement(By.cssSelector("body")).getText();
Assertions.assertEquals("用户名或密码错误!",test);
driver.navigate().back();//跳转回上一个界面
}
@Order(4)
@ParameterizedTest
@CsvSource(value = {",","123,",",123"})//分别针对空账号空密码,空账号和空密码进行测试
//验证我们的错误登录,我们输入几种错误的案例
public void nullLogin(String name,String password) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
//处理用户名为空的情况
if (name!=null) {
driver.findElement(By.cssSelector("#username")).sendKeys(name);
}
driver.findElement(By.cssSelector("#password")).clear();
if (password!=null) {
driver.findElement(By.cssSelector("#password")).sendKeys(password);
}
driver.findElement(By.cssSelector("#submit")).click();
String test = driver.findElement(By.cssSelector("body")).getText();
Assertions.assertEquals("当前用户名或密码为空!",test);
driver.navigate().back();//跳转回上一个界面
}
测试正确的登录案例
@Order(5)
@ParameterizedTest
@CsvSource(value = {"aaa,123","bbb,456"})
//验证我们密码正确的情况
public void trueLogin(String name,String password) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
String test = driver.findElement(By.cssSelector("body")).getText();
driver.navigate().back();//跳转回上一个界面
}
整体代码
package blogsystem;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86184
* Date: 2023-01-22
* Time: 15:51
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LoginTest {
private static ChromeDriver driver;
//这里我们采用懒汉模式
private static ChromeDriver getDriver() {
if (driver == null) {
driver = new ChromeDriver();
//隐式等待
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
return driver;
}
@BeforeAll
public static void start() {
driver = getDriver();
driver.get("http://101.42.54.191:8080/blog_system/blog_login.html");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));//等待页面的渲染完成
}
@Test
@Order(1)
//先对我们登录界面的一些文字描述进行测试
public void appearance() {
start();
//检测系统名称是否正确
String str1 = driver.findElement(By.cssSelector("body > div.nav > span")).getText();
Assertions.assertEquals("我的博客系统",str1);
//主页,和写博客是否正常
String str2 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).getText();
Assertions.assertEquals("主页",str2);
//登录窗口的内容
String str3 = driver.findElement(By.cssSelector("body > div.login-container > form > div > h3")).getText();
Assertions.assertEquals("登录",str3);
String str4 = driver.findElement(By.cssSelector
("body > div.login-container > form > div > div:nth-child(2) > span")).getText();
Assertions.assertEquals("用户名",str4);
String str5 = driver.findElement(By.cssSelector("body > div.login-container > form > div > div:nth-child(3) > span")).getText();
Assertions.assertEquals("密码",str5);
String str6 = driver.findElement(By.cssSelector("#submit")).getAttribute("value");
Assertions.assertEquals("提交",str6);
}
@Test
@Order(2)
//测试窗口是否能正常的伸缩
public void windowSize() {
driver.manage().window().maximize();
driver.manage().window().minimize();
driver.manage().window().setSize(new Dimension(1000,800));//自定义大小
}
// @Test
@Order(3)
@ParameterizedTest
@CsvSource(value = {"11,22","2334,4566","张三,李四","王五,123"})//分别针对错误账号密码,空账号和空密码进行验证
//验证我们的错误登录,我们输入几种错误的案例
public void falseLogin(String name,String password) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
//处理用户名为空的情况
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
String test = driver.findElement(By.cssSelector("body")).getText();
Assertions.assertEquals("用户名或密码错误!",test);
driver.navigate().back();//跳转回上一个界面
}
@Order(4)
@ParameterizedTest
@CsvSource(value = {",","123,",",123"})//分别针对空账号空密码,空账号和空密码进行测试
//验证我们的错误登录,我们输入几种错误的案例
public void nullLogin(String name,String password) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
//处理用户名为空的情况
if (name!=null) {
driver.findElement(By.cssSelector("#username")).sendKeys(name);
}
driver.findElement(By.cssSelector("#password")).clear();
if (password!=null) {
driver.findElement(By.cssSelector("#password")).sendKeys(password);
}
driver.findElement(By.cssSelector("#submit")).click();
String test = driver.findElement(By.cssSelector("body")).getText();
Assertions.assertEquals("当前用户名或密码为空!",test);
driver.navigate().back();//跳转回上一个界面
}
@Order(5)
@ParameterizedTest
@CsvSource(value = {"aaa,123","bbb,456"})
//验证我们密码正确的情况
public void trueLogin(String name,String password) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
String test = driver.findElement(By.cssSelector("body")).getText();
driver.navigate().back();//跳转回上一个界面
}
@AfterAll
//所有的测试完了之后关闭驱动
public static void close() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}
}
博客列表页测试
在博客列表页中我们主要测试当前列表中的各个文字的显示是否正确,测试对其它页面的跳转是否正常。
界面文字模块的测试
@Test
@Order(1)
//先对我们登录界面的一些文字描述进行测试
public void appearance() {
login();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));//等待页面的渲染完成
//检测系统名称是否正确
String str1 = driver.findElement(By.cssSelector("body > div.nav > span")).getText();
Assertions.assertEquals("我的博客系统",str1);
//主页,和写博客是否正常
String str2 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).getText();
Assertions.assertEquals("主页",str2);
String str3 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).getText();
Assertions.assertEquals("注销",str3);
String str4 = driver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
Assertions.assertEquals("aaa",str4);//验证用户名
//验证标签内容是否正确
String str5 = driver.findElement(By.cssSelector("body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")).getText();
Assertions.assertEquals("文章",str5);
String str6 = driver.findElement(By.cssSelector("body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")).getText();
Assertions.assertEquals("分类",str6);
String str7 = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
Assertions.assertEquals("这是第一篇博客",str7);
String str8 = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.date")).getText();
Assertions.assertEquals("2023-01-14 15:04:13",str8);
String str9 = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).getText();
Assertions.assertEquals("查看全文 >>",str9);
}
测试对编辑页的跳转是否正常
//是否能成功的跳转到博客编辑页
@Test
@Order(2)
public void skipDetail() {
driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertNotEquals("http://101.42.54.191:8080/blog_system/blog_login.html",url);
driver.navigate().back();//跳转回上一个界面
}
测试对博客编辑页的跳转是否正常
@Test
@Order(3)
public void skipWrite() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertNotEquals("http://101.42.54.191:8080/blog_system/blog_login.html",url);
driver.navigate().back();//跳转回上一个界面
}
测试注销按钮是否正常
@Test
@Order(4)
public void skipQuit() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_login.html",url);
}
整体代码
package blogsystem;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86184
* Date: 2023-01-22
* Time: 19:11
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ListTest {
private static ChromeDriver driver;
private static ChromeDriver getDriver() {
if (driver == null) {
driver = new ChromeDriver();
//隐式等待
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
return driver;
}
@BeforeAll
public static void start() {
driver = getDriver();
driver.get("http://101.42.54.191:8080/blog_system/blog_login.html");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));//等待页面的渲染完成
}
public void login() {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys("aaa");
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
}
@Test
@Order(1)
//先对我们登录界面的一些文字描述进行测试
public void appearance() {
login();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));//等待页面的渲染完成
//检测系统名称是否正确
String str1 = driver.findElement(By.cssSelector("body > div.nav > span")).getText();
Assertions.assertEquals("我的博客系统",str1);
//主页,和写博客是否正常
String str2 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).getText();
Assertions.assertEquals("主页",str2);
String str3 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).getText();
Assertions.assertEquals("注销",str3);
String str4 = driver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
Assertions.assertEquals("aaa",str4);//验证用户名
//验证标签内容是否正确
String str5 = driver.findElement(By.cssSelector("body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")).getText();
Assertions.assertEquals("文章",str5);
String str6 = driver.findElement(By.cssSelector("body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")).getText();
Assertions.assertEquals("分类",str6);
String str7 = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
Assertions.assertEquals("这是第一篇博客",str7);
String str8 = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.date")).getText();
Assertions.assertEquals("2023-01-14 15:04:13",str8);
String str9 = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).getText();
Assertions.assertEquals("查看全文 >>",str9);
}
//是否能成功的跳转到博客编辑页
@Test
@Order(2)
public void skipDetail() {
driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertNotEquals("http://101.42.54.191:8080/blog_system/blog_login.html",url);
driver.navigate().back();//跳转回上一个界面
}
@Test
@Order(3)
public void skipWrite() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertNotEquals("http://101.42.54.191:8080/blog_system/blog_login.html",url);
driver.navigate().back();//跳转回上一个界面
}
//
@Test
@Order(4)
public void skipQuit() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_login.html",url);
}
@AfterAll
//所有的测试完了之后关闭驱动
public static void close() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}
}
博客详情页的测试
在博客编辑页我们主要对界面的文字是否正确进行测试,然后对我们的删除按键是否能正确的显示进行测试,还有对各个页面的跳转进行测试。
界面文字模块的测试
@Test
@Order(1)
//先对我们登录界面的一些文字描述进行测试
public void appearance() {
login();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));//等待页面的渲染完成
//检测系统名称是否正确
String str1 = driver.findElement(By.cssSelector("body > div.nav > span")).getText();
Assertions.assertEquals("我的博客系统",str1);
//主页,和写博客是否正常
String str2 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).getText();
Assertions.assertEquals("主页",str2);
String str3 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).getText();
Assertions.assertEquals("注销",str3);
//验证标签内容是否正确
String str5 = driver.findElement(By.cssSelector("body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")).getText();
Assertions.assertEquals("文章",str5);
String str6 = driver.findElement(By.cssSelector("body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")).getText();
Assertions.assertEquals("分类",str6);
}
对删除按键的显示与功能进行测试
@Test
@Order(2)
//验证我们的删除按钮是否能正常的显示并且使用
public void delete() {
// login();
String name = driver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
//如果当前的博客用户名与登录用户名一致删除按钮应该存在
if ("bbb".equals(name)) {
String delete = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).getText();
Assertions.assertEquals("删除",delete);
} else {
try{
String delete = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).getText();
}catch (Exception e) {
System.out.println("找不到删除键");
}
}
}
测试能否成功跳转到博客编辑页
//是否能成功的跳转到博客编辑页
@Test
@Order(3)
public void skipWrite() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_edit.html",url);
driver.navigate().back();//跳转回上一个界面
}
测试能否跳转到主页
@Test
@Order(4)
public void skipMain() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_list.html",url);
driver.navigate().back();//跳转回上一个界面
}
测试能否注销登录
@Test
@Order(5)
public void skipQuit() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_login.html",url);
}
整体代码
package blogsystem;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86184
* Date: 2023-01-22
* Time: 21:04
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DetailTest {
private static ChromeDriver driver;
private String name;//用户名
private static ChromeDriver getDriver() {
if (driver == null) {
driver = new ChromeDriver();
//隐式等待
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
return driver;
}
@BeforeAll
public static void start() {
driver = getDriver();
driver.get("http://101.42.54.191:8080/blog_system/blog_login.html");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));//等待页面的渲染完成
}
public void login() {
String name = "bbb";
this.name = name;
System.out.println(this.name);
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#password")).sendKeys("456");
driver.findElement(By.cssSelector("#submit")).click();
//切换到博客详情页
driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
}
@Test
@Order(1)
//先对我们登录界面的一些文字描述进行测试
public void appearance() {
login();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));//等待页面的渲染完成
//检测系统名称是否正确
String str1 = driver.findElement(By.cssSelector("body > div.nav > span")).getText();
Assertions.assertEquals("我的博客系统",str1);
//主页,和写博客是否正常
String str2 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).getText();
Assertions.assertEquals("主页",str2);
String str3 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).getText();
Assertions.assertEquals("注销",str3);
//验证标签内容是否正确
String str5 = driver.findElement(By.cssSelector("body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")).getText();
Assertions.assertEquals("文章",str5);
String str6 = driver.findElement(By.cssSelector("body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")).getText();
Assertions.assertEquals("分类",str6);
}
@Test
@Order(2)
//验证我们的删除按钮是否能正常的显示并且使用
public void delete() {
// login();
String name = driver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
//如果当前的博客用户名与登录用户名一致删除按钮应该存在
if ("bbb".equals(name)) {
String delete = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).getText();
Assertions.assertEquals("删除",delete);
} else {
try{
String delete = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).getText();
}catch (Exception e) {
System.out.println("找不到删除键");
}
}
}
//是否能成功的跳转到博客编辑页
@Test
@Order(3)
public void skipWrite() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_edit.html",url);
driver.navigate().back();//跳转回上一个界面
}
@Test
@Order(4)
public void skipMain() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_list.html",url);
driver.navigate().back();//跳转回上一个界面
}
@Test
@Order(5)
public void skipQuit() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_login.html",url);
}
@AfterAll
//所有的测试完了之后关闭驱动
public static void close() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}
}
博客编辑页的测试
测试界面文字
@Test
@Order(1)
//先对我们登录界面的一些文字描述进行测试
public void appearance() {
login();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));//等待页面的渲染完成
//检测系统名称是否正确
String str1 = driver.findElement(By.cssSelector("body > div.nav > span")).getText();
Assertions.assertEquals("我的博客系统",str1);
//主页,和写博客是否正常
String str2 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).getText();
Assertions.assertEquals("主页",str2);
String str3 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).getText();
Assertions.assertEquals("注销",str3);
String str4 = driver.findElement(By.cssSelector("#submit")).getAttribute("value");
Assertions.assertEquals("发布文章",str4);
String str5 = driver.findElement(By.cssSelector("#title")).getAttribute("placeholder");
Assertions.assertEquals("在此处输入标题",str5);
}
测试发布功能是否正常
@Test
@Order(3)
public void publish() {
driver.findElement(By.cssSelector("#title")).sendKeys("这是第三篇博客");
driver.findElement(By.cssSelector("#submit")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
//若发布成功则会跳转到博客列表页
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_list.html",url);
driver.navigate().back();//跳转回上一个界面
}
测试跳转主页是否正常
@Test
@Order(2)
public void skipMain() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_list.html",url);
driver.navigate().back();//跳转回上一个界面
}
测试退出登录是否正常
@Test
@Order(4)
public void skipQuit() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_login.html",url);
}
整体代码
package blogsystem;
import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86184
* Date: 2023-01-22
* Time: 23:08
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class editTest {
private static ChromeDriver driver;
private static ChromeDriver getDriver() {
if (driver == null) {
driver = new ChromeDriver();
//隐式等待
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
return driver;
}
@BeforeAll
public static void start() {
driver = getDriver();
driver.get("http://101.42.54.191:8080/blog_system/blog_login.html");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));//等待页面的渲染完成
}
public void login() {
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys("bbb");
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#password")).sendKeys("456");
driver.findElement(By.cssSelector("#submit")).click();
//切换到博客编辑页
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
}
@Test
@Order(1)
//先对我们登录界面的一些文字描述进行测试
public void appearance() {
login();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));//等待页面的渲染完成
//检测系统名称是否正确
String str1 = driver.findElement(By.cssSelector("body > div.nav > span")).getText();
Assertions.assertEquals("我的博客系统",str1);
//主页,和写博客是否正常
String str2 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).getText();
Assertions.assertEquals("主页",str2);
String str3 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).getText();
Assertions.assertEquals("注销",str3);
String str4 = driver.findElement(By.cssSelector("#submit")).getAttribute("value");
Assertions.assertEquals("发布文章",str4);
String str5 = driver.findElement(By.cssSelector("#title")).getAttribute("placeholder");
Assertions.assertEquals("在此处输入标题",str5);
}
@Test
@Order(2)
public void skipMain() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_list.html",url);
driver.navigate().back();//跳转回上一个界面
}
@Test
@Order(3)
public void publish() {
driver.findElement(By.cssSelector("#title")).sendKeys("这是第三篇博客");
driver.findElement(By.cssSelector("#submit")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
//若发布成功则会跳转到博客列表页
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_list.html",url);
driver.navigate().back();//跳转回上一个界面
}
@Test
@Order(4)
public void skipQuit() {
driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
String curWindow = driver.getWindowHandle();//获取当前的句柄
Set<String> windows = driver.getWindowHandles();//获取所有的句柄
for (String window:windows) {
if (window!=curWindow) {
driver.switchTo().window(window);//切换到点击后的句柄
}
}
String url = driver.getCurrentUrl();//获取当前页面的url然后与博客详情页的页面url做对比看看页面是否切换成功
Assertions.assertEquals("http://101.42.54.191:8080/blog_system/blog_login.html",url);
}
@AfterAll
//所有的测试完了之后关闭驱动
public static void close() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}
}
通过 Suite 套件测试
package com.auto;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 86184
* Date: 2023-01-21
* Time: 22:25
*/
@Suite
@SelectPackages("com.auto")
public class RunAll {
}
测试全部通过。