博客系统实现自动化测试
创始人
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();}

相关内容

热门资讯

在大吉订购APP订购交易亏损被...   近期,不少投资者反映在“大吉订购App”上投资白银、铂金等商品遭遇严重亏损。该平台宣称提供现货订...
铂银壹号APP商品订购交易亏损...   许多中小投资者误以为网上的“商品现货订购”是合规合法的理财渠道,殊不知这背后隐藏着巨大的合规风险...
兴鑫淘金APP贵金属订购交易亏...   白银、贵金属或其他商品类投资,除国家批准的正规期货交易场所及持牌期货公司外,市面上大量第三方交易...
铂银掘金APP无资质的违规平台...   铂银掘金等平台的投资者,往往是被“注册送现金”“充值返现券”“首单免亏”等广告话术吸引,进而下载...
八方淘金APP订购交易不合规,...   “八方淘金APP”“铂银掘金APP”“铂银壹号APP”等软件被指以“现货订购”为名,实际采用高杠...
在指尖订购APP订购商品亏损被...   指尖订购APP这类平台,表面是“订购交易”,实则是披着电商外衣的非法期货陷阱。绝大多数投资者被蒙...
在犀贝易购APP做商品订购交易...   市面上打着“现货投资”“贵金属交易”旗号的APP并不少,但真正合规、受监管的平台并不多。犀贝易购...
易达淘金APP虚假商品订购交易...   易达淘金APP存在严重的虚假宣传与违规交易行为。该平台打着“商品订购”的旗号,实则通过不合规的交...
大浪淘金APP白银订购亏损被骗...   大浪淘金APP最隐蔽的套路,在于其完全封闭的内盘交易系统。表面上,它宣称与国际贵金属市场接轨;实...
一品好购APP上订购白银出现亏...   一品好购APP原名“一品订购”,运营方为上海宝智未来信息技术有限公司。近期不少投资者反映在该平台...
九州订购APP贵金属订购投资骗...   九州订购APP正通过抖.音、快手、今日头条等平台大量投放投资广告,用“5元起投”“充值返现”“0...
坚锅淘金APP交易软件并不正规...   当前,市面上大量所谓的“现货商城订购”交易软件,早已偏离了正常的实物商品买卖,演变为高杠杆、高手...
斗金订购APP贵金属期货交易骗...   斗金订购APP的投资者往往被铺天盖地的广告宣传所诱导——注册即送现金、充值返现券等看似诱人的福利...
警惕!云启购APP违规经营,披...   "5元起投、充值返现、稳赚不赔"——这些诱人的广告语背后,可能是一个精心设计的金融陷阱。  近期...
量子淘金APP贵金属投资不合规...   "4元起投""充值返券""月入过万"——这些极具诱惑力的广告语在短视频平台、社交软件上铺天盖地。...
鑫汇智投APP贵金属商品订购骗...   鑫汇智投APP非法期货投资骗局,受害者在投资过程中根本不懂交易,也不去研究和验证这个平台是否正规...
矅光智策APP平台违规经营非法...   矅光智策APP对外宣称是“商城订购”“现货交易”,但查询其经营范围,并未显示具备开展现货期货交易...
黄金珠宝怎么选?内侧刻字都代表... 潮新闻客户端 记者 马焱 通讯员 任倩影 9月8日下午,“云游杭州 乐购金秋”——“浙城·浙放心”活...
原创 冰... 最近全球国债市场正在上演魔幻一幕: 美债、日债、欧债遭遇猛烈抛售,收益率冲到几十年新高,美国财政部都...
日韩股市开盘表现分化 上证报中国证券网讯 9月9日,日韩股市开盘表现分化,韩国综合指数开盘上涨0.26%,报6972.87...