目录
一、设计博客系统的测试用例
二、利用测试用例进行测试
测试登录页面
界面测试
功能测试
测试博客列表页
界面测试
功能测试
测试博客详情页
界面测试
功能测试
博客编辑页测试
界面测试
功能测试
一、设计博客系统的测试用例
二、利用测试用例进行测试
测试的文件放在maven项目的test文件夹下,需要在之前的maven项目中添加一些自动化测试的依赖:
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-reporting -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-reporting</artifactId>
<version>1.8.2</version>
</dependency>
测试登录页面
首先定义start方法和close方法,并利用相关注解使其在测试之前和测试之后都执行一次。
@BeforeAll
public void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
@AfterAll
public void close(){
driver.quit();
}
界面测试
首先来测试界面的文字信息以及页面的元素布局是否正确。
public class InterfaceTest {
public static ChromeDriver driver;
public static ChromeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
driver = new ChromeDriver();
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 测试登陆文字
*/
@Test
public void testDengLu(){
String dengLu = driver.findElement(By.xpath("/html/body/div[2]/div/h3")).getText();
Assertions.assertEquals(dengLu,"登录");
}
/**
* 测试提交按钮的文字
*/
@Test
public void testTiJiao(){
String tiJiao = driver.findElement(By.xpath("//*[@id=\"submit\"]")).getAttribute("value");
Assertions.assertEquals(tiJiao,"提交");
}
/**
* 测试用户名输入框
*/
@Test
public void testUserInput(){
WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input"));
Assertions.assertNotNull(webElement);
}
/**
* 测试密码输入框
*/
@Test
public void testPasswordInput(){
WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input"));
Assertions.assertNotNull(webElement);
}
/**
* 测试提交按钮
*/
@Test
public void testSubmit(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"submit\"]"));
Assertions.assertNotNull(webElement);
}
}
功能测试
测试输入正确的用户名和密码、错误的用户名或密码以及空的用户名或密码来查看是否会跳转到博客列表页。
测试正确的用户名和密码:
/**
* 测试正确登录
*/
@ParameterizedTest
@CsvSource(value = {"zhangsan,1234","zhangyi,1234"})
public void testLoginTrue(String user,String password){
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys(user);
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals(url,"http://43.143.208.132:8080/blog_system/blog-list.html");
driver.navigate().back();
}
测试用户名或密码为空:
/**
* 测试用户名或密码为空
*/
@ParameterizedTest
@CsvSource(value = {"zhangyi,",",1234",","})
public void testLoginNull(String user,String password){
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
if(user != null){
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys(user);
}
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
if(password != null){
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys(password);
}
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
String tips = driver.findElement(By.xpath("/html/body")).getText();
Assertions.assertEquals("用户名或密码不能为空",tips);
driver.navigate().back();
}
测试用户名或密码错误:
/**
* 测试用户名或密码错误
*/
@ParameterizedTest
@CsvSource(value = {"zhangyi,6781","liuyy,1234"})
public void testLoginFalse(String user,String password){
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys(user);
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
String tips = driver.findElement(By.xpath("/html/body")).getText();
Assertions.assertEquals("用户名或密码错误",tips);
driver.navigate().back();
}
测试博客列表页
界面测试
主要测试页面的文字,个人信息以及查看全文按钮是否正常显示。
public class InterfaceTest {
public static ChromeDriver driver;
public static ChromeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
driver = new ChromeDriver();
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys("zhangsan");
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys("1234");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 测试个人信息
*/
@Test
public void testInfo(){
String dengLu = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/a")).getText();
Assertions.assertEquals(dengLu,"gitee地址");
String user = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/h3")).getText();
Assertions.assertEquals(user,"zhangsan");
}
/**
* 测试查看全文按钮的文字
*/
@Test
public void testQuanWen(){
String tiJiao = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).getText();
Assertions.assertEquals("查看全文",tiJiao);
}
/**
* 测试个人信息的头像是否正常
*/
@Test
public void testUserInput(){
WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/img"));
Assertions.assertNotNull(webElement);
}
/**
* 测试文章标题是否正常
*/
@Test
public void testPasswordInput(){
WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]"));
Assertions.assertNotNull(webElement);
}
}
功能测试
查看全文按钮的功能是否正常。
/**
* 查看全文按钮是否能正确跳转
*/
@Test
public void testQuanWen(){
driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-detail.html?blogId=5",url);
driver.navigate().back();
}
写博客按钮是否正常。
/**
* 写博客超链接是否正常
*/
@Test
public void testXieBoKe(){
driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-edit.html",url);
driver.navigate().back();
}
测试注销超链接是否正常。
/**
* 注销超链接是否正常
*/
@Test
public void testZhuXiao(){
driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-login.html",url);
driver.navigate().back();
}
测试博客详情页
界面测试
测试博客的详情信息是否都正确显示。
public class InterfaceTest {
public static ChromeDriver driver;
public static ChromeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
driver = new ChromeDriver();
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys("zhangsan");
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys("1234");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 测试个人信息
*/
@Test
public void testInfo(){
String dengLu = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/a")).getText();
Assertions.assertEquals(dengLu,"gitee地址");
String user = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/h3")).getText();
Assertions.assertEquals(user,"zhanger");
WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/img"));
Assertions.assertNotNull(webElement);
}
/**
* 测试文章标题
*/
@Test
public void testTitle(){
String title = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/h3")).getText();
Assertions.assertNotNull(title);
}
/**
* 测试文章发表日期
*/
@Test
public void testDate(){
String date = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[1]")).getText();
Assertions.assertNotNull(date);
}
/**
*测试文章正文
*/
public void testText(){
String text = driver.findElement(By.xpath("//*[@id=\"desc\"]/p")).getText();
Assertions.assertNotNull(text);
}
}
功能测试
博客详情页的功能测试与博客列表页相似,主要是对超链接进行测试。
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FunctionTest {
public static ChromeDriver driver;
public static ChromeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
driver = new ChromeDriver();
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys("yiyi");
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys("1234");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 写博客超链接是否正常
*/
@Test
@Order(1)
public void testXieBoKe(){
driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-edit.html",url);
driver.navigate().back();
}
/**
*测试主页超链接是否正常
*/
@Test
@Order(2)
public void testZguYe(){
driver.findElement(By.xpath("/html/body/div[1]/a[1]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-list.html",url);
driver.navigate().back();
}
/**
* 注销超链接是否正常
*/
@Test
@Order(3)
public void testZhuXiao(){
driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-login.html",url);
driver.navigate().back();
}
}
博客编辑页测试
界面测试
查看页面的元素能否正确展示。
public class InterfaceTest {
public static ChromeDriver driver;
public static ChromeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
driver = new ChromeDriver();
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys("zhangsan");
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys("1234");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
}
@AfterAll
public static void close(){
driver.quit();
}
@Test
public void testEdit(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"editor\"]/div[1]/div[6]"));
Assertions.assertNotNull(webElement);
}
@Test
public void testFaBu(){
String str = driver.findElement(By.xpath("//*[@id=\"submit\"]")).getAttribute("value");
Assertions.assertEquals("发布文章",str);
}
@Test
public void testInputTitle(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"title\"]"));
Assertions.assertNotNull(webElement);
}
}
功能测试
测试能否正确发表文章。
/**
* 测试发表文章是否正常
*/
@Test
public void submit(){
driver.findElement(By.xpath("//*[@id=\"editor\"]/div[1]/div[6]")).sendKeys("自动化测试的流程:");
driver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys("自动化测试");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-list.html",url);
driver.navigate().back();
}
标题为空时,无法发表。
/**
* 标题为空无法发表
*/
@Test
public void submitNull(){
driver.findElement(By.xpath("//*[@id=\"title\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertNotEquals("http://43.143.208.132:8080/blog_system/blog-list.html",url);
driver.navigate().back();
}