一、测试项目介绍
本网页版五子棋双人对战小游戏基于SSM框架和 WebSocket 技术实现,前端页面由登录、注册、游戏大厅、游戏房间四个组成,测试目标旨在该项目功能是否能够正常运行。
二、测试
测试环境:Windows11,Google chrome浏览器 128.0.6613.138 (正式版本) (64 位)
1.编写测试用例
2.功能测试
2.1 注册测试
输入用户名和密码进行注册,附上部分测试截图:
测试代码:
public class SignIn extends AutotestUtils{
@ParameterizedTest
@CsvFileSource(resources = "/test.csv")
public void signTest(String username, String password) throws IOException {
// 找到注册用户输入框
WebElement signInput = webDriver.findElement(By.id("signUpUser"));
signInput.sendKeys(username);
// 找到密码输入框
WebElement signUpPassword = webDriver.findElement(By.id("signUpPassword"));
signUpPassword.sendKeys(password);
// 点击注册
WebElement button = webDriver.findElement(By.cssSelector("#signUpSubmit"));
// 使用 JavaScript 触发点击
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("arguments[0].click();", button);
// 用注册的账户进行登录, 若登录成功说明注册成功
LoginTest login = new LoginTest();
login.loginTest(username, password);
}
}
2.2 登录测试
输入用户名和密码,验证登录功能是否能够正常通过,附上部分截图:
代码:
public class LoginTest extends AutotestUtils{
// 正常登录
@Test
public void testLogin() throws IOException {
loginTest("zhangsan", "123456");
}
// 异常登录
@Test
public void testLogin2() throws IOException {
// 用户名正确, 密码错误
loginTest("zhangsan", "1234567");
}
@Test
public void testLogin3() throws IOException {
// 用户名错误, 密码正确
loginTest("zhangsan1111", "123456");
}
@Test
public void testLogin4() throws IOException {
// 用户名密码均错误
loginTest("zha456ngsan", "1234567");
}
@Test
public void testLogin5() throws IOException {
// 密码正确,用户名为空
loginTest("", "123456");
}
@Test
public void testLogin6() throws IOException {
// 用户名正确, 密码为空
loginTest("zhangsan", "");
}
public void loginTest(String username, String password) throws IOException {
// 找到登录元素
WebElement login = webDriver.findElement(By.id("signIn"));
login.click(); // 点击该元素
// 找到输入用户名元素
WebElement inputName = webDriver.findElement(By.id("username"));
inputName.sendKeys(username);
// 找到输入密码元素
WebElement inputPassword = webDriver.findElement(By.id("password"));
inputPassword.sendKeys(password);
// 点击登录
WebElement button = webDriver.findElement(By.cssSelector("#submit"));
// 使用 JavaScript 触发点击
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("arguments[0].click();", button);
// 等待弹窗出现
wait.until(ExpectedConditions.alertIsPresent());
// 切换到弹窗
Alert alert = webDriver.switchTo().alert();
// 获取弹窗信息
String text = alert.getText();
// 结束弹窗
alert.accept();
boolean isLoginSuccess = isLoginSuccessful();
if (isLoginSuccess) {
System.out.println("登录成功:" + text);
AutotestUtils.getScreenShot(webDriver);
}else {
System.out.println("登录失败:" + text);
AutotestUtils.getScreenShot(webDriver);
}
}
// 确认是否登录成功并跳转页面
private boolean isLoginSuccessful() {
try {
// 登录成功
// 检查新页面是否成功跳转
wait.until(ExpectedConditions.urlToBe("http://47.120.16.255:8081/game_hall.html"));
return true;
}catch (Exception e) {
return false;
}
}
}
2.3 游戏大厅测试
2.4 游戏房间测试
2.5 游戏胜负测试
2.6 用户多开测试
2.自动化测试
自动化测试覆盖模块:登录页面、注册页面。
自动化代码示例:测试代码
测试用例:pass:15/15。
遗留风险:项目主功能没有问题,可能存在细节测试不到位等风险。