博客系统实现自动化测试
创始人
2025-05-30 00:36:56
0

目录

一、设计博客系统的测试用例

二、利用测试用例进行测试 

测试登录页面

界面测试 

功能测试

测试博客列表页 

界面测试

功能测试 

测试博客详情页 

界面测试

功能测试

博客编辑页测试

界面测试

功能测试 


一、设计博客系统的测试用例

二、利用测试用例进行测试 

测试的文件放在maven项目的test文件夹下,需要在之前的maven项目中添加一些自动化测试的依赖:

        org.seleniumhq.seleniumselenium-java4.0.0org.junit.jupiterjunit-jupiter5.8.2testorg.junit.platformjunit-platform-suite1.8.2testorg.junit.platformjunit-platform-reporting1.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();}

相关内容

热门资讯

现货黄金直线跳水,跌破5200... 新闻荐读 1月29日晚,现货黄金白银快速走低,回吐盘中全部涨幅。23:15左右,现货黄金跌破5300...
加拿大拟与多国联合设立国防银行 新华社北京1月31日电 加拿大财政部长商鹏飞1月30日说,加拿大将在未来数月与国际伙伴密切合作,推进...
马斯克大消息!SpaceX申请... 据券商中国,美东时间1月30日,路透社报道,据两位知情人士透露,马斯克旗下SpaceX公司2025年...
澳网:雷巴金娜2-1萨巴伦卡女... 北京时间1月31日,2026赛季网球大满贯澳大利亚公开赛继续进行,在女单决赛中,5号种子雷巴金娜6-...
春节前白酒促销热:“扫码抽黄金... 春节临近,白酒市场再现价格异动。 近日,飞天茅台批价拉升,有酒商直言“年前要冲2000元关口”,引发...
新安县人民医院让专业护理走进千... 由211名专业人员组成的服务团队,提供60项全维度服务,累计完成上门服务3217人次;实现“入院—出...
跨国企业负责人高度肯定中国经济... 本文转自【中国经济网-《经济日报》】; 参观者在第八届中国国际进口博览会美敦力公司一款超硬导丝产品...
中药配方颗粒标准化浪潮:数商云... 在中医药现代化与国际化加速推进的背景下,中药配方颗粒行业正经历一场以标准化为核心的深刻变革。截至20...
长江能科迪拜孙公司完成注册 拓... 来源:新浪财经-鹰眼工作室 【财经网讯】长江三星能源科技股份有限公司(证券代码:920158,证券简...
银行职工因贪污罪获刑后留任,在... 新京报记者 刘锦涵 制作 礼牧周 ▲新京报我们视频出品(ID:wevideo) 近日,农发行福建福鼎...
黄金创40年来最大单日跌幅!金... (来源:劳动报) 转自:劳动报 1月31日,国际金银价格同步大跌,创40余年来最大跌幅。国内金饰价...
“一人公司”近来何以兴起? 2026年开年,“一人公司”发展备受关注。这种新型创业模式正在上海、北京、江苏等地悄然兴起,凭借低成...
寒武纪预计 2025 年净利润... 消息,AI 芯片企业寒武纪今日发布 2025 年年度业绩预告: 经财务部门初步测算,公司预计 2...
和讯投顾徐剑波:ETF买入法! 这轮牛市是机构主导的ETF牛市,选对ETF往往比选股更加赚钱。那么如何投资ETF?今天教给大家一个非...
君乐宝上市申请已递交,国内乳品... 2026年 1月19日,中国领先的综合乳制品企业君乐宝乳业集团股份有限公司正式向香港联交所递交主板上...
大涨!马斯克,突传大消息!重磅... SpaceX的“赚钱能力”曝光。 据最新消息,世界首富埃隆·马斯克旗下的商业航天公司SpaceX去年...
原创 顶... 2025年微博之夜定档于2026年2月5日北京线上直播,这场已经走过二十多年风雨的互联网年度盛典,因...
体检查出肺结节?3个日常行为正... 太原龙城中医医院科普:如今越来越多人在体检中发现肺结节,看到报告上的“阴影”便忧心忡忡。其实研究表明...
记者观察丨美联储下任主席提名揭... 在经过长达一年反复挑选后,美国总统唐纳德·特朗普终于做出决定,提名凯文·沃什为下一任美联储主席,接替...
首饰金,一夜大跌上百元!金价暴... 【导读】多家首饰品牌金价出现大幅下跌 中国基金报记者 忆山 随着国际金价急速下跌,国内首饰金价也迎来...