目录
一、设计博客系统的测试用例
二、利用测试用例进行测试
测试登录页面
界面测试
功能测试
测试博客列表页
界面测试
功能测试
测试博客详情页
界面测试
功能测试
博客编辑页测试
界面测试
功能测试
测试的文件放在maven项目的test文件夹下,需要在之前的maven项目中添加一些自动化测试的依赖:
org.seleniumhq.selenium selenium-java 4.0.0 org.junit.jupiter junit-jupiter 5.8.2 test org.junit.platform junit-platform-suite 1.8.2 test org.junit.platform junit-platform-reporting 1.8.2
首先定义start方法和close方法,并利用相关注解使其在测试之前和测试之后都执行一次。
@BeforeAllpublic void start(){driver = getDriver();driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");//使用隐式等待渲染页面完成driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));}@AfterAllpublic 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;}@BeforeAllpublic 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));}@AfterAllpublic static void close(){driver.quit();}/*** 测试登陆文字*/@Testpublic void testDengLu(){String dengLu = driver.findElement(By.xpath("/html/body/div[2]/div/h3")).getText();Assertions.assertEquals(dengLu,"登录");}/*** 测试提交按钮的文字*/@Testpublic void testTiJiao(){String tiJiao = driver.findElement(By.xpath("//*[@id=\"submit\"]")).getAttribute("value");Assertions.assertEquals(tiJiao,"提交");}/*** 测试用户名输入框*/@Testpublic void testUserInput(){WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input"));Assertions.assertNotNull(webElement);}/*** 测试密码输入框*/@Testpublic void testPasswordInput(){WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input"));Assertions.assertNotNull(webElement);}/*** 测试提交按钮*/@Testpublic 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;}@BeforeAllpublic 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();}@AfterAllpublic static void close(){driver.quit();}/*** 测试个人信息*/@Testpublic 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");}/*** 测试查看全文按钮的文字*/@Testpublic void testQuanWen(){String tiJiao = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).getText();Assertions.assertEquals("查看全文",tiJiao);}/*** 测试个人信息的头像是否正常*/@Testpublic void testUserInput(){WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/img"));Assertions.assertNotNull(webElement);}/*** 测试文章标题是否正常*/@Testpublic void testPasswordInput(){WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]"));Assertions.assertNotNull(webElement);}}
查看全文按钮的功能是否正常。
/*** 查看全文按钮是否能正确跳转*/@Testpublic 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();}
写博客按钮是否正常。
/*** 写博客超链接是否正常*/@Testpublic 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();}
测试注销超链接是否正常。
/*** 注销超链接是否正常*/@Testpublic 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;}@BeforeAllpublic 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();}@AfterAllpublic static void close(){driver.quit();}/*** 测试个人信息*/@Testpublic 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);}/*** 测试文章标题*/@Testpublic void testTitle(){String title = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/h3")).getText();Assertions.assertNotNull(title);}/*** 测试文章发表日期*/@Testpublic 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;}@BeforeAllpublic 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();}@AfterAllpublic 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;}@BeforeAllpublic 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();}@AfterAllpublic static void close(){driver.quit();}@Testpublic void testEdit(){WebElement webElement = driver.findElement(By.xpath("//*[@id=\"editor\"]/div[1]/div[6]"));Assertions.assertNotNull(webElement);}@Testpublic void testFaBu(){String str = driver.findElement(By.xpath("//*[@id=\"submit\"]")).getAttribute("value");Assertions.assertEquals("发布文章",str);}@Testpublic void testInputTitle(){WebElement webElement = driver.findElement(By.xpath("//*[@id=\"title\"]"));Assertions.assertNotNull(webElement);}
}
测试能否正确发表文章。
/*** 测试发表文章是否正常*/@Testpublic 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();}
标题为空时,无法发表。
/*** 标题为空无法发表*/@Testpublic 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();}
上一篇:京东售后服务认证考试
下一篇:吕冬梅说的藏药仁青盘嘎怎么样?