当前位置: 首页 > news >正文

五子棋项目自动化测试

目录

一、五子棋项目介绍

二、编写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、运行程序

        结果如下:

        有一个警告,但不影响程序的运行,可以不用理会。

        测试用例全部都能通过。


http://www.mrgr.cn/news/48513.html

相关文章:

  • 电脑无线网wifi和有线网同时使用(内网+外网同时使用)
  • 【Linux 从基础到进阶】防止数据泄露的策略与工具
  • 【WebGIS】Cesium:天地图加载
  • 每日OJ题_牛客_比那名居的桃子_滑动窗口/前缀和_C++_Java
  • C语言—双链表
  • 科大讯飞嵌入式面试题及参考答案
  • c++ 多线程全局变量安全操作------原子操作
  • 网工内推 | 初级网工,Base北京,IE认证优先,最高14K+餐补
  • Feign的使用
  • 【专题】智启未来:新质生产力引擎驱动下的智能制造行业革新报告合集PDF分享(附原数据表)
  • 邮件营销案例成功技巧:如何打动目标客户?
  • 18063 圈中的游戏
  • 探索极简计算的新边界:从Uxn虚拟机看未来编程生态
  • 儿童画画在线支付预约报名表单在线制作小程序源码系统 带完整的安装代码包以及搭建部署教程
  • 思迅商云8四级分类
  • 哪个牌子的护眼灯防蓝光效果好?五款市场上评价较高的护眼台灯
  • xtu oj 彩球
  • 自监督学习:引领机器学习的新革命
  • Java Mail腾讯企业邮箱或其他邮箱发送邮件失败bug记录
  • MySQL的基础语法-2