项目体验地址(账号:123,密码:123)http://120.53.20.213:8080/blog_system/login.html
项目后端说明:http://t.csdn.cn/32Nnv
项目码云Gitee地址:https://gitee.com/GoodManSS/project/tree/master/blog_system
项目测试说明:http://t.csdn.cn/LtWYJ
脑图分析:
AutoTestUtil类
存放一些公共方法,比如单例模式创建ChromeDriver对象,截图功能。
//单例模式创建ChromeDrive
public static ChromeDriver createDriver() {
if (driver == null) {
synchronized (AutoTestUtil.class) {
if (driver == null) {
driver = new ChromeDriver();
//创建隐式等待
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
}
}
}
return driver;
}
//截图功能
public static void getScreen(String location) throws IOException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
String time = simpleDateFormat.format(System.currentTimeMillis());
String saveFileName = "./src/test/java/com/screenPicture/"+location+"/"+time+".png";
File srcFile = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile,new File(saveFileName));
}
1.LoginTest登录页测试
登录页面加载测试,登录成功/登陆失败的功能测试
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LoginTest extends AutoTestUtil {
private static ChromeDriver driver = createDriver();
//进入到登录页面
@BeforeAll
static void baseControl() {
//注意这里可能没有登录记录 注意前面的登录测试要有记录
driver.get("http://120.53.20.213:8080/blog_system/login.html");
}
// 1.判断页面元素是否正常加载
// 检查点:是否存在 注册和登录按钮
@Test
@Order(1)
public void loginPageLoadRight() {
//注册按钮
String expect1 = driver.findElement(By.cssSelector("body > div.nav > a")).getText();
Assertions.assertEquals(expect1, "注册");
//登录按钮
String expect2 = driver.findElement(By.xpath("//*[@id=\"login-btn\"]")).getAttribute("value");
Assertions.assertEquals(expect2, "确认登陆");
}
//2.错误的登录测试
@ParameterizedTest
@CsvSource({"11,1", "22,333"})
@Order(2)
public void loginError(String userId, String passWord) {
//①.进行登录
driver.findElement(By.cssSelector("#userid")).sendKeys(userId);
driver.findElement(By.cssSelector("#password")).sendKeys(passWord);
driver.findElement(By.xpath("//*[@id=\"login-btn\"]")).click();
//②.判断是否登陆成功--通过看是否出现用户名
String expect1 = driver.findElement(By.cssSelector("body")).getText();
Assertions.assertNotEquals(expect1, "用户名或密码为空! 登陆失败!");
//③.返回页面
driver.navigate().back();
driver.findElement(By.cssSelector("#userid")).clear();
driver.findElement(By.cssSelector("#password")).clear();
}
//3.正常登录的测试
@ParameterizedTest
@CsvSource({"123,123"})
@Order(3)
public void loginRight(String userId, String passWord) {
//①.进行登录
driver.findElement(By.cssSelector("#userid")).sendKeys(userId);
driver.findElement(By.cssSelector("#password")).sendKeys(passWord);
driver.findElement(By.xpath("//*[@id=\"login-btn\"]")).click();
//②.判断是否登陆成功--通过看是否出现用户名
driver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3"));
//③.返回页面
driver.navigate().back();
}
2.ListTest列表页测试
测试列表页是否能够正常加载
public class ListTest extends AutoTestUtil {
private static ChromeDriver driver = createDriver();
public static void baseControl() {
driver.get("http://120.53.20.213:8080/blog_system/blog_list.html");
}
//测试列表页是否能正常加载
@Test
public void listPageLoadRight() {
baseControl();
String expect1 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).getText();
Assertions.assertEquals(expect1, "写博客");
String expect2 = driver.findElement(By.cssSelector("body > div.nav > span")).getText();
Assertions.assertEquals(expect2, "我的博客系统");
}
}
3.EditTest编辑页测试
主要测试博客编辑页是否能够正常加载、博客编辑功能是否能够正常使用
public class EditTest extends AutoTestUtil {
public static ChromeDriver driver;
@BeforeAll
private static void baseControl() {
driver = createDriver();
driver.get("http://120.53.20.213:8080/blog_system/blog_edit.html");
}
//1.测试编辑页导航栏是否正常加载
@Test
void editPageLoadRight() throws IOException {
String expect1 = driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).getText();
Assertions.assertEquals(expect1, "注销");
String expect2 = driver.findElement(By.cssSelector("body > div.nav > span")).getText();
Assertions.assertEquals(expect2, "我的博客系统");
getScreen("editPageLoadRight");
}
//2.测试编辑文章 发布文章功能是否ok
@Test
void editAndSubimitBlog() throws IOException {
String expect = "test";
driver.findElement(By.cssSelector("#blog-title")).sendKeys(expect);
//因博客系统使用到的编辑是第三方软件,所以不能直接使用sendKeys向编辑模块发送文本
driver.findElement(By.cssSelector("#editor > div.editormd-toolbar > div > ul > li:nth-child(21) > a")).click();
driver.findElement(By.cssSelector("#editor > div.editormd-toolbar > div > ul > li:nth-child(5) > a")).click();
driver.findElement(By.cssSelector("#submit")).click();
//获取列表页第一条博客的标题文本,检查是否跟预期相符
String actual = driver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title")).getText();
Assertions.assertEquals(expect,actual);
}
}
4.DetailTest详情页测试
测试了博客详情页是否可以正常加载显示
测试删除功能是否正常
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DetailTest extends AutoTestUtil {
private static ChromeDriver driver;
@BeforeAll
public static void baseControl() {
driver = createDriver();
driver.get("http://120.53.20.213:8080/blog_system/blog_list.html");
}
@Test
@Order(1)
void detailLoadRight() throws IOException {
driver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > a")).click();
driver.findElement(By.cssSelector("#content"));
getScreen("detailTest");
}
@Test
@Order(2)
void blogDelete(){
String expect = driver.findElement(By.cssSelector("body > div.container > div.container-right > div > div.date")).getText();
//删除 在测试博客编辑功能时候发布的文章
driver.findElement(By.cssSelector("#delete-btn")).click();
//确定返回到博客列表页 第一篇文章不再是测试发布的
String actual = driver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.date")).getText();
Assertions.assertNotEquals(expect,actual);
}
}