目录
前言
一、测试环境
二、环境部署
三、测试用例
四、执行测试
4.1、公共类设计
创建浏览器驱动对象
测试套件
释放驱动类
4.2、功能测试
注册页面
登录页面
版块
帖子
用户个人中心页
站内信
4.3、界面测试
注册页面
登录页面
版块
帖子
用户个人中心页
站内信
前言
个人论坛系统是一种在线社交平台,为用户提供了丰富的功能,让他们能够轻松地创建帖子、分享信息、讨论话题以及互动交流。
在开发阶段,实时进行了单元测试,所以本文章主要进行自动化测试(功能、界面),分为设计测试用例和进行测试两方面。
一、测试环境
操作系统:Window11专业版
项目运行环境:IntelliJ IDEA 2022.3.3、JDK1.8、MySQL5.7
浏览器:Chrome、Microsoft Edge
浏览器驱动:ChromeDriver、EdgeDriver
web自动化测试工具:selenium4
单元测试框架:Junit5
测试技术:主要进行功能与界面方面的自动化测试
二、环境部署
在pom.xml中部署
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter-test</artifactId><version>3.0.0</version><scope>test</scope><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>4.10.0</version></dependency><!--保存屏幕截图文件所要用到的包 --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>1.4</version></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.8.2</version><scope>test</scope></dependency><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-suite</artifactId><version>1.8.2</version><scope>test</scope></dependency>
三、测试用例
四、执行测试
4.1、公共类设计
创建浏览器驱动对象
为了避免在每次测试时都重复创建浏览器驱动对象,浪费资源和时间,我们可以考虑创建一个公共类来统一管理浏览器驱动对象的创建。
public class AutotestUtils {public static ChromeDriver chromeDriver;public static EdgeDriver edgeDriver;//创建驱动对象public static ChromeDriver createChromeDriver(){//驱动创建好了/没有创建if (chromeDriver==null){ChromeOptions options=new ChromeOptions();options.addArguments("--remote-allow-origins=*");chromeDriver=new ChromeDriver(options);//隐式等待Duration duration=Duration.ofSeconds(10);long millis= duration.toMillis();chromeDriver.manage().timeouts().implicitlyWait(millis, TimeUnit.MILLISECONDS);}return chromeDriver;}public static EdgeDriver createEdgeDriver(){//驱动创建好了/没有创建if (edgeDriver==null){EdgeOptions options=new EdgeOptions();options.addArguments("--remote-allow-origins=*");edgeDriver=new EdgeDriver(options);//隐式等待Duration duration=Duration.ofSeconds(10);long millis= duration.toMillis();edgeDriver.manage().timeouts().implicitlyWait(millis,TimeUnit.MILLISECONDS);}return edgeDriver;}
}
测试套件
使用suite测试套件来进行测试,指定类来运行用例
@Suite
@SelectClasses({driverQuitTest.class})
public class runSuite {
}
释放驱动类
单独写一个释放驱动类
public class driverQuitTest extends AutotestUtils {public static ChromeDriver chromeDriver;public static EdgeDriver edgeDriver;@Teststatic void driverQuit(){if (chromeDriver!=null){chromeDriver.quit();} else if (edgeDriver!=null) {edgeDriver.quit();}else {chromeDriver.quit();edgeDriver.quit();}}
}
4.2、功能测试
注册页面
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-up.html");}
测试空的用户名能否正常注册
/*** 测试空的用户名* @param username* @param nickname* @param password* @param passwordRepeat* @throws InterruptedException*/@Order(1)@ParameterizedTest@CsvSource({",baekhyun,1992,1992"})public void testRegisterNullUsername(String username,String nickname,String password,String passwordRepeat) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#nickname")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#passwordRepeat")).clear();if (nickname!=null){chromeDriver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);}if (password!=null){chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);}if (passwordRepeat!=null){chromeDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);}chromeDriver.findElement(By.cssSelector("#policy")).click();chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="用户名不能为空";String actual=chromeDriver.findElement(By.xpath("//*[@id=\"signUpForm\"]/div/div[1]/div")).getText();Assertions.assertEquals(except,actual);Thread.sleep(1000);//复原勾选框chromeDriver.findElement(By.cssSelector("#policy")).click();}
测试空的昵称能否正常注册
/*** 测试空的昵称* @param username* @param nickname* @param password* @param passwordRepeat*/@Order(2)@ParameterizedTest@CsvSource({"baekhyun,,1992,1992"})public void testRegisterNullNickname(String username,String nickname,String password,String passwordRepeat) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#nickname")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#passwordRepeat")).clear();if (username!=null){chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);}if (password!=null){chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);}if (passwordRepeat!=null){chromeDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);}chromeDriver.findElement(By.cssSelector("#policy")).click();chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="昵称不能为空";String actual=chromeDriver.findElement(By.xpath("//*[@id=\"signUpForm\"]/div/div[2]/div")).getText();Assertions.assertEquals(except,actual);Thread.sleep(1000);//复原勾选框chromeDriver.findElement(By.cssSelector("#policy")).click();}
测试空的密码能否正常注册
/*** 测试空的密码* @param username* @param nickname* @param password* @param passwordRepeat* @throws InterruptedException*/@Order(3)@ParameterizedTest@CsvSource({"baekhyun,baekhyun,,1992"})public void testRegisterNullPassword(String username,String nickname,String password,String passwordRepeat) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#nickname")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#passwordRepeat")).clear();if (username!=null){chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);}if (nickname!=null){chromeDriver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);}if (passwordRepeat!=null){chromeDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);}chromeDriver.findElement(By.cssSelector("#policy")).click();chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="密码不能为空";String actual=chromeDriver.findElement(By.xpath("//*[@id=\"signUpForm\"]/div/div[3]/div/div")).getText();Assertions.assertEquals(except,actual);Thread.sleep(1000);//复原勾选框chromeDriver.findElement(By.cssSelector("#policy")).click();}
测试空的确认密码能否正常注册
/*** 测试空的确认密码* @param username* @param nickname* @param password* @param passwordRepeat* @throws InterruptedException*/@Order(4)@ParameterizedTest@CsvSource({"baekhyun,baekhyun,1992,"})public void testRegisterNullPasswordRepeat(String username,String nickname,String password,String passwordRepeat) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#nickname")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#passwordRepeat")).clear();if (username!=null){chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);}if (nickname!=null){chromeDriver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);}if (password!=null){chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);}chromeDriver.findElement(By.cssSelector("#policy")).click();chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="请检查确认密码";String actual=chromeDriver.findElement(By.xpath("//*[@id=\"signUpForm\"]/div/div[4]/div/div")).getText();Assertions.assertEquals(except,actual);Thread.sleep(1000);//复原勾选框chromeDriver.findElement(By.cssSelector("#policy")).click();}
测试密码和确认密码不一致时能否正常注册
/*** 测试密码和确认密码不一致* @param username* @param nickname* @param password* @param passwordRepeat* @throws InterruptedException*/@Order(5)@ParameterizedTest@CsvSource({"baekhyun,baekhyun,1992,1993"})public void testRegisterNullSame(String username,String nickname,String password,String passwordRepeat) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#nickname")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#passwordRepeat")).clear();if (username!=null){chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);}if (nickname!=null){chromeDriver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);}if (password!=null){chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);}if (passwordRepeat!=null){chromeDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);}chromeDriver.findElement(By.cssSelector("#policy")).click();chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="请检查确认密码";String actual=chromeDriver.findElement(By.xpath("//*[@id=\"signUpForm\"]/div/div[4]/div/div")).getText();Assertions.assertEquals(except,actual);Thread.sleep(1000);//复原勾选框chromeDriver.findElement(By.cssSelector("#policy")).click();}
测试用户名已注册时能否正常注册
/*** 测试用户名已存在* @param username* @param nickname* @param password* @param passwordRepeat* @throws InterruptedException*/@Order(6)@ParameterizedTest@CsvSource({"baekhyun,baekhyun,1992,1992"})public void testRegisterNullExistUsername(String username,String nickname,String password,String passwordRepeat) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#nickname")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#passwordRepeat")).clear();if (username!=null){chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);}if (nickname!=null){chromeDriver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);}if (password!=null){chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);}if (passwordRepeat!=null){chromeDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);}chromeDriver.findElement(By.cssSelector("#policy")).click();chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String exp_url="http://49.232.185.203:58080/sign-up.html";String cur_url=chromeDriver.getCurrentUrl();Assertions.assertEquals(exp_url,cur_url);Thread.sleep(1000);//复原勾选框chromeDriver.findElement(By.cssSelector("#policy")).click();Thread.sleep(1000);}
测试没有点击同意协议能否正常注册
/*** 测试没有点击同意协议* @param username* @param nickname* @param password* @param passwordRepeat* @throws InterruptedException*/@Order(7)@ParameterizedTest@CsvSource({"do,do,1993,1993"})public void testRegisterNullPolicy(String username,String nickname,String password,String passwordRepeat) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#nickname")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#passwordRepeat")).clear();if (username!=null){chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);}if (nickname!=null){chromeDriver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);}if (password!=null){chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);}if (passwordRepeat!=null){chromeDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);}chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String exp_url="http://49.232.185.203:58080/sign-up.html";String cur_url=chromeDriver.getCurrentUrl();Assertions.assertEquals(exp_url,cur_url);Thread.sleep(1000);}
测试输入正确的用户名、昵称、密码、确认密码能否正常注册
/*** 正确的用户名、昵称、密码、确认密码* @param username* @param nickname* @param password* @param passwordRepeat* @throws InterruptedException*/@Order(8)@ParameterizedTest@CsvSource({"sehun,sehun,1994,1994"})public void testRegisterSuccess(String username,String nickname,String password,String passwordRepeat) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#nickname")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#passwordRepeat")).clear();if (username!=null){chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);}if (nickname!=null){chromeDriver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);}if (password!=null){chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);}if (passwordRepeat!=null){chromeDriver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);}chromeDriver.findElement(By.cssSelector("#policy")).click();chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(3000);String exp_url="http://49.232.185.203:58080/sign-in.html";String cur_url=chromeDriver.getCurrentUrl();Assertions.assertEquals(exp_url,cur_url);}
测试结果:
登录页面
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-in.html");}
测试输入错误的用户名和密码
/*** 测试错误的用户名或密码* @param username* @param password* @throws InterruptedException*/@Order(1)@ParameterizedTest@CsvSource({"baekhyun,1999","dd,1993"})void testLoginErrorUser(String username,String password) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String exp_url="http://49.232.185.203:58080/sign-in.html";String cur_url=chromeDriver.getCurrentUrl();Assertions.assertEquals(exp_url,cur_url);Thread.sleep(1000);}
测试只输入用户名
/*** 测试空的用户名* @param username* @param password* @throws InterruptedException*/@Order(2)@ParameterizedTest@CsvSource({",1992",",1993"})void testLoginNullUsername(String username,String password) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();if (password!=null){chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);}chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="用户名不能为空";String actual=chromeDriver.findElement(By.xpath("//*[@id=\"signInForm\"]/div[1]/div")).getText();Assertions.assertEquals(except,actual);Thread.sleep(1000);}
测试只输入密码
/*** 测试空的密码* @param username* @param password* @throws InterruptedException*/@Order(3)@ParameterizedTest@CsvSource({"baekhyun,","do,"})void testLoginNullPassword(String username,String password) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();if (username!=null){chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);}chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="密码不能为空";String actual=chromeDriver.findElement(By.xpath("//*[@id=\"signInForm\"]/div[2]/div/div")).getText();Assertions.assertEquals(except,actual);Thread.sleep(1000);}
测试输入正确的用户名和密码
/*** 测试正确的用户名和密码* @param username* @param password* @throws InterruptedException*/@Order(4)@ParameterizedTest@CsvSource({"baekhyun,1992","do,1993"})void testLoginSuccess(String username,String password) throws InterruptedException {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String exp_url="http://49.232.185.203:58080/index.html";String cur_url=chromeDriver.getCurrentUrl();Assertions.assertEquals(exp_url,cur_url);chromeDriver.navigate().back();Thread.sleep(1000);}
测试结果:
版块
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-in.html");chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("baekhyun");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");chromeDriver.findElement(By.cssSelector("#submit")).click();}
测试是否显示所有帖子信息
/*** 测试是否显示板块下所有帖子的信息*/@Order(1)@Testvoid testDisplayALLArticle() throws InterruptedException {Thread.sleep(1000);String exp1=chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).getText();Assertions.assertEquals("招聘开发岗位",exp1);String exp2=chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > div > div.col > div.text-truncate > a > strong")).getText();Assertions.assertEquals("测试测试333",exp2);String exp3=chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(3) > div > div.col > div.text-truncate > a > strong")).getText();Assertions.assertEquals("测试测试222",exp3);String exp4=chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(4) > div > div.col > div.text-truncate > a > strong")).getText();Assertions.assertEquals("测试测试111",exp4);}
测试右上角用户信息
/*** 测试右上角用户信息*/@Order(2)@Testvoid testUserInfo() throws InterruptedException {Thread.sleep(1000);WebElement userinfo=chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown"));Assertions.assertNotNull(userinfo);}
测试站内信按钮
/*** 测试站内信按钮*/@Order(3)@Testvoid testDisplayMail() throws InterruptedException {Thread.sleep(1000);WebElement mail=chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div"));Assertions.assertNotNull(mail);}
测试点击用户头像是否出现下拉框信息
/*** 测试点击用户头像是否出现下拉框*/@Order(4)@Testvoid testDisplayDownList() throws InterruptedException {Thread.sleep(1000);//首先得点击下拉框chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();String exp1=chromeDriver.findElement(By.xpath("//*[@id=\"index_user_profile\"]")).getText();Assertions.assertEquals("我的帖子",exp1);String exp2=chromeDriver.findElement(By.xpath("//*[@id=\"index_user_settings\"]")).getText();Assertions.assertEquals("个人中心",exp2);String quit=chromeDriver.findElement(By.xpath("//*[@id=\"index_user_logout\"]")).getText();Assertions.assertEquals("退出",quit);chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();}
测试能否跳转到详情页
/*** 测试能否跳转到详情页*/@Order(5)@Testvoid testArticleDetail() throws InterruptedException {chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();String except="招聘开发岗位测试";String actual=chromeDriver.findElement(By.xpath("//*[@id=\"details_article_content\"]/p")).getText();Thread.sleep(1000);Assertions.assertEquals(except,actual);chromeDriver.findElement(By.cssSelector("#nav_board_index > a")).click();}
测试能否正确跳转到帖子编辑页
/*** 测试发布帖子能否正确跳到帖子编辑页*/@Order(6)@Testvoid Publish1() throws InterruptedException {Thread.sleep(2000);chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();Thread.sleep(3000);String text=chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]")).getText();Assertions.assertEquals("发新贴",text);chromeDriver.findElement(By.cssSelector("#nav_board_index > a")).click();}
测试点击版块能否跳转到版块下的所有帖子
/*** 测试点击版块能否跳到板块下所有的帖子*/@Order(7)@Testvoid testBoardInfo() throws InterruptedException {Thread.sleep(1000);chromeDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]")).click();Thread.sleep(2000);String text=chromeDriver.findElement(By.xpath("//*[@id=\"article_list_board_title\"]")).getText();Assertions.assertEquals("Java",text);String exp1=chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).getText();Assertions.assertEquals("测试测试222",exp1);String exp2=chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > div > div.col > div.text-truncate > a > strong")).getText();Assertions.assertEquals("测试测试111",exp2);}
测试退出按钮能否跳转到登录页面
/*** 测试退出按钮*/@Order(8)@Testvoid testQuit() throws InterruptedException {//首先得点击下拉框chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();chromeDriver.findElement(By.xpath("//*[@id=\"index_user_logout\"]")).click();Thread.sleep(1000);String exp_url="http://49.232.185.203:58080/sign-in.html";String actual_url=chromeDriver.getCurrentUrl();Assertions.assertEquals(exp_url,actual_url);}
测试结果:
帖子
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-in.html");chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("baekhyun");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");chromeDriver.findElement(By.cssSelector("#submit")).click();}
测试正确发布帖子
/*** 测试发布新帖* @throws InterruptedException*/@Order(1)@Testvoid Publish1() throws InterruptedException {chromeDriver.findElement(By.xpath("//*[@id=\"nav_board_index\"]/a/span[2]")).click();chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();chromeDriver.findElement(By.cssSelector("#article_post_title")).sendKeys("自动化测试标题");chromeDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);chromeDriver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")).sendKeys("自动化测试内容");chromeDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);chromeDriver.findElement(By.cssSelector("#article_post_submit")).click();Thread.sleep(1000);String text = chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).getText();Assertions.assertEquals("自动化测试标题",text);}
测试能否正确跳转到帖子详情页
/*** 测试能否跳转到详情页*/@Order(1)@Testvoid testArticleDetail() throws InterruptedException {chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();String except="招聘开发岗位测试";String actual=chromeDriver.findElement(By.xpath("//*[@id=\"details_article_content\"]/p")).getText();Thread.sleep(1000);Assertions.assertEquals(except,actual);}
测试能否显示帖子的所有信息和用户信息
/*** 测试是否显示帖子的所有信息和用户信息*/@Order(2)@Testvoid testDisplayArticle_userInfo() throws InterruptedException {//帖子作者String article_username="do";String username=chromeDriver.findElement(By.xpath("//*[@id=\"article_details_author_name\"]")).getText();//帖子标题String article_title="招聘开发岗位";String title=chromeDriver.findElement(By.xpath("//*[@id=\"details_article_title\"]")).getText();//帖子内容String article_content="招聘开发岗位测试";String content=chromeDriver.findElement(By.xpath("//*[@id=\"details_article_content\"]/p")).getText();Thread.sleep(1000);Assertions.assertEquals(article_username,username);Assertions.assertEquals(article_title,title);Assertions.assertEquals(article_content,content);}
测试能否显示帖子的创建时间、点赞数、浏览量、回复数
/*** 测试是否显示帖子的创建时间、点赞数、浏览量、回复数*/@Order(3)@Testvoid testDisplayarticleInfo(){//帖子创建时间String ex_createTime="2024-05-28 12:08:57";String createTime=chromeDriver.findElement(By.xpath("//*[@id=\"details_article_createTime\"]")).getText();Assertions.assertEquals(ex_createTime,createTime);//点赞数WebElement like_count_nav=chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3) > svg"));Assertions.assertNotNull(like_count_nav);//浏览量WebElement browse_nav=chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2) > svg"));Assertions.assertNotNull(browse_nav);//回复数WebElement reply_nav=chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(4) > svg"));Assertions.assertNotNull(reply_nav);}
测试用户不是作者,是否会显示点赞选项
/*** 测试如果不是作者,是否出现点赞按钮*/@Order(4)@Testvoid testuserNotArticleUser(){//不是作者的情况下//点赞按钮WebElement like_nav=chromeDriver.findElement(By.cssSelector("#details_btn_like_count > svg"));Assertions.assertNotNull(like_nav);}
测试用户是作者,是否显示点赞、编辑、删除选项
/*** 测试如果是作者,是否显示点赞、编辑、删除按钮*/@Order(5)@Testvoid testuserIsArticleUser(){//是作者的情况下,需先退出当前帖子详情chromeDriver.findElement(By.cssSelector("#nav_board_index > a")).click();//进入作者为别人的帖子详情页chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(2) > div > div.col > div.text-truncate > a > strong")).click();//点赞按钮WebElement like_nav=chromeDriver.findElement(By.cssSelector("#details_btn_like_count > svg"));Assertions.assertNotNull(like_nav);//编辑按钮WebElement edit_nav=chromeDriver.findElement(By.cssSelector("#details_artile_edit > svg"));Assertions.assertNotNull(edit_nav);//删除按钮WebElement del_nav=chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a"));Assertions.assertNotNull(del_nav);}
测试结果:
用户个人中心页
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-in.html");chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("do");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1993");chromeDriver.findElement(By.cssSelector("#submit")).click();}
测试能否正确显示当前用户下的所有帖子
/*** 测试能否正确显示当前用户下的所有帖子*/@Order(1)@Testvoid testDisplayUserAllArticle() throws InterruptedException {Thread.sleep(1000);//首先得点击下拉框chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();chromeDriver.findElement(By.xpath("//*[@id=\"index_user_profile\"]")).click();String article_title=chromeDriver.findElement(By.xpath("//*[@id=\"profile_article_body\"]/li/div[2]/div/div/div/div/div[1]/a/strong")).getText();Assertions.assertEquals("招聘开发岗位",article_title);}
测试能否正确显示发帖数、邮箱、注册日期、个人介绍
/*** 测试能否正确显示发帖数、邮箱、注册日期、个人介绍*/@Order(2)@Testvoid testUserInfo(){WebElement articlecount_nav=chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div[2]/div/div[1]"));Assertions.assertNotNull(articlecount_nav);WebElement email_nav=chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div[2]/div/div[2]"));Assertions.assertNotNull(email_nav);WebElement createtime_nav=chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div[2]/div/div[3]"));Assertions.assertNotNull(createtime_nav);WebElement introduce_nav=chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[2]/div/div/div[2]/div/div"));Assertions.assertNotNull(introduce_nav);}
测试能否正确显示他人的帖子信息和用户信息
/*** 测试能否正确显示他人的帖子信息或用户信息*/@Order(3)@Testvoid testOtherUserInfo(){//点击首页chromeDriver.findElement(By.cssSelector("#nav_board_index > a")).click();//点击别人文章chromeDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[2]/div/div[2]/div[1]/a/strong")).click();//点击别人的名称进入详情页chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[2]/div/div/div[1]/div[1]/div/h3")).click();WebElement articlecount_nav=chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div[2]/div/div[1]"));Assertions.assertNotNull(articlecount_nav);WebElement email_nav=chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div[2]/div/div[2]"));Assertions.assertNotNull(email_nav);WebElement createtime_nav=chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div[2]/div/div[3]"));Assertions.assertNotNull(createtime_nav);WebElement introduce_nav=chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[2]/div/div/div[2]/div/div"));Assertions.assertNotNull(introduce_nav);}
测试修改昵称、电子邮箱、电话号码
/*** 测试修改昵称、邮箱地址、电话号码*/@Order(4)@Testvoid testModifyUserInfo() throws InterruptedException {Thread.sleep(1000);//首先得点击下拉框chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();//点击用户中心chromeDriver.findElement(By.xpath("//*[@id=\"index_user_settings\"]")).click();Thread.sleep(1000);//修改昵称chromeDriver.findElement(By.xpath("//*[@id=\"setting_input_nickname\"]")).clear();chromeDriver.findElement(By.xpath("//*[@id=\"setting_input_nickname\"]")).sendKeys("dodo");chromeDriver.findElement(By.xpath("//*[@id=\"setting_submit_nickname\"]")).click();Thread.sleep(1000);//修改邮箱地址chromeDriver.findElement(By.xpath("//*[@id=\"setting_input_email\"]")).clear();chromeDriver.findElement(By.xpath("//*[@id=\"setting_input_email\"]")).sendKeys("19933@qq.com");chromeDriver.findElement(By.xpath("//*[@id=\"setting_submit_email\"]")).click();Thread.sleep(1000);//修改电话号码chromeDriver.findElement(By.xpath("//*[@id=\"setting_input_phoneNum\"]")).clear();chromeDriver.findElement(By.xpath("//*[@id=\"setting_input_phoneNum\"]")).sendKeys("12727178333");Thread.sleep(1000);chromeDriver.findElement(By.xpath("//*[@id=\"setting_submit_phoneNum\"]")).click();Thread.sleep(2000);//退出chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();chromeDriver.findElement(By.xpath("//*[@id=\"index_user_logout\"]")).click();Thread.sleep(1000);//登录chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("do");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(3000);chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();chromeDriver.findElement(By.xpath("//*[@id=\"index_user_settings\"]")).click();Thread.sleep(1000);String nickname=chromeDriver.findElement(By.xpath("//*[@id=\"settings_nickname\"]")).getText();Assertions.assertEquals("dodo",nickname);}
测试修改密码
/*** 测试修改密码*/@Order(5)@Testvoid testModifyPassword() throws InterruptedException {Thread.sleep(1000);chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();//点击用户中心chromeDriver.findElement(By.xpath("//*[@id=\"index_user_settings\"]")).click();Thread.sleep(1000);//填写旧密码chromeDriver.findElement(By.xpath("//*[@id=\"settings_input_oldPassword\"]")).sendKeys("1992");//新密码chromeDriver.findElement(By.xpath("//*[@id=\"settings_input_newPassword\"]")).sendKeys("1993");//确认密码chromeDriver.findElement(By.xpath("//*[@id=\"settings_input_passwordRepeat\"]")).sendKeys("1993");Thread.sleep(2000);chromeDriver.findElement(By.xpath("//*[@id=\"settings_submit_password\"]")).click();Thread.sleep(1000);String exp_url="http://49.232.185.203:58080/sign-in.html";String actual_url=chromeDriver.getCurrentUrl();Assertions.assertEquals(exp_url,actual_url);Thread.sleep(1000);chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("do");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1993");chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(3000);String exp_url2="http://49.232.185.203:58080/index.html";String actual_url2=chromeDriver.getCurrentUrl();Assertions.assertEquals(exp_url2,actual_url2);}
测试结果:
站内信
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();public static EdgeDriver edgeDriver=createEdgeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-in.html");chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("do");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1993");chromeDriver.findElement(By.cssSelector("#submit")).click();edgeDriver.get("http://49.232.185.203:58080/sign-in.html");edgeDriver.findElement(By.cssSelector("#username")).clear();edgeDriver.findElement(By.cssSelector("#password")).clear();edgeDriver.findElement(By.cssSelector("#username")).sendKeys("baekhyun");edgeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");edgeDriver.findElement(By.cssSelector("#submit")).click();}
测试能否成功发送站内信
/*** 测试能否成功发送站内信*/@Order(1)@Test()@Transactionalvoid testSendMailSuccess() throws InterruptedException {edgeDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).click();edgeDriver.findElement(By.xpath("//*[@id=\"btn_details_send_message\"]")).click();edgeDriver.findElement(By.xpath("//*[@id=\"index_message_receive_content\"]")).sendKeys("我是baekhyun");edgeDriver.findElement(By.xpath("//*[@id=\"btn_index_send_message\"]")).click();Thread.sleep(1000);String exp_url="http://49.232.185.203:58080/index.html";String actual_url=chromeDriver.getCurrentUrl();Assertions.assertEquals(exp_url,actual_url);Thread.sleep(1000);}
测试能否正确显示站内信
/*** 测试能否正确显示站内信*/@Order(2)@Testvoid testDisplayMail() throws InterruptedException {chromeDriver.navigate().refresh();Thread.sleep(1000);chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div")).click();String receive=chromeDriver.findElement(By.xpath("//*[@id=\"index_div_message_list\"]/div[1]/div/div[2]/div")).getText();Assertions.assertEquals("我是baekhyun",receive);}
测试站内信未读时,前方是否有[未读]标志
/*** 测试站内信未读时,前方是否有[未读]标志*/@Order(3)@Testvoid testUnread() throws InterruptedException {chromeDriver.navigate().refresh();Thread.sleep(1000);//点击站内信chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div")).click();String expect=chromeDriver.findElement(By.xpath("//*[@id=\"index_div_message_list\"]/div[1]/div/div[2]/a/span[1]")).getText();Assertions.assertEquals("[未读]",expect);}
测试站内信已读时,前方是否有[已读]标志
/*** 测试站内信已读时,前方是否有[已读]标志*/@Order(4)@Testvoid testread() throws InterruptedException {chromeDriver.navigate().refresh();Thread.sleep(1000);//点击站内信chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div")).click();//点击要读取的站内信chromeDriver.findElement(By.xpath("//*[@id=\"index_div_message_list\"]/div[1]/div/div[2]/a")).click();//点击退出chromeDriver.findElement(By.xpath("//*[@id=\"index_message_reply_modal\"]/div/div/div[1]/button")).click();Thread.sleep(1000);chromeDriver.navigate().refresh();//点击站内信chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div")).click();String expect=chromeDriver.findElement(By.xpath("//*[@id=\"index_div_message_list\"]/div[1]/div/div[2]/a/span[1]")).getText();Assertions.assertEquals("[已读]",expect);}
测试站内信已回复时,前方是否有[已回复]标志
/*** 测试站内信已回复时,前方是否有[已回复]标志*/@Order(5)@Testvoid testReplied() throws InterruptedException {Thread.sleep(1000);chromeDriver.navigate().refresh();//点击站内信chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div")).click();//点击要回复的站内信chromeDriver.findElement(By.xpath("//*[@id=\"index_div_message_list\"]/div[1]/div/div[2]/a")).click();//点击回复chromeDriver.findElement(By.xpath("//*[@id=\"btn_index_message_reply\"]")).click();Thread.sleep(1000);//输入要回复的内容chromeDriver.findElement(By.xpath("//*[@id=\"index_message_reply_receive_content\"]")).sendKeys("我是do");//点击发送chromeDriver.findElement(By.xpath("//*[@id=\"btn_index_send_message_reply\"]")).click();Thread.sleep(2000);chromeDriver.navigate().refresh();//点击站内信chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div")).click();String expect=chromeDriver.findElement(By.xpath("//*[@id=\"index_div_message_list\"]/div[1]/div/div[2]/a/span[1]")).getText();Assertions.assertEquals("[已回复]",expect);}
测试回复站内信能否成功
/*** 测试回复站内信能否成功*/@Order(6)@Testvoid replySuccess() throws InterruptedException {Thread.sleep(1000);edgeDriver.navigate().refresh();//点击站内信edgeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div")).click();//点击要回复的站内信String text=edgeDriver.findElement(By.xpath("//*[@id=\"index_div_message_list\"]/div[1]/div/div[2]/div")).getText();Assertions.assertEquals("我是do",text);}
测试结果:
4.3、界面测试
注册页面
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-up.html");}
测试用户注册标题是否存在
/*** 测试用户注册标题是否存在*/@Order(1)@Testvoid testRegisterTitle(){String expect_title="用户注册";String actual=chromeDriver.findElement(By.xpath("//*[@id=\"signUpForm\"]/div/h2")).getText();Assertions.assertEquals(expect_title,actual);}
测试用户名输入框是否存在
/*** 测试用户名输入框是否存在*/@Order(2)@Testvoid testUsernameInput(){WebElement userInput=chromeDriver.findElement(By.xpath("//*[@id=\"username\"]"));Assertions.assertNotNull(userInput);}
测试昵称输入框是否存在
/*** 测试昵称输入框是否存在*/@Order(3)@Testvoid testNicknameInput(){WebElement nicknameInput=chromeDriver.findElement(By.xpath("//*[@id=\"nickname\"]"));Assertions.assertNotNull(nicknameInput);}
测试密码输入框是否存在
/*** 测试密码输入框是否存在*/@Order(4)@Testvoid testPasswordInput(){WebElement passwordInput=chromeDriver.findElement(By.xpath("//*[@id=\"password\"]"));Assertions.assertNotNull(passwordInput);}
测试确认密码输入框是否存在
/*** 测试密码确认输入框是否存在*/@Order(5)@Testvoid testPasswordRepeat(){WebElement passwordRepeatInput=chromeDriver.findElement(By.xpath("//*[@id=\"passwordRepeat\"]"));Assertions.assertNotNull(passwordRepeatInput);}
测试注册按钮是否存在
/*** 测试注册按钮是否存在*/@Order(6)@Testvoid testRegisterButton(){WebElement registerButton=chromeDriver.findElement(By.xpath("//*[@id=\"submit\"]"));Assertions.assertNotNull(registerButton);}
测试登录跳转按钮是否存在
/*** 测试登录跳转按钮是否存在*/@Order(7)@Testvoid testLoginJumpButton(){WebElement loginJumpButton=chromeDriver.findElement(By.xpath("/html/body/div/div/div[2]/a"));Assertions.assertNotNull(loginJumpButton);}
测试结果:
登录页面
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-in.html");}
测试用户登录标题是否存在
/*** 测试用户登录标题是否存在*/@Order(1)@Testvoid testLoginTitle(){String login_title="用户登录";String except=chromeDriver.findElement(By.xpath("/html/body/div/div/div/div[1]/div/div[2]/div/h2")).getText();Assertions.assertEquals(login_title,except);}
测试用户名输入框是否存在
/*** 测试用户名输入框是否存在*/@Order(2)@Testvoid usernameInput(){WebElement usernameInput=chromeDriver.findElement(By.xpath("//*[@id=\"username\"]"));Assertions.assertNotNull(usernameInput);}
测试密码输入框是否存在
/*** 测试密码输入框是否存在*/@Order(3)@Testvoid passwordInput(){WebElement passwordInput=chromeDriver.findElement(By.xpath("//*[@id=\"password\"]"));Assertions.assertNotNull(passwordInput);}
测试登录按钮是否存在
/*** 测试登录按钮是否存在*/@Order(4)@Testvoid testLoginButton(){WebElement loginButton=chromeDriver.findElement(By.xpath("//*[@id=\"submit\"]"));Assertions.assertNotNull(loginButton);}
测试注册跳转按钮是否存在
/*** 测试注册跳转按钮是否存在*/@Order(5)@Testvoid testLoginJumpButton(){WebElement loginJumpButton=chromeDriver.findElement(By.xpath("/html/body/div/div/div/div[1]/div/div[3]/a"));Assertions.assertNotNull(loginJumpButton);}
测试结果:
版块
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-in.html");chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("baekhyun");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");chromeDriver.findElement(By.cssSelector("#submit")).click();}
测试导航栏中版块是否存在
/*** 测试导航栏中版块是否存在*/@Order(1)@Testvoid testExistBoard(){//首页WebElement board1=chromeDriver.findElement(By.xpath("//*[@id=\"nav_board_index\"]/a"));Assertions.assertNotNull(board1);//javaWebElement board2=chromeDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a"));Assertions.assertNotNull(board2);//c++WebElement board3=chromeDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a"));Assertions.assertNotNull(board3);//前端技术WebElement board4=chromeDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a"));Assertions.assertNotNull(board4);//MysqlWebElement board5=chromeDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a"));Assertions.assertNotNull(board5);//面试宝典WebElement board6=chromeDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a"));Assertions.assertNotNull(board6);//经验分享WebElement board7=chromeDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a"));Assertions.assertNotNull(board7);//招聘信息WebElement board8=chromeDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a"));Assertions.assertNotNull(board8);//福利待遇WebElement board9=chromeDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a"));Assertions.assertNotNull(board9);//灌水区WebElement board10=chromeDriver.findElement(By.xpath("//*[@id=\"topBoardList\"]/li[2]/a"));Assertions.assertNotNull(board10);}
测试夜间模式按钮是否存在
/*** 测试夜间模式按钮是否存在*/@Order(2)@Testvoid testExistButton(){WebElement button=chromeDriver.findElement(By.xpath("/html/body/div[1]/header[1]/div/div/div[2]/a[1]"));Assertions.assertNotNull(button);}
测试结果:
帖子
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-in.html");chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("baekhyun");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");chromeDriver.findElement(By.cssSelector("#submit")).click();}
测试发布帖子按钮是否存在
/*** 测试发布帖子按钮是否存在*/@Order(1)@Testvoid testPublish(){WebElement publish= chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div[2]/div"));Assertions.assertNotNull(publish);}
测试发私信按钮是否存在
/*** 测试发私信按钮是否存在*/@Order(2)@Testvoid testSendMessage(){chromeDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).click();WebElement button=chromeDriver.findElement(By.xpath("//*[@id=\"btn_details_send_message\"]"));Assertions.assertNotNull(button);}
测试我的帖子按钮是否存在
/*** 测试我的帖子按钮是否存在*/@Order(3)@Testvoid testMyArticle(){//首先得点击下拉框chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();WebElement button=chromeDriver.findElement(By.xpath("//*[@id=\"index_user_profile\"]"));Assertions.assertNotNull(button);}
测试结果:
用户个人中心页
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-in.html");chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("baekhyun");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");chromeDriver.findElement(By.cssSelector("#submit")).click();}
测试昵称输入框是否存在
/*** 测试昵称输入框是否存在*/@Order(1)@Testvoid testNicknameInput(){//首先得点击下拉框chromeDriver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown")).click();chromeDriver.findElement(By.xpath("//*[@id=\"index_user_settings\"]")).click();WebElement nicknameInput=chromeDriver.findElement(By.xpath("//*[@id=\"setting_input_nickname\"]"));Assertions.assertNotNull(nicknameInput);}
测试邮箱地址是否存在
/*** 测试邮箱地址输入框是否存在*/@Order(2)@Testvoid testEmailInput(){WebElement emailInput=chromeDriver.findElement(By.xpath("//*[@id=\"setting_input_email\"]"));Assertions.assertNotNull(emailInput);}
测试电话号码是否存在
/*** 测试电话号码输入框是否存在*/@Order(3)@Testvoid testPhoneNumInput(){WebElement phoneNumInput=chromeDriver.findElement(By.xpath("//*[@id=\"setting_input_phoneNum\"]"));Assertions.assertNotNull(phoneNumInput);}
测试原密码是否存在
/*** 测试原密码输入框是否存在*/@Order(4)@Testvoid testOldPasswordInput(){WebElement oldPasswordInput=chromeDriver.findElement(By.xpath("//*[@id=\"settings_input_oldPassword\"]"));Assertions.assertNotNull(oldPasswordInput);}
测试新密码是否存在
/*** 测试新密码输入框是否存在*/@Order(5)@Testvoid testNewPasswordInput(){WebElement newPasswordInput=chromeDriver.findElement(By.xpath("//*[@id=\"settings_input_newPassword\"]"));Assertions.assertNotNull(newPasswordInput);}
测试确认密码是否存在
/*** 测试确认密码输入框是否存在*/@Order(6)@Testvoid testPasswordRepeatInput(){WebElement passwordRepeatInput=chromeDriver.findElement(By.xpath("//*[@id=\"settings_input_passwordRepeat\"]"));Assertions.assertNotNull(passwordRepeatInput);}
测试提交修改按钮是否存在
/*** 测试提交修改按钮是否存在*/@Order(7)@Testvoid testSubmitInput(){WebElement submitInput=chromeDriver.findElement(By.xpath("//*[@id=\"settings_submit_password\"]"));Assertions.assertNotNull(submitInput);}
测试个人简介标题是否存在
/*** 测试个人简介标题是否存在*/@Order(8)@Testvoid testProfile(){String text=chromeDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[2]/div/div/div/div[2]/div/h3[5]")).getText();Assertions.assertEquals("个人简介",text);}
测试结果:
站内信
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:58080/sign-in.html");chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("baekhyun");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");chromeDriver.findElement(By.cssSelector("#submit")).click();}
测试站内信按钮是否存在
/*** 测试站内信按钮是否存在*/@Order(1)@Testvoid testMailButton(){WebElement mailButton=chromeDriver.findElement(By.xpath("/html/body/div[1]/header[1]/div/div/div[2]/div"));Assertions.assertNotNull(mailButton);}
测试站内信内回复按钮是否存在
/*** 测试回复站内信按钮是否存在*/@Order(2)@Testvoid testReplyButton() throws InterruptedException {chromeDriver.findElement(By.xpath("/html/body/div[1]/header[1]/div/div/div[2]/div")).click();chromeDriver.findElement(By.xpath("//*[@id=\"index_div_message_list\"]/div/div/div[2]/a")).click();Thread.sleep(1000);WebElement replyButton=chromeDriver.findElement(By.xpath("//*[@id=\"btn_index_message_reply\"]"));Assertions.assertNotNull(replyButton);}
测试站内信内取消按钮是否存在
/*** 测试站内信取消按钮是否存在*/@Order(3)@Testvoid testCancellationButton(){WebElement cancellationButton=chromeDriver.findElement(By.xpath("//*[@id=\"index_message_reply_cancel\"]"));Assertions.assertNotNull(cancellationButton);}
测试结果:
测试套件结果:
总结:目前针对论坛系统的测试如上述博客,由于部分用例无法进行自动化测试,所以进行相应的人工测试,且测试通过