该项目是一个网页对战的五子棋游戏,支持真人匹配对战和人机对战,支持多用户同时匹配进行五子棋对战,通过同的天梯积分匹配对战。
使用的主要的技术栈:Java、MySQL、SpringBoot、MyBatis、WebSocket
功能测试用例来编写自动化测试,为了方便自动化测试关闭了验证码功能。
编写自动化代码,打开注册页面,使用断言检测是否正确打开,再进行错误注册和正确注册。
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class RegAutoTest {
private static EdgeDriver driver;
@BeforeAll
public static void init() {
driver = AutoTestUtil.getEdgeDriver();
driver.get("http://127.0.0.1:8080/reg.html");
driver.manage().window().maximize();
}
@Order(1)
public static void check() {
String key = driver.findElement(By.xpath("/html/body/div/div[2]/h1")).getText();
Assertions.assertEquals("用户注册",key);
}
@Order(2)
@ParameterizedTest
@CsvSource(value = {"hhy,hhy,hhy","null,null,null","null,test,test","test,null,test","test,test,null"})
public void failRegTest(String username,String password,String finalPass) throws InterruptedException {
WebElement usernameElement = driver.findElement(By.xpath("//*[@id=\"username\"]"));
WebElement passwordElement = driver.findElement(By.cssSelector("#password"));
WebElement finalPassElement = driver.findElement(By.cssSelector("#confirmPassword"));
if (!"null".equals(username)) {
usernameElement.sendKeys(username);
}
if (!"null".equals(password)) {
passwordElement.sendKeys(password);
}
if (!"null".equals(finalPass)) {
finalPassElement.sendKeys(finalPass);
}
driver.findElement(By.xpath("/html/body/div/div[2]/div/div[5]/input")).click();
// 显示等待弹窗
Thread.sleep(800);
driver.switchTo().alert().accept();
// 清空输入数据
usernameElement.clear();
passwordElement.clear();
finalPassElement.clear();
}
@Order(3)
@ParameterizedTest
@CsvSource(value = {"test,test,test"})
public void successRegTest(String username,String password,String finalPass) throws InterruptedException {
WebElement usernameElement = driver.findElement(By.xpath("//*[@id=\"username\"]"));
WebElement passwordElement = driver.findElement(By.cssSelector("#password"));
WebElement finalPassElement = driver.findElement(By.cssSelector("#confirmPassword"));
usernameElement.sendKeys(username);
passwordElement.sendKeys(password);
finalPassElement.sendKeys(finalPass);
driver.findElement(By.xpath("/html/body/div/div[2]/div/div[5]/input")).click();
// 显示等待弹窗
Thread.sleep(800);
driver.switchTo().alert().accept();
}
@AfterAll
public static void quit() {
driver.quit();
}
}
执行自动化代码:
编写登录测试自动化代码,和注册自动化测试类似,进行错误登录和正确登录测试用例编写自动化代码,正确登录后通过断言检测匹配按钮是否存在。
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LoginAutoTest {
private static EdgeDriver driver;
@BeforeAll
public static void init() {
driver = AutoTestUtil.getEdgeDriver();
driver.get("http://127.0.0.1:8080/login.html");
driver.manage().window().maximize();
}
@Order(1)
@Test
public void check() {
String key = driver.findElement(By.xpath("/html/body/div/div[2]/h1")).getText();
Assertions.assertEquals("网页对战五子棋",key);
}
@Order(2)
@ParameterizedTest
@CsvSource(value = {"null,null","test,null","null,test","test,1234"})
public void failLoginAutoTest(String username,String password) throws InterruptedException {
WebElement usernameElement = driver.findElement(By.xpath("//*[@id=\"username\"]"));
WebElement passwordElement = driver.findElement(By.xpath("//*[@id=\"password\"]"));
if (!"null".equals(username)) {
usernameElement.sendKeys(username);
}
if (!"null".equals(password)) {
passwordElement.sendKeys(password);
}
driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/div[4]/input")).click();
Thread.sleep(650);
driver.switchTo().alert().accept();
usernameElement.clear();
passwordElement.clear();
}
@Order(3)
@ParameterizedTest
@CsvSource(value = {"test,test"})
public void successLoginAutoTest(String username,String password) throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/div[4]/input")).click();
Thread.sleep(800);
}
@AfterAll
public static void quit() {
driver.quit();
}
}
自动化演示:
使用浏览器打开两个窗口进行测试,模拟两个用户进行登录后进行匹配。
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MatchAutoTest {
private static EdgeDriver driver;
private static EdgeDriver edgeDriverNoTraceWindow;
@BeforeAll
public static void init() throws InterruptedException {
driver = AutoTestUtil.getEdgeDriver();
edgeDriverNoTraceWindow = AutoTestUtil.getEdgeDriverNoTraceWindow();
driver.get("http://127.0.0.1:8080/login.html");
edgeDriverNoTraceWindow.get("http://127.0.0.1:8080/login.html");
Thread.sleep(1000);
}
@Test
@Order(1)
public void login() throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("hhy");
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("hhy");
edgeDriverNoTraceWindow.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("root");
edgeDriverNoTraceWindow.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("root");
driver.findElement(By.cssSelector("body > div > div.right > div.userInfo > div:nth-child(4) > input")).click();
edgeDriverNoTraceWindow.findElement(By.cssSelector("body > div > div.right > div.userInfo > div:nth-child(4) > input")).click();
Thread.sleep(1000);
}
@Order(2)
@Test
public void match() throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"match\"]")).click();
Thread.sleep(1500);
edgeDriverNoTraceWindow.findElement(By.xpath("//*[@id=\"match\"]")).click();
}
}
自动化演示:
使用LR对项目进行一个简单的性能测试,模拟多个用户同时进行登录。
Action()
{
//开启进入登录页事务
lr_start_transaction("login_index");
/* 注册获取返回参数,该方法可以配合打印返回数据,检测数据内容 */
web_reg_save_param("ParaResult",
"LB=",
"RB=",
LAST);
// 定时检查点,检测登录页面是否正确打开
web_reg_find("Text=网页对战五子棋",LAST);
// 定义虚拟用户结合点
lr_rendezvous("start");
// 进入登录页面
web_url("login_html",
"URL=http://127.0.0.1:8080/login.html",
"TargetFrame=",
"Resource=0",
"Referer=",
LAST);
// 结束进入登录页事务
lr_end_transaction("login_index", LR_AUTO);
//定时检测是否成功进入游戏大厅
//开启登录事务
lr_start_transaction("login");
// 进行登录
web_submit_data("login",
"Action=http://127.0.0.1:8080/user/login",
"Method=POST",
"EncType=application/x-www-form-urlencoded; charset=UTF-8",
"TargetFrame=",
"Referer=",
"Mode=HTTP",
ITEMDATA,
"Name=username", "Value={username}", ENDITEM,
"Name=password", "Value={password}", ENDITEM,
LAST);
// 结束登录事务
lr_end_transaction("login", LR_AUTO);
return 0;
}