登录页面:
注册页面:
游戏大厅页面:
游戏房间页面:
1、在IDEA创建Maven项目,导入pom.xlm相关依赖
org.seleniumhq.selenium
selenium-java
3.141.59
commons-io
commons-io
2.6
org.junit.jupiter
junit-jupiter
5.8.2
test
org.junit.platform
junit-platform-suite
1.8.2
test
2、包名、类名设计
- 因为每次测试都需要创建一个浏览器驱动对象造成资源和时间的浪费,所以可以创建一个公共类用来创建浏览器驱动对象。用屏幕截图来记录每次测试的页面,利用日期为文件夹,时间和类名来作为文件名进行记录,也可以这些代码放在公共类里面
public static ChromeDriver driver; //创建驱动对象 public static ChromeDriver createDriver(){ //驱动对象已经创建好了就直接返回,没有创建就创建 if(driver == null){ driver = new ChromeDriver(); //创建隐式等待,让其渲染完成 Duration duration = Duration.ofSeconds(10); long millis = duration.toMillis(); driver.manage().timeouts().implicitlyWait(millis, TimeUnit.MILLISECONDS); } return driver; } public List
getTime(){ //文件按照天的维度按文件夹进行保存 //文件格式 20240203-123103毫秒 SimpleDateFormat sim1 = new SimpleDateFormat("yyyyMMdd-HHmmssSS"); SimpleDateFormat sim2 = new SimpleDateFormat("yyyyMMdd"); String filename = sim1.format(System.currentTimeMillis()); String dirname = sim2.format(System.currentTimeMillis()); List list = new ArrayList<>(); list.add(dirname); list.add(filename); return list; } /** * 获取屏幕截图,把所有的用例执行结果保存下来 */ public void getScreenShot(String str) throws IOException { List list = getTime(); //dir + filename // ./指的是当前项目路径下,也就是java_gobang // ./src/test/java/com/java_gobangWebAutoTest/dirname/filename // ./src/test/java/com/java_gobangWebAutoTest/20240203/20240203-210303毫秒.png String filename = "./src/test/java/com/java_gobangWebAutoTest/" + list.get(0) +"/" + str +"_"+ list.get(1) + ".png"; File srcFile = driver.getScreenshotAs(OutputType.FILE); //把屏幕截图生成的文件放到指定路径 FileUtils.copyFile(srcFile,new File(filename)); }
- 使用suite测试套件来进行测试,指定类来运行用例。单独写一个释放驱动类,让其最后运行。
@Suite @SelectClasses({loginTest.class,game_roomTest.class,driverQuitTest.class}) public class runSuite { }
进行自动化登录测试首先得创建浏览器驱动和访问 登录的URL:
public static ChromeDriver driver= createDriver();
//如果要测试登录页面,以下所有的用例都有一个共同步骤:
//1、要有浏览器对象(驱动对象) 2、访问登录页面的URL
@BeforeAll
static void baseControl(){
driver.get("http://8.130.18.164:6060/login.html");
}
测试登录页面正常打开,页面元素是否存在:
/**
* 检查登录页面打开是否正确
* 检查点:主页的 注册、五子棋对战 元素是否存在
*/
@Test
@Order(1)
void loginPageLoadRight() throws IOException {
driver.findElement(By.cssSelector("body > div.nav > a"));
driver.findElement(By.cssSelector("body > div.nav > span"));
getScreenShot(getClass().getName());
}
测试登录失败的情况:
@ParameterizedTest
@CsvSource({"lisi,111"})
@Order(2)
void loginFail(String name, String password) throws InterruptedException, IOException {
String expected = "登录失败!";
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
//强制等待0.1s,弹窗出现
Thread.sleep(100);
//切换到弹窗进行弹窗处理
Alert alert = driver.switchTo().alert();
String accept = alert.getText();
alert.accept();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
}
测试登录成功的情况:
/**
* 检查正常登录的情况,会出现弹窗:登录成功
*/
@ParameterizedTest
@CsvSource({"lisi,123","zhangsan,123"})
@Order(3)
void loginSuc(String name, String password) throws InterruptedException, IOException {
String expected = "登录成功!";
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
//强制等待0.1s,弹窗出现
Thread.sleep(100);
//切换到弹窗进行弹窗处理
Alert alert = driver.switchTo().alert();
String accept = alert.getText();
alert.accept();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
//回退,重新登录另一个账号
driver.navigate().back();
}
运行结果:
注意:在获取弹窗内容的时候,需要强制等待0.1秒,让弹窗出现,否则无法获取弹窗内容。
测试注册页面正常打开,页面元素是否存在:
/**
* 检查登录页面打开是否正确
* 检查点:主页的 注册元素是否存在
*/
@Test
@Order(1)
void registerPageLoadRight() throws IOException {
driver.findElement(By.cssSelector("body > div.login-container > div > h3"));
getScreenShot(getClass().getName());
}
测试异常注册的情况:
@ParameterizedTest
@CsvSource({"oppp"})
@Order(2)
void registerFail(String name) throws InterruptedException, IOException {
String expected = "注册失败!";
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#submit")).click();
//强制等待0.1s,弹窗出现
Thread.sleep(100);
//切换到弹窗进行弹窗处理
Alert alert = driver.switchTo().alert();
String accept = alert.getText();
alert.accept();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
}
正常注册的情况:
/**
* 检查正常注册的情况,会出现弹窗:注册成功
*/
@Test
@Order(3)
void registerSuc() throws InterruptedException, IOException {
String expected = "注册成功!";
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
String name = System.currentTimeMillis()/1000 + "";
String password = System.currentTimeMillis()/1000 + "";
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
//强制等待0.1s,弹窗出现
Thread.sleep(100);
//切换到弹窗进行弹窗处理
Alert alert = driver.switchTo().alert();
String accept = alert.getText();
alert.accept();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
}
运行结果:
测试登录状态下游戏大厅是否有元素:
/**
* 检查登录状态下的游戏大厅元素是否有 screen属性
*/
@Test
@Order(1)
void game_hallLoadRight() throws IOException {
driver.findElement(By.cssSelector("#screen"));
getScreenShot(getClass().getName());
}
测试登录状态下,按钮文字是否变化:
/**
* 正常登录情况下检查点击匹配按钮是否会变化
*/
@Test
@Order(2)
void game_hallSuc() throws InterruptedException, IOException {
String expected = "匹配中...(点击停止)";
driver.findElement(By.cssSelector("#match-button")).click();
//强制等待,等按钮文字改变
Thread.sleep(1000);
String accept = driver.findElement(By.cssSelector("#match-button")).getText();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
}
运行结果:
测试通过链接进入游戏房间是否有弹窗:
/**
* 检查未登录状态下的游戏房间是否有弹窗
*/
@Test
@Order(2)
void game_roomFail() throws InterruptedException, IOException {
String expected = "连接游戏失败! reason: 当前房间已满, 您不能加入房间";
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
String accept = alert.getText();
alert.accept();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
}
运行结果:
整体运行结果:
package com.java_gobangWebAutoTest.Tests;
import com.java_gobangWebAutoTest.common.autoTestCommonUtils;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.IOException;
/**
* Created with IntelliJ IDEA.
* Description:
* User: D.ROSE
* Date: 2024-02-02
* Time: 16:20
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class loginTest extends autoTestCommonUtils{
public static ChromeDriver driver= createDriver();
//如果要测试登录页面,以下所有的用例都有一个共同步骤:
//1、要有浏览器对象(驱动对象) 2、访问登录页面的URL
@BeforeAll
static void baseControl(){
driver.get("http://8.130.18.164:6060/login.html");
}
/**
* 检查登录页面打开是否正确
* 检查点:主页的 注册、五子棋对战 元素是否存在
*/
@Test
@Order(1)
void loginPageLoadRight() throws IOException {
driver.findElement(By.cssSelector("body > div.nav > a"));
driver.findElement(By.cssSelector("body > div.nav > span"));
getScreenShot(getClass().getName());
}
/**
* 检查正常登录的情况,会出现弹窗:登录成功
*/
@ParameterizedTest
@CsvSource({"lisi,123","zhangsan,123"})
@Order(3)
void loginSuc(String name, String password) throws InterruptedException, IOException {
String expected = "登录成功!";
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
//强制等待0.1s,弹窗出现
Thread.sleep(100);
//切换到弹窗进行弹窗处理
Alert alert = driver.switchTo().alert();
String accept = alert.getText();
alert.accept();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
//回退,重新登录另一个账号
driver.navigate().back();
}
@ParameterizedTest
@CsvSource({"lisi,111"})
@Order(2)
void loginFail(String name, String password) throws InterruptedException, IOException {
String expected = "登录失败!";
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
//强制等待0.1s,弹窗出现
Thread.sleep(100);
//切换到弹窗进行弹窗处理
Alert alert = driver.switchTo().alert();
String accept = alert.getText();
alert.accept();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
}
}
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class registerTest extends autoTestCommonUtils{
public static ChromeDriver driver= createDriver();
//如果要测试登录页面,以下所有的用例都有一个共同步骤:
//1、要有浏览器对象(驱动对象) 2、访问登录页面的URL
@BeforeAll
static void baseControl(){
driver.get("http://8.130.18.164:6060/register.html");
}
/**
* 检查登录页面打开是否正确
* 检查点:主页的 注册元素是否存在
*/
@Test
@Order(1)
void registerPageLoadRight() throws IOException {
driver.findElement(By.cssSelector("body > div.login-container > div > h3"));
getScreenShot(getClass().getName());
}
@ParameterizedTest
@CsvSource({"oppp"})
@Order(2)
void registerFail(String name) throws InterruptedException, IOException {
String expected = "注册失败!";
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#submit")).click();
//强制等待0.1s,弹窗出现
Thread.sleep(100);
//切换到弹窗进行弹窗处理
Alert alert = driver.switchTo().alert();
String accept = alert.getText();
alert.accept();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
}
/**
* 检查正常注册的情况,会出现弹窗:注册成功
*/
@Test
@Order(3)
void registerSuc() throws InterruptedException, IOException {
String expected = "注册成功!";
driver.findElement(By.cssSelector("#username")).clear();
driver.findElement(By.cssSelector("#password")).clear();
String name = System.currentTimeMillis()/1000 + "";
String password = System.currentTimeMillis()/1000 + "";
driver.findElement(By.cssSelector("#username")).sendKeys(name);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
//强制等待0.1s,弹窗出现
Thread.sleep(100);
//切换到弹窗进行弹窗处理
Alert alert = driver.switchTo().alert();
String accept = alert.getText();
alert.accept();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
}
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class game_roomTest extends autoTestCommonUtils{
public static ChromeDriver driver = createDriver();
@BeforeAll
static void baseControl(){
driver.get("http://8.130.18.164:6060/game_room.html");
}
/**
* 检查未登录状态下的游戏房间是否有弹窗
*/
@Test
@Order(2)
void game_roomFail() throws InterruptedException, IOException {
String expected = "连接游戏失败! reason: 当前房间已满, 您不能加入房间";
Thread.sleep(1000);
Alert alert = driver.switchTo().alert();
String accept = alert.getText();
alert.accept();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
}
}
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class game_hallTest extends autoTestCommonUtils{
public static ChromeDriver driver = createDriver();
@BeforeAll
static void baseControl(){
driver.get("http://8.130.18.164:6060/game_hall.html");
}
/**
* 检查登录状态下的游戏大厅元素是否有 screen属性
*/
@Test
@Order(1)
void game_hallLoadRight() throws IOException {
driver.findElement(By.cssSelector("#screen"));
getScreenShot(getClass().getName());
}
/**
* 正常登录情况下检查点击匹配按钮是否会变化
*/
@Test
@Order(2)
void game_hallSuc() throws InterruptedException, IOException {
String expected = "匹配中...(点击停止)";
driver.findElement(By.cssSelector("#match-button")).click();
//强制等待,等按钮文字改变
Thread.sleep(1000);
String accept = driver.findElement(By.cssSelector("#match-button")).getText();
getScreenShot(getClass().getName());
Assertions.assertEquals(expected,accept);
}
}