文章目录
- 前言
- 一、测试用例设计
- 二、自动化测试
- 1.登录功能
- 2.注册功能
- 3.发布帖子
- 4.删除帖子
- 5.修改帖子
- 6.帖子详情
- 7.个人中心
- 8.通过suite套件总测试
前言
对论坛项目进行测试用例编写,通过 Selenium + JUnit 单元测试框架对项目的功能进行自动化测试。
论坛项目的部分功能:用户注册登录、帖子详情查看、帖子发布、修改、删除、用户个人信息展示和修改。
一、测试用例设计
二、自动化测试
1.登录功能
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LoginTest {
private static ChromeDriver driver;
@BeforeAll
static void setUp() throws InterruptedException {
driver = new ChromeDriver();
driver.get("http://127.0.0.1:58080/sign-in.html");
driver.manage().window().maximize();
Thread.sleep(200);
}
@AfterAll
static void tearDown(){
driver.quit();
}
//正确登录
@Order(1)
@ParameterizedTest
@CsvSource({"test,123","test5,123"})
void rightLogin(String username,String password){
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.id("submit")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
String user = driver.findElement(By.id("article_list_board_title")).getText();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
Assertions.assertEquals(user,"首页");
driver.navigate().back();
}
//错误登录
@Order(2)
@ParameterizedTest
@CsvSource(value = {"t,123", "te,123", "tes,123"})
void falseLogin(String username,String password){
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.id("submit")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
String currentUrl = driver.getCurrentUrl();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
Assertions.assertNotEquals("http://127.0.0.1:58080/index.html",currentUrl);
}
@Test
@Order(3)
void notUnLogin() throws InterruptedException {
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#submit")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
@Test
@Order(4)
void notPwLogin() throws InterruptedException {
driver.findElement(By.cssSelector("#username")).sendKeys("test");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#submit")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
}
2.注册功能
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class RegistTest {
private static ChromeDriver driver;
@BeforeAll
static void setUp(){
driver = new ChromeDriver();
}
@AfterAll
static void tearDown(){
driver.quit();
}
@BeforeEach
void start(){
driver.get("http://127.0.0.1:58080/sign-up.html");
}
//注册功能测试
//正确注册
@ParameterizedTest
@CsvSource(value = {"test10,测试10,123,123","test11,测试11,123,123","test12,测试12,123,123"})
@Order(1)
void rightRegist(String username,String nickname,String password,String passwordRepeat) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#policy")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#submit")).click();
Thread.sleep(1000);
driver.switchTo().alert().accept();
Thread.sleep(1000);
}
//错误注册
//不输入用户名
//不输入密码
//密码与错误密码不一样
//用户名已存在
@ParameterizedTest
@CsvSource(value = {"'',测试13,123,123", "test14,'',123,123","test15,测试15,123,1234","test10,测试10,123,123"})
@Order(2)
void failRegist(String username,String nickname,String password,String passwordRepeat) throws InterruptedException {
driver.findElement(By.cssSelector("#username")).sendKeys(username);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#policy")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#submit")).click();
}
}
3.发布帖子
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class PublishTest {
private static ChromeDriver driver;
@BeforeAll
static void setUp() throws InterruptedException {
driver = new ChromeDriver();
driver.get("http://127.0.0.1:58080/sign-in.html");
driver.manage().window().maximize();
driver.findElement(By.cssSelector("#username")).sendKeys("test");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.id("submit")).click();
Thread.sleep(200);
}
@AfterAll
static void tearDown(){
driver.quit();
}
//发布帖子测试
@Test
@Order(1)
void post() throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div[2]/div/a[1]")).click();
sleep(3000);
String title = driver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div/h2")).getText();
Assertions.assertEquals(title,"发新贴");
driver.switchTo().activeElement().sendKeys("自动化测试正文");
driver.findElement(By.id("article_post_title")).sendKeys("自动化测试标题");
driver.executeScript("window.scrollTo(0,1000)");
sleep(2000);
driver.findElement(By.id("article_post_submit")).click();
}
}
4.删除帖子
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DeleteTest {
private static ChromeDriver driver;
@BeforeAll
static void setUp() throws InterruptedException {
driver = new ChromeDriver();
driver.get("http://127.0.0.1:58080/sign-in.html");
driver.manage().window().maximize();
driver.findElement(By.cssSelector("#username")).sendKeys("test");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.id("submit")).click();
sleep(200);
}
@AfterAll
static void tearDown(){
driver.quit();
}
//帖子删除测试
@Test
@Order(1)
void delete() throws InterruptedException {
//获取第一篇文章时间
sleep(2000);
String articleCreatTime = driver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[2]/div/div[1]/ul/li[2]")).getText();
//点击文章
driver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a")).click();
sleep(1000);
//点击文章删除按钮
driver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[2]/div/div/div[1]/div[2]/div[2]/div/div[3]/div/a")).click();
sleep(1000);
//点击确定删除
driver.findElement(By.id("details_artile_delete")).click();
sleep(1000);
//再此获取第一篇文章的时间,(第一篇文章删除了,这里获取的是第二篇文章)
//时间不一样就是删除成功,时间一样就是删除不成功
String cact = driver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[2]/div/div[1]/ul/li[2]")).getText();
Assertions.assertNotEquals(articleCreatTime,cact);
}
}
5.修改帖子
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class UpdateTest {
private static ChromeDriver driver;
@BeforeAll
static void setUp() throws InterruptedException {
driver = new ChromeDriver();
driver.get("http://127.0.0.1:58080/sign-in.html");
driver.manage().window().maximize();
driver.findElement(By.cssSelector("#username")).sendKeys("test");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.id("submit")).click();
sleep(200);
}
@AfterAll
static void tearDown(){
driver.quit();
}
@Order(1)
@Test
void update() throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a")).click();
driver.findElement(By.id("details_artile_edit")).click();
driver.findElement(By.id("edit_article_title")).clear();
driver.findElement(By.id("edit_article_title")).sendKeys("自动化修改测试标题");
driver.findElement(By.id("edit_article_title")).sendKeys(Keys.TAB,Keys.CONTROL,"x");
driver.switchTo().activeElement().sendKeys("自动化修改测试正文");
driver.executeScript("window.scrollTo(0,1000)");
Thread.sleep(300);
driver.findElement(By.id("edit_article_submit")).click();
String title = driver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).getText();
Assertions.assertEquals(title,"自动化修改测试标题");
}
}
6.帖子详情
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DetailsTest {
private static ChromeDriver driver;
@BeforeAll
static void setUp() throws InterruptedException {
driver = new ChromeDriver();
driver.get("http://127.0.0.1:58080/sign-in.html");
driver.manage().window().maximize();
driver.findElement(By.cssSelector("#username")).sendKeys("test");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.id("submit")).click();
sleep(200);
}
@AfterAll
static void tearDown(){
driver.quit();
}
@Test
void details() throws InterruptedException {
//文章主页显示标题
String title = driver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).getText();
//文章作者
String author = driver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[2]/div/div[1]/ul/li[1]")).getText();
//文章时间
String time = driver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[2]/div/div[1]/ul/li[2]")).getText();
//文章访问量
Integer numOfVisit = Integer.valueOf(driver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[2]/div/div[2]/ul/li[1]")).getText());
Thread.sleep(200);
driver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
//文章详情页的标题
String checkTitle = driver.findElement(By.cssSelector("#details_article_title")).getText();
//作者
String checkAuthor = driver.findElement(By.cssSelector("#article_details_author_name")).getText();
//时间
String checkTime = driver.findElement(By.cssSelector("#details_article_createTime")).getText();
//访问量
Integer checkNumOfVisit = Integer.valueOf(driver.findElement(By.cssSelector("#details_article_visitCount")).getText());
//检查
Assertions.assertEquals(title,checkTitle);
Assertions.assertEquals(author,checkAuthor);
Assertions.assertEquals(time,checkTime);
//因为点击进入文章,文章的访问量就会加一
Assertions.assertEquals(numOfVisit+1,checkNumOfVisit);
//获取文章点赞数
Integer thumbsUpNum = Integer.valueOf(driver.findElement(By.cssSelector("#details_article_likeCount")).getText());
Thread.sleep(200);
//点赞
driver.findElement(By.cssSelector("#details_btn_like_count")).click();
//点赞后文章的点赞数
Integer checkThumbsUpNum = Integer.valueOf(driver.findElement(By.cssSelector("#details_article_likeCount")).getText());
Assertions.assertEquals(thumbsUpNum+1,checkThumbsUpNum);
}
}
7.个人中心
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class CenterTest {
private static ChromeDriver driver;
@BeforeAll
static void setUp() throws InterruptedException {
driver = new ChromeDriver();
driver.get("http://127.0.0.1:58080/sign-in.html");
driver.manage().window().maximize();
driver.findElement(By.cssSelector("#username")).sendKeys("test");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.findElement(By.id("submit")).click();
sleep(200);
driver.findElement(By.cssSelector("#index_nav_nickname")).click();
driver.findElement(By.cssSelector("#index_user_settings")).click();
}
@AfterAll
static void tearDown(){
driver.quit();
}
@Order(1)
@Test
void center() throws InterruptedException {
driver.findElement(By.cssSelector("#setting_input_nickname")).clear();
driver.findElement(By.cssSelector("#setting_input_nickname")).sendKeys("101");
driver.findElement(By.cssSelector("#setting_submit_nickname")).click();
driver.findElement(By.cssSelector("#setting_input_email")).clear();
driver.findElement(By.cssSelector("#setting_input_email")).sendKeys("111");
driver.findElement(By.cssSelector("#setting_submit_email")).click();
driver.executeScript("window.scrollTo(0,1000)");
driver.findElement(By.id("setting_input_phoneNum")).clear();
driver.findElement(By.id("setting_input_phoneNum")).sendKeys("188");
driver.findElement(By.id("setting_submit_phoneNum")).click();
Thread.sleep(1500);
driver.findElement(By.id("settings_textarea_remark")).clear();
driver.findElement(By.id("settings_textarea_remark")).sendKeys("自动化测试1");
Thread.sleep(1000);
driver.findElement(By.id("settings_submit_remark")).click();
}
@ParameterizedTest
@CsvSource(value = {"'',1234,1234","123,'',1234","123,1234,''"})
@Order(2)
void falseUpdatePassword(String oldPassword,String newPassword,String passwordRepeat) throws InterruptedException {
driver.executeScript("window.scrollTo(0,1000)");
sleep(1500);
// 原密码、新原密码、确认密码都为空
driver.findElement(By.id("settings_input_oldPassword")).clear();
driver.findElement(By.id("settings_input_oldPassword")).sendKeys(oldPassword);
driver.findElement(By.id("settings_input_newPassword")).clear();
driver.findElement(By.id("settings_input_newPassword")).sendKeys(newPassword);
driver.findElement(By.id("settings_input_passwordRepeat")).clear();
driver.findElement(By.id("settings_input_passwordRepeat")).sendKeys(passwordRepeat);
driver.findElement(By.id("settings_submit_password")).click();
}
@ParameterizedTest
@CsvSource(value = {"123,1234,1234"})
@Order(3)
void rightUpdatePassword(String oldPassword,String newPassword,String passwordRepeat) throws InterruptedException {
sleep(1000);
driver.findElement(By.id("settings_input_oldPassword")).clear();
driver.findElement(By.id("settings_input_oldPassword")).sendKeys(oldPassword);
driver.findElement(By.id("settings_input_newPassword")).clear();
driver.findElement(By.id("settings_input_newPassword")).sendKeys(newPassword);
driver.findElement(By.id("settings_input_passwordRepeat")).clear();
driver.findElement(By.id("settings_input_passwordRepeat")).sendKeys(passwordRepeat);
sleep(1500);
driver.findElement(By.id("settings_submit_password")).click();
}
}
8.通过suite套件总测试
@Suite
@SelectClasses(value = {LoginTest.class,RegistTest.class,PublishTest.class,DeleteTest.class,UpdateTest.class,DetailsTest.class,CenterTest.class})
public class RunAllSuite {
}