目录
一、五子棋项目介绍
二、编写Web测试用例
三、自动化测试脚本开发
1、引入依赖
2、设计框架
3、Utils
4、LoginPage
5、RegisterPage
6、MatchPage
7、RunTest类
8、运行程序
一、五子棋项目介绍
五子棋项目是基于 WebSocket 实现的多人在线对战系统,该项目也是基于 五子棋游戏规则实现的对战游戏。其中项目主要分为三个模块:登录模块、匹配模块、对战模块。
这里就针对 登录模块、注册模块 和 匹配模块 进行自动化测试。对战模块需要模拟真人对弈,落子下标不好定位,自动化测试比较困难,也不建议做这模块的自动化测试。
项目地址:http://120.79.61.184:9090/login.html
二、编写Web测试用例
三、自动化测试脚本开发
1、引入依赖
//Selenium驱动
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
//webdrivermanager
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.8.0</version>
</dependency>
//屏幕截图
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
2、设计框架
common文件夹:存放创建的驱动对象、等待。
tests文件夹:存放测试用例
3、Utils
public class Utils {
public static WebDriver driver;
public static WebDriver createDriver() {
if(driver == null) {
WebDriverManager.edgedriver().setup();
EdgeOptions options = new EdgeOptions();
//允许访问所有的链接
options.addArguments("--remote-allow-origins=*");
driver = new EdgeDriver(options);
//隐式等待(全局情况,查找元素的时候都让它等待2秒)
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
}
return driver;
}
public Utils(String url) {
driver = createDriver();
driver.get(url);
}
}
4、LoginPage
package tests;
import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class LoginPage extends Utils {
private static String url = "http://120.79.61.184:9090/login.html";
public LoginPage() {
super(url);
}
public void loginSuccess() {
//正确账号,正确密码
driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
driver.get("http://120.79.61.184:9090/login.html");
driver.findElement(By.cssSelector("#username")).sendKeys("lisi");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
driver.get("http://120.79.61.184:9090/login.html");
}
public void loginFail() throws InterruptedException {
// 错误账号,正确密码
driver.findElement(By.cssSelector("#username")).sendKeys("yyds");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现
Alert alert = driver.switchTo().alert();
alert.accept();
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
// 错误账号,错误密码
driver.findElement(By.cssSelector("#username")).sendKeys("yyds");
driver.findElement(By.cssSelector("#password")).sendKeys("666");
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现
alert = driver.switchTo().alert();
alert.accept();
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
// 正确账号,错误密码
driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
driver.findElement(By.cssSelector("#password")).sendKeys("666");
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现
alert = driver.switchTo().alert();
alert.accept();
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
}
}
5、RegisterPage
package tests;
import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class RegisterPage extends Utils {
private static String url = "http://120.79.61.184:9090/register.html";
public RegisterPage() {
super(url);
}
public void registerFail() {
driver.get("http://120.79.61.184:9090/register.html");
// 没有输入账号、密码
driver.findElement(By.cssSelector("#username")).sendKeys("");
driver.findElement(By.cssSelector("#password")).sendKeys("");
driver.findElement(By.cssSelector("#submit")).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
// 输入账户,不输入密码
driver.findElement(By.cssSelector("#username")).sendKeys("first");
driver.findElement(By.cssSelector("#password")).sendKeys("");
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());
alert = driver.switchTo().alert();
alert.accept();
driver.findElement(By.cssSelector("#username")).clear();
// 不输入账户,输入密码
driver.findElement(By.cssSelector("#username")).sendKeys("");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());
alert = driver.switchTo().alert();
alert.accept();
driver.findElement(By.cssSelector("#password")).clear();
}
public void registerSuccess() {
driver.findElement(By.cssSelector("#username")).sendKeys("first1");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
String url = driver.getCurrentUrl();
assert url.equals("http://120.79.61.184:9090/login.html");
driver.get("http://120.79.61.184:9090/register.html");
driver.findElement(By.cssSelector("#username")).sendKeys("测试1");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());
alert = driver.switchTo().alert();
alert.accept();
url = driver.getCurrentUrl();
assert url.equals("http://120.79.61.184:9090/login.html");
}
}
6、MatchPage
package tests;
import common.Utils;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class MatchPage extends Utils {
private static String url = "http://120.79.61.184:9090/game_hall.html";
public MatchPage() {
super(url);
}
// 强制登录
public void noLoginToMatch() {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
String url = driver.getCurrentUrl();
assert url.equals("http://120.79.61.184:9090/login.html");
}
// 单人匹配
public void singleMatch() throws InterruptedException {
driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
// 确保有对应的按钮
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#match-button")));
WebElement element = driver.findElement(By.cssSelector("#match-button"));
String tmp = element.getText();
assert tmp.equals("开始匹配");
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#match-button")));
driver.findElement(By.cssSelector("#match-button")).click();
wait.until(ExpectedConditions.textToBe(By.cssSelector("#match-button"), "匹配中...(点击停止)"));
element = driver.findElement(By.cssSelector("#match-button"));
tmp = element.getText();
assert tmp.equals("匹配中...(点击停止)");
driver.close();
}
}
7、RunTest类
包含main方法,用来运行上述接口
import tests.LoginPage;
import tests.MatchPage;
import tests.RegisterPage;
public class RunTest {
public static void main(String[] args) throws InterruptedException {
LoginPage loginPage = new LoginPage();
loginPage.loginFail();
loginPage.loginSuccess();
RegisterPage registerPage = new RegisterPage();
registerPage.registerFail();
registerPage.registerSuccess();
MatchPage matchPage = new MatchPage();
matchPage.noLoginToMatch();
matchPage.singleMatch();
}
}
8、运行程序
结果如下:
有一个警告,但不影响程序的运行,可以不用理会。
测试用例全部都能通过。