目录
一、设计测试用例
二、执行测试
登录页面
功能测试
界面测试
注册界面
功能测试
界面测试
游戏大厅
功能测试
界面测试
游戏房间
功能测试
界面测试
一、设计测试用例
二、执行测试
在执行测试之前首先获取到驱动。
登录页面
功能测试
首先定义start()方法和close()方法,分别测试用例之前和之后执行。
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8086/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterAll
public static void close(){
driver.quit();
}
正确的用户名和密码进行测试:
/**
* 测试正确登录
*/
@ParameterizedTest
@CsvSource(value = {"zhangsan,1234","lucky,12345"})
public void testLoginTrueUser(String user,String password) throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(user);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8099/game_hall.html",url);
driver.navigate().back();
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
测试结果:
测试错误的用户名或密码:
/**
* 测试用户名或密码错误
*/
@ParameterizedTest
@CsvSource(value = {"ioio,12345","jake,909"})
public void testLoginErrorUser(String user,String password) throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(user);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
System.out.println("alert: " + tips);
Assertions.assertEquals("用户名或密码输入错误",tips);
alert.accept();
}
测试结果:
测试空的登录名:
@ParameterizedTest
@CsvSource(value = {",12345"})
public void testLoginNullUser(String user,String password){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
if(password != null){
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
}
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("请输入登录名",tips);
}
测试结果:
测试空的密码登录:
/**
* 测试密码为空
*/
@ParameterizedTest
@CsvSource(value = {"Jake,"})
public void testLoginNullPassword(String user,String password){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
if(user != null){
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(user);
}
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("请输入密码",tips);
}
测试结果:
测试用户名或密码中含有特殊字符:
/**
* 测试特殊字符
*/
@ParameterizedTest
@CsvSource(value = {"0*&%=#,12345","Kop,*&%$#"})
public void testLoginPartUser(String user,String password) throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(user);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8086/list.html",url);
driver.navigate().back();
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
测试结果:
测试页面中的注册链接是否有效:
/**
* 测试注册链接
*/
@Test
public void testLink(){
driver.findElement(By.xpath("/html/body/div[2]/div/div[4]/a")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8099/register.html",url);
}
测试结果:
界面测试
public class InterfaceTest {
public static EdgeDriver driver;
public static EdgeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
EdgeOptions options = new EdgeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new EdgeDriver(options);
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8099/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 测试登陆文字
*/
@Test
public void testDengLu(){
String dengLu = driver.findElement(By.xpath("//*[@id=\"body\"]/div/h3")).getText();
Assertions.assertEquals(dengLu,"登录");
}
/**
* 测试提交按钮的文字
*/
@Test
public void testTiJiao(){
String tiJiao = driver.findElement(By.xpath("//*[@id=\"submit\"]")).getAttribute("value");
Assertions.assertEquals(tiJiao,"提交");
}
/**
* 测试用户名输入框
*/
@Test
public void testUserInput(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"username\"]"));
Assertions.assertNotNull(webElement);
}
/**
* 测试密码输入框
*/
@Test
public void testPasswordInput(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"password\"]"));
Assertions.assertNotNull(webElement);
}
/**
* 测试提交按钮
*/
@Test
public void testSubmit(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"submit\"]"));
Assertions.assertNotNull(webElement);
}
}
测试结果:
注册界面
功能测试
首先定义好start()方法和close()方法。
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8099/register.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterAll
public static void close(){
driver.quit();
}
测试空的用户名:
/**
* 测试用户名为空
*/
@ParameterizedTest
@CsvSource(value = {",12345,12345"})
public void testNullUsername(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("请先输入用户名",tips);
alert.accept();
}
测试结果:
测试空的密码:
/**
* 测试密码为空
*/
@ParameterizedTest
@CsvSource(value = {"tester,,12345"})
public void testNullPassword(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("请先输入密码",tips);
alert.accept();
}
测试结果:
测试空的确认密码:
/**
* 测试确认密码为空
*/
@ParameterizedTest
@CsvSource(value = {"tester,12345,"})
public void testNullPassword2(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("请先输入确认密码",tips);
alert.accept();
}
测试结果:
测试两次密码输入不一致:
/**
* 测试两次密码不一致
*/
@ParameterizedTest
@CsvSource(value = {"tester,12345,09876"})
public void testNOtEqual(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("两次密码输入不一致,请重新输入",tips);
alert.accept();
}
测试结果:
测试用户已存在:
/**
* 测试用户已存在
*/
@ParameterizedTest
@CsvSource(value = {"jake,12345,12345"})
public void testUserExit(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("用户已存在",tips);
alert.accept();
}
测试结果:
测试用户名或密码中含有特殊字符:
/**
* 测试用户名或密码中含有特殊字符
*/
@ParameterizedTest
@CsvSource(value = {"j&#*@^,12345,12345","muyt,09*&,09*&"})
public void testPartUser1(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("添加成功",tips);
alert.accept();
alert.start();
}
测试结果:
测试正常注册:
/**
* 正常注册
*/
@ParameterizedTest
@CsvSource(value = {"qiyu,12345,12345"})
public void testPartUser(String username,String password,String password2){
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(username);
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"password2\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password2\"]")).sendKeys(password2);
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
Alert alert = driver.switchTo().alert();
String tips =alert.getText();
Assertions.assertEquals("添加成功",tips);
alert.accept();
}
测试结果:
界面测试
public class InterfaceTest {
public static EdgeDriver driver;
public static EdgeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
EdgeOptions options = new EdgeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new EdgeDriver(options);
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8099/register.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 测试注册文字
*/
@Test
public void testZhuCe(){
String zhuce = driver.findElement(By.xpath("/html/body/div[2]/div/h3")).getText();
Assertions.assertEquals(dengLu,"注册");
}
/**
* 测试提交按钮的文字
*/
@Test
public void testTiJiao(){
String tiJiao = driver.findElement(By.xpath("//*[@id=\"submit\"]")).getAttribute("value");
Assertions.assertEquals(tiJiao,"提交");
}
/**
* 测试用户名输入框
*/
@Test
public void testUserInput(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"username\"]"));
Assertions.assertNotNull(webElement);
}
/**
* 测试密码输入框
*/
@Test
public void testPasswordInput(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"password\"]"));
Assertions.assertNotNull(webElement);
}
/**
* 测试确认密码输入框
*/
@Test
public void testPassword2Input(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"password2\"]"));
Assertions.assertNotNull(webElement);
}
/**
* 测试提交按钮
*/
@Test
public void testSubmit(){
WebElement webElement = driver.findElement(By.xpath("//*[@id=\"submit\"]"));
Assertions.assertNotNull(webElement);
}
}
测试结果:
游戏大厅
功能测试
首先定义start()方法和close()方法。
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8099/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(18));
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("jake");
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("12345");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterAll
public static void close(){
driver.quit();
}
测试开始匹配按钮点击后文字是否改变:
/**
* 测试开始匹配按钮
*/
@Test
public void testButton(){
String word = driver.findElement(By.xpath("//*[@id=\"match-button\"]")).getText();
Assertions.assertEquals(word,"开始匹配");
driver.findElement(By.xpath("//*[@id=\"match-button\"]")).click();
word = driver.findElement(By.xpath("//*[@id=\"match-button\"]")).getText();
Assertions.assertEquals(word,"匹配中...(点击停止)");
}
测试结果:
界面测试
public class InterfaceTest {
public static EdgeDriver driver;
public static EdgeDriver getDriver(){
if(driver == null){
synchronized (PrepareTest.class){
if(driver == null){
EdgeOptions options = new EdgeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new EdgeDriver(options);
}
}
}
return driver;
}
@BeforeAll
public static void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8099/login.html");
//使用隐式等待渲染页面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(18));
driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("jake");
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("12345");
driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 测试按钮
*/
@Test
public void testButton(){
WebElement button = driver.findElement(By.xpath("//*[@id=\"match-button\"]"));
Assertions.assertNotNull(button);
String word = driver.findElement(By.xpath("//*[@id=\"match-button\"]")).getText();
Assertions.assertEquals(word,"开始匹配");
}
/**
* 测试导航栏
*/
@Test
public void testNavigate(){
String word = driver.findElement(By.xpath("/html/body/div[1]")).getText();
Assertions.assertEquals("对战五子棋",word);
}
/**
* 测试个人信息块
*/
@Test
public void testInfo(){
WebElement screen = driver.findElement(By.xpath("//*[@id=\"screen\"]"));
Assertions.assertNotNull(screen);
}
}
测试结果:
游戏房间
由于该页面需要两个用户匹配成功后才能进入,自动化测试实现相对复杂,就设计出测试用例进行了手工测试,且测试通过。