项目简介:以web网站为基础搭建的一个知识分享社区平台,用户可以发布帖子分享知识,也可以察看别的用户发布的内容进行交流学习。用户可以对自己发布的文章进行修改、删除;也可以对自己的账号进行注销等操作。
对应技术::MySQL、servIet、CSS、HTML、JavaScipct
测试用例:
使用Selenium进行web自动化测试:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.9.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>
接下来针对登录页、列表页进行
自动化测试、主页进行测试
代码结构设计:因为每个测试用例都需要驱动,我们把它封装成一个类,让其他类进行继承即可
package BLOG;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class InitAndEnd {
static WebDriver webDriver;
@BeforeAll
static void SetUp(){
webDriver=new ChromeDriver();
}
//退出浏览器
@AfterAll
static void TearDown(){
webDriver.quit();
}
}
登录页面自动化测试:
public class BlogCases extends InitAndEnd{
// 输入正确的账号密码登陆成功
@ParameterizedTest
@CsvFileSource(resources = "LoginSuccess.csv")
void LoginSuccess(String username,String password,String blog_list_url) {
//打开博客登录页面
webDriver.get("http://127.0.0.1:9090/login.html");
//输入账号dwj
webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//输入密码123
webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//点击提交
webDriver.findElement(By.cssSelector("#submit")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//跳转到列表页
//获取到当前页面的url
String cur_url=webDriver.getCurrentUrl();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//如果url=http://127.0.0.1:9090/myblog_list.html测试通过,否则不通过
Assertions.assertEquals(blog_list_url,cur_url);
}
帖子标题不为空:
@Test
void BlogDetail(){
//打开列表页
//webDriver.get("http://127.0.0.1:9090/myblog_list.html");
//停3秒
// webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//点击查看全文
webDriver.findElement(By.xpath("//*[@id=\"artlist\"]/div[1]/a[1]")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//获取博客标题
String t=webDriver.findElement(By.xpath("//*[@id=\"title\"]")).getText();
// 如果博客正文标题不为空,测试通过
// 否则测试不通过
Assertions.assertNotNull(t);
// Assertions.assertEquals(expected_t,t);
}
编写帖子:
/**
* 写博客
*/
@Test
void EditBlog(){
webDriver.get("http://127.0.0.1:9090/myblog_list.html");
//找到写博客按钮
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
//找到输入框输入标题
// 执行js(选中标题输入框,输入字符串)
((JavascriptExecutor)webDriver).executeScript("document.querySelector(\"title\").value=\"自动化测试\"");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//点击发布
webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
// 校验已发布博客
@ParameterizedTest
@MethodSource("Genertor")
@Test
void checkBlog(){
webDriver.get("http://127.0.0.1:9090/myblog_list.html");
//获取博客标题#artlist > div:nth-child(1) > div.title
String t=webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.title")).getText();
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
Assertions.assertEquals("自动化测试",t);
}
跳回主页:
@Test
void mainLog(){
webDriver.get("http://127.0.0.1:9090/myblog_list.html");
webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
String cur_url= webDriver.getCurrentUrl();
Assertions.assertEquals("http://127.0.0.1:9090/blog_list.html",cur_url);
}