Java+Selenium自动化测试
测试环境:win10,IEDA2021,Chrome94
测试用例
使用java+selenium进行部分测试用例的测试
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class UserCase extends InitAndEnd{
//登录测试-成功
@Order(1)
@ParameterizedTest
@CsvFileSource(resources = "UserCase.csv")
void LoginSuccess(String username, String password,String list,String nickname) throws InterruptedException {
System.out.println(username + password + list );
//
webDriver.get("url");
sleep(300);
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
sleep(300);
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
sleep(300);
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(300);
String cur_url = webDriver.getCurrentUrl();
sleep(300);
Assertions.assertEquals(list,cur_url);
sleep(300);
sleep(300);
String cur_name = webDriver.findElement(By.cssSelector("#index_nav_nickname")).getText();
Assertions.assertEquals(nickname,cur_name);
}
//登录测试-失败
@ParameterizedTest
void LoginFailed(String username, String password,String list,String nickname) throws InterruptedException {
System.out.println(username + password + list );
//
webDriver.get("url");
sleep(300);
webDriver.findElement(By.cssSelector("#username")).sendKeys("username");
sleep(300);
webDriver.findElement(By.cssSelector("#password")).sendKeys("password");
sleep(300);
webDriver.findElement(By.cssSelector("#submit")).click();
sleep(300);
String cur_url = webDriver.getCurrentUrl();
sleep(300);
Assertions.assertEquals(list,cur_url);
sleep(300);
sleep(300);
String cur_name = webDriver.findElement(By.cssSelector("#index_nav_nickname")).getText();
Assertions.assertEquals(nickname,cur_name);
}
//获取个人帖子
@Order(4)
@Test
void MyArticleList() {
// 获取页面上 我的帖子 对应的元素
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//点击头像
webDriver.findElement(By.cssSelector("#index_nav_avatar")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//点击 我的帖子
webDriver.findElement(By.cssSelector("#index_user_profile")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//获得数量
int title_num = webDriver.findElements(By.cssSelector("#profile_articleCount")).size();
// 如果元素数量不为0,测试通过
Assertions.assertNotEquals(0 ,title_num);
}
//发帖子测试
@Order(2)
@Test
void createArticle() throws InterruptedException {
webDriver.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();
sleep(300);
webDriver.findElement(By.cssSelector("#article_post_title")).sendKeys("自动化测试");
sleep(300);
((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"article_post_content\").value=\"自动化测试内容\"");
// webDriver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span")).sendKeys("自动化测试内容");
// webDriver.findElement(By.id("article_post_content")).sendKeys("自动化测试内容");
sleep(300);
sleep(300);
System.out.println("测试自动化11111");
webDriver.findElement(By.cssSelector("#article_post_submit")).click();
sleep(300);
sleep(300);
String currentUrl = webDriver.getCurrentUrl();
sleep(300);
Assertions.assertEquals("url",currentUrl);
}
@Order(3)
@Test
//校验已发布帖子信息,点赞功能、回复功能测试
void articleinfocheck(){
webDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
String articlename = webDriver.findElement(By.cssSelector("#article_details_author_name")).getText();
System.out.println(articlename+"22222222222");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
Assertions.assertEquals("壮壮",articlename);
String articletime = webDriver.findElement(By.cssSelector("#details_article_createTime")).getText();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
System.out.println(articletime);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
webDriver.findElement(By.cssSelector("#details_btn_like_count")).click();
}
//校验退出登录
@Order(5)
@Test
void logout() throws InterruptedException {
sleep(300);
webDriver.findElement(By.cssSelector("#index_nav_avatar")).click();
sleep(300);
webDriver.findElement(By.cssSelector("#index_user_logout")).click();
sleep(300);
String currentUrl = webDriver.getCurrentUrl();
System.out.println(currentUrl);
// Assertions.assertEquals("http://47.113.188.104:58080/sign-in.html",currentUrl);
String text = webDriver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")).getText();
Assertions.assertEquals("用户登录",text);
}
}
测试结果: