本文主要是介绍网页版五子棋的自动化测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
前言
一、主要技术
二、测试环境的准备部署
三、测试用例
四、执行测试
4.1、公共类设计
创建浏览器驱动对象
测试套件
释放驱动类
4.2、功能测试
登录页面
注册页面
游戏大厅页面
游戏房间页面
测试套件结果
4.3、界面测试
登录页面
注册页面
游戏大厅页面
游戏房间页面
前言
在五子棋对战项目的基础上进行自动化测试,已知此项目由用户模块、匹配模块、对战模块构成。用户模块包含用户的注册和登录,管理用户的天梯分数、比赛场数、获胜场数等信息;匹配模块包含依据用户的天梯积分,实现匹配机制;对战模块包含把两个匹配到的玩家放到一个游戏房间中,对方通过网页的形式来进行对战比赛。
整体对项目进行自动化测试分为设计测试用例和进行测试两方面
一、主要技术
整体测试流程主要涉及谷歌浏览器驱动 ChromeDriver + 自动化测试工具selenium4 + 单元测试框架Junit5。
selenium是一款开源免费的web自动化测试工具,支持多浏览器,支持多系统,支持多语言,而且其中提供了很多可供测试的API,使用十分便捷。
JUnit是一款Java单元测试的工具,用于优化我们的自动化脚本。
二、测试环境的准备部署
在pom导入要依赖的包
<dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>4.10.0</version></dependency><!--保存屏幕截图文件所要用到的包 --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>1.4</version></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.8.2</version><scope>test</scope></dependency><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-suite</artifactId><version>1.8.2</version><scope>test</scope></dependency>
三、测试用例
四、执行测试
4.1、公共类设计
创建浏览器驱动对象
由于每次测试都需要创建一个浏览器驱动对象造成资源和时间的浪费,所以可以创建一个公共类用来创建浏览器驱动对象
public class AutotestUtils {public static ChromeDriver chromeDriver;public static EdgeDriver edgeDriver;//创建驱动对象public static ChromeDriver createChromeDriver(){//驱动创建好了、没有创建if (chromeDriver==null){ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");chromeDriver=new ChromeDriver(options);//隐式等待Duration duration = Duration.ofSeconds(10);long millis = duration.toMillis();chromeDriver.manage().timeouts().implicitlyWait(millis, TimeUnit.MILLISECONDS);}return chromeDriver;}public static EdgeDriver createEdgeDriver(){//驱动创建好了、没有创建if (edgeDriver==null){EdgeOptions options = new EdgeOptions();options.addArguments("--remote-allow-origins=*");edgeDriver=new EdgeDriver(options);//隐式等待edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));}return edgeDriver;}
}
测试套件
使用suite测试套件来进行测试,指定类来运行用例
@Suite
@SelectClasses({LoginFunctionTest.class, driverQuitTest.class})
public class runSuite {
}
释放驱动类
单独写一个释放驱动类
public class driverQuitTest extends AutotestUtils {public static ChromeDriver chromeDriver;public static EdgeDriver edgeDriver;@Teststatic void driverQuit(){if (chromeDriver!=null){chromeDriver.quit();}else if (edgeDriver!=null){edgeDriver.quit();}else {chromeDriver.quit();edgeDriver.quit();}}
}
4.2、功能测试
登录页面
首先定义浏览器对象和url
public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:9999/login.html");}
测试错误的用户名和密码
/*** 测试用户名或者密码错误*/@ParameterizedTest@CsvSource({"baekhyun,1999","bb,1993"})@Order(1)void testLoginErrorUser(String username,String password) throws InterruptedException {try {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);//登录失败的结果进行检测String except="登录失败!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();System.out.println("alert:"+actual);Assertions.assertEquals(except,actual);alert.accept();}catch (NoAlertPresentException e){System.out.println("没有发现警告窗!");}catch (UnhandledAlertException e){System.out.println("警告窗文本与预期不符合!");}}
测试用户名为空
/*** 测试空的用户名*/@Order(2)@ParameterizedTest@CsvSource({",1999",",1993"})void testLoginNullUser(String username,String password) throws InterruptedException {try {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();if (password!=null){chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);}chromeDriver.findElement(By.cssSelector("#submit")).click();//等待一秒钟Thread.sleep(1000);//登录失败的结果进行检测String except="登录失败!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();System.out.println("alert:"+actual);Assertions.assertEquals(except,actual);alert.accept();}catch (NoAlertPresentException e){System.out.println("没有发现警告窗!");}catch (UnhandledAlertException e){System.out.println("警告窗文本与预期不符合!");}}
测试密码为空
/*** 测试空的密码* @param username* @param password* @throws InterruptedException*/@Order(3)@ParameterizedTest@CsvSource({"baekhyun,","bb,"})void testLoginNullPassword(String username,String password) throws InterruptedException {try {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();if (username!=null){chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);}chromeDriver.findElement(By.cssSelector("#submit")).click();//等待一秒钟Thread.sleep(1000);//登录失败的结果进行检测String except="登录失败!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();System.out.println("alert:"+actual);Assertions.assertEquals(except,actual);alert.accept();}catch (NoAlertPresentException e){System.out.println("没有发现警告窗!");}catch (UnhandledAlertException e){System.out.println("警告窗文本与预期不符合!");}}
测试用户名或密码出现特殊符号
/*** 测试特殊字符*/@Order(4)@ParameterizedTest@CsvSource({"$^*(,1999","krystal,#^&**((*"})void testLoginPartUser(String username,String password) throws InterruptedException {try {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);chromeDriver.findElement(By.cssSelector("#submit")).click();//等待一秒钟Thread.sleep(1000);//登录失败的结果进行检测String except="登录失败!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();System.out.println("alert:"+actual);Assertions.assertEquals(except,actual);alert.accept();}catch (NoAlertPresentException e){System.out.println("没有发现警告窗!");}catch (UnhandledAlertException e){System.out.println("警告窗文本与预期不符合!");}}
检测多开是否能正常登录
/*** 测试多开*/@Order(5)@Testvoid testTwoUser() throws InterruptedException {edgeDriver.get("http://49.232.185.203:9999/login.html");//使用隐式等待渲染页面完成edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(18));edgeDriver.findElement(By.cssSelector("#username")).clear();edgeDriver.findElement(By.cssSelector("#password")).clear();edgeDriver.findElement(By.cssSelector("#username")).sendKeys("baekhyun");edgeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");edgeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);Alert alert2=edgeDriver.switchTo().alert();alert2.accept();edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("baekhyun");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);Alert alert3=chromeDriver.switchTo().alert();alert3.accept();Thread.sleep(1000);String except="当前检测到了多开,请使用其他账号登录!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();Assertions.assertEquals(except,actual);Thread.sleep(1000);alert.accept();edgeDriver.quit();}
测试正确的用户名和密码
/*** 测试正确登录*/@Order(5)@ParameterizedTest@CsvSource({"baekhyun,1992"})void testLoginSuccess(String username,String password) throws InterruptedException {try {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);chromeDriver.findElement(By.cssSelector("#submit")).click();//等待一秒钟Thread.sleep(1000);//登录失败的结果进行检测String except="登录成功!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();System.out.println("alert:"+actual);Assertions.assertEquals(except,actual);alert.accept();}catch (NoAlertPresentException e){System.out.println("没有发现警告窗!");}catch (UnhandledAlertException e){System.out.println("警告窗文本与预期不符合!");}}
测试结果:
注册页面
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:9999/register.html");}
测试空的用户名
/*** 测试空的用户名* @param username* @param password* @throws InterruptedException*/@Order(1)@ParameterizedTest@CsvSource({",1999"})public void testRegisterNullUsername(String username,String password) throws InterruptedException {try {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();if (password!=null){chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);}chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="注册失败!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();System.out.println("alert:"+actual);Assertions.assertEquals(except,actual);alert.accept();}catch (NoAlertPresentException e){System.out.println("没有发现警告窗!");}catch (UnhandledAlertException e){System.out.println("警告窗文本与预期不符合!");}}
测试空的密码
/*** 测试空的密码* @param username* @param password* @throws InterruptedException*/@Order(2)@ParameterizedTest@CsvSource({"hahaha,"})public void testRegisterNullPassword(String username,String password) throws InterruptedException {try {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();if (username!=null){chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);}chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="注册失败!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();System.out.println("alert:"+actual);Assertions.assertEquals(except,actual);alert.accept();}catch (NoAlertPresentException e){System.out.println("没有发现警告窗!");}catch (UnhandledAlertException e){System.out.println("警告窗文本与预期不符合!");}}
测试用户已存在
/*** 测试用户已存在*/@Order(3)@ParameterizedTest@CsvSource({"baekhyun,1933"})public void testRegisterUserExit(String username,String password) throws InterruptedException {try {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="注册失败!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();System.out.println("alert:"+actual);Assertions.assertEquals(except,actual);alert.accept();}catch (NoAlertPresentException e){System.out.println("没有发现警告窗!");}catch (UnhandledAlertException e){System.out.println("警告窗文本与预期不符合!");}}
测试用户名或密码中含有特殊字符
/*** 测试用户名或密码中含有特殊字符*/@Order(4)@ParameterizedTest@CsvSource({"%^^&*^*,1999","bakkk,%&&*&*&("})public void testRegisterPartUser(String username,String password) throws InterruptedException {try {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="注册成功!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();System.out.println("alert:"+actual);Assertions.assertEquals(except,actual);alert.accept();chromeDriver.navigate().back();}catch (NoAlertPresentException e){System.out.println("没有发现警告窗!");}catch (UnhandledAlertException e){System.out.println("警告窗文本与预期不符合!");}}
测试正常注册
/*** 测试正常注册*/@Order(5)@ParameterizedTest@CsvSource({"ccc,199923"})public void testRegisterSuccess(String username,String password) throws InterruptedException {try {chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys(username);chromeDriver.findElement(By.cssSelector("#password")).sendKeys(password);chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);String except="注册成功!";Alert alert=chromeDriver.switchTo().alert();String actual=alert.getText();System.out.println("alert:"+actual);Assertions.assertEquals(except,actual);alert.accept();}catch (NoAlertPresentException e){System.out.println("没有发现警告窗!");}catch (UnhandledAlertException e){System.out.println("警告窗文本与预期不符合!");}}
测试结果:
游戏大厅页面
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl() throws InterruptedException {chromeDriver.get("http://49.232.185.203:9999/login.html");//使用隐式等待渲染页面完成chromeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(18));chromeDriver.findElement(By.cssSelector("#username")).clear();chromeDriver.findElement(By.cssSelector("#password")).clear();chromeDriver.findElement(By.cssSelector("#username")).sendKeys("baekhyun");chromeDriver.findElement(By.cssSelector("#password")).sendKeys("1992");chromeDriver.findElement(By.cssSelector("#submit")).click();Thread.sleep(1000);Alert alert=chromeDriver.switchTo().alert();alert.accept();chromeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));}
测试登录状态下,按钮能否正常点击并且文字变为匹配中
/*** 测试登录状态下,按钮能否正常点击并且文字变为匹配中*/@Testvoid testButton() throws InterruptedException {Thread.sleep(1000);String word=chromeDriver.findElement(By.cssSelector("#match-button")).getText();Assertions.assertEquals(word,"开始匹配");Thread.sleep(1000);chromeDriver.findElement(By.cssSelector("#match-button")).click();//强制等待Thread.sleep(1000);word=chromeDriver.findElement(By.cssSelector("#match-button")).getText();Assertions.assertEquals(word,"匹配中...(点击停止)");}
测试再次点击之后文字是否变为开始匹配
/*** 测试再次点击后文字是否变为开始匹配*/@Order(2)@Testvoid testButton2() throws InterruptedException {Thread.sleep(1000);String word=chromeDriver.findElement(By.cssSelector("#match-button")).getText();Assertions.assertEquals(word,"匹配中...(点击停止)");Thread.sleep(1000);chromeDriver.findElement(By.cssSelector("#match-button")).click();//强制等待Thread.sleep(1000);word=chromeDriver.findElement(By.cssSelector("#match-button")).getText();Assertions.assertEquals(word,"开始匹配");}
测试两个用户点击匹配是否能匹配成功
/*** 测试两个用户点击匹配是否能匹配成功*/@Testvoid jionSuccess() throws InterruptedException {chromeDriver.findElement(By.cssSelector("#match-button")).click();edgeDriver.findElement(By.cssSelector("#match-button")).click();Thread.sleep(1000);String url1=chromeDriver.getCurrentUrl();String url2= edgeDriver.getCurrentUrl();Assertions.assertEquals("http://49.232.185.203:9999/game_room.html",url1);Assertions.assertEquals("http://49.232.185.203:9999/game_room.html",url2);}
测试结果:
游戏房间页面
检测下棋过程中是否有提示语
/*** 测试下棋过程中是否有提示语*/@Testvoid testGameRoomText() throws InterruptedException {chromeDriver.findElement(By.cssSelector("#match-button")).click();edgeDriver.findElement(By.cssSelector("#match-button")).click();Thread.sleep(1000);String text1=chromeDriver.findElement(By.cssSelector("#screen")).getText();String text2=edgeDriver.findElement(By.cssSelector("#screen")).getText();System.out.println(text1);System.out.println(text2);if (text1.equals("轮到你落子了!")){Assertions.assertEquals(text1,"轮到你落子了!");}else {Assertions.assertEquals(text1,"轮到对方落子了!");}if (text2.equals("轮到你落子了!")){Assertions.assertEquals(text2,"轮到你落子了!");}else {Assertions.assertEquals(text2,"轮到对方落子了!");}}
测试结果:
测试套件结果
4.3、界面测试
登录页面
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:9999/login.html");}
测试提交用户登录标题
/*** 测试用户登录标题*/@Testvoid testDenglu(){String loginText=chromeDriver.findElement(By.cssSelector("body > div.login-container > div > h3")).getText();Assertions.assertEquals(loginText,"登录");}
测试登录用户名输入框
/*** 测试用户名输入框*/@Testvoid testUserInput(){WebElement webElement=chromeDriver.findElement(By.cssSelector("#username"));Assertions.assertNotNull(webElement);}
测试登录密码输入框
/*** 测试密码输入框*/@Testvoid testPasswordInput(){WebElement webElement=chromeDriver.findElement(By.cssSelector("#password"));Assertions.assertNotNull(webElement);}
测试登录提交按钮
/*** 测试提交按钮*/@Testvoid testSubmit(){WebElement webElement=chromeDriver.findElement(By.cssSelector("#submit"));Assertions.assertNotNull(webElement);}
测试登录提交按钮的文字
/*** 测试提交按钮的文字*/@Testvoid testSubmitText(){String submitText=chromeDriver.findElement(By.cssSelector("#submit")).getText();Assertions.assertEquals(submitText,"提交");}
测试结果:
注册页面
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:9999/register.html");}
测试用户注册标题
/***测试用户注册标题*/@Testvoid testRegister(){String registerText=chromeDriver.findElement(By.cssSelector("body > div.login-container > div > h3")).getText();Assertions.assertEquals(registerText,"注册");}
测试注册用户名输入框
/*** 测试用户名输入框*/@Testvoid testUserInput(){WebElement webElement=chromeDriver.findElement(By.cssSelector("#username"));Assertions.assertNotNull(webElement);}
测试注册密码输入框
/*** 测试密码输入框*/@Testvoid testPasswordInput(){WebElement webElement=chromeDriver.findElement(By.cssSelector("#password"));Assertions.assertNotNull(webElement);}
测试注册提交按钮
/*** 测试提交按钮*/@Testvoid testSubmit(){WebElement webElement=chromeDriver.findElement(By.cssSelector("#submit"));Assertions.assertNotNull(webElement);}
测试注册提交按钮文字
/*** 测试提交按钮的文字*/@Testvoid testSubmitText(){String submitText=chromeDriver.findElement(By.cssSelector("#submit")).getText();Assertions.assertEquals(submitText,"提交");}
测试结果:
游戏大厅页面
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:9999/game_hall.html");}
测试匹配按钮
/*** 测试按钮*/@Testvoid testButton(){WebElement button=chromeDriver.findElement(By.cssSelector("#match-button"));Assertions.assertNotNull(button);}
测试匹配按钮上的文字
/*** 测试按钮上的文字*/@Testvoid testButtonText(){String buttonText=chromeDriver.findElement(By.cssSelector("#match-button")).getText();Assertions.assertEquals(buttonText,"开始匹配");}
测试导航栏
/*** 测试导航栏*/@Testvoid testNav(){WebElement nav=chromeDriver.findElement(By.cssSelector("body > div.nav"));Assertions.assertNotNull(nav);}
测试导航栏上的文字
/*** 测试导航栏上的文字*/@Testvoid testNavText(){String navText=chromeDriver.findElement(By.cssSelector("body > div.nav")).getText();Assertions.assertEquals(navText,"五子棋对战");}
测试用户信息块
/*** 测试用户信息块*/@Testvoid testUserInfo(){WebElement screen=chromeDriver.findElement(By.cssSelector("#screen"));Assertions.assertNotNull(screen);}
测试结果:
游戏房间页面
首先定义浏览器对象和url
/*** 1、浏览器对象* 2、访问登录页面的url*/public static ChromeDriver chromeDriver=createChromeDriver();@BeforeAllstatic void baseControl(){chromeDriver.get("http://49.232.185.203:9999/game_room.html");}
测试棋盘
/*** 测试棋盘*/@Testvoid testChess(){WebElement chess=chromeDriver.findElement(By.cssSelector("#chess"));Assertions.assertNotNull(chess);}
测试导航栏
/*** 测试导航栏*/@Testvoid testNav(){WebElement nav=chromeDriver.findElement(By.cssSelector("body > div.nav"));Assertions.assertNotNull(nav);}
测试导航栏上的文字
/*** 测试导航栏上的文字*/@Testvoid testNavText(){String navText=chromeDriver.findElement(By.cssSelector("body > div.nav")).getText();Assertions.assertEquals(navText,"五子棋对战");}
测试提示语框
/*** 测试提示语框*/@Testvoid testScreen(){WebElement screen=chromeDriver.findElement(By.cssSelector("#screen"));Assertions.assertNotNull(screen);}
测试结果:
总结:目前针对网页版五子棋的测试如上述博客,由于部分用例无法进行自动化测试,所以进行相应的人工测试,且测试通过
这篇关于网页版五子棋的自动化测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!