网页聊天——测试报告——Selenium自动化测试
一,项目概括
1.1 项目名称
网页聊天
1.2 测试时间
2024.9
1.3 编写目的
对编写的网页聊天项目进行软件测试活动,揭示潜在问题,总结测试经验
二,测试计划
2.1 测试环境与配置
服务器:云服务器 ubuntu_22
PC机:机械师T58-V
平台:Windows 10
浏览器:Chrome、FireFox、Edge
自动化测试工具:Selenium
2.2 提取测试点
2.3 设计测试用例
所有的测试用例绑定在这个帖子上了,需要自取
Selenium自动化执行测试用例
文件分类-- 功能进行分块
main函数
public static void main(String[] args) throws IOException, InterruptedException {
noLoginTest noLogin=new noLoginTest();
noLogin.NoLoginOpen();
registerTest register_test=new registerTest();
register_test.registerTest();
LoginTest login_test=new LoginTest();
login_test.loginTest();
friendListTest friendList_test=new friendListTest();
friendList_test.ListTest();
addFriendTest addFriendTest =new addFriendTest();
addFriendTest.addFriendTest();
chatTest chat=new chatTest();
chat.chatTest();
driver.quit();
}
Utils--所有公共模块
public class Utils {
public static WebDriver driver;
public static WebDriver createDriver(){//创建驱动
if(driver!=null){
return driver;
}
WebDriverManager.edgedriver().setup();//自动下载配置驱动程序
EdgeOptions edgeOptions=new EdgeOptions();
edgeOptions.addArguments("--allow-remote-origins=*");
driver=new EdgeDriver(edgeOptions);//用option创建驱动
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));//隐式等待
return driver;
}
public static void getScreenShot(String str) throws IOException {//截图
SimpleDateFormat simple1=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat simple2=new SimpleDateFormat("hhmmssSS");
String format = simple1.format(System.currentTimeMillis());
String format2 = simple2.format(System.currentTimeMillis());
String fileName="./src/test/image/"+format+"/"+str+"-"+format2+".png";
File screenshotAs = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotAs,new File(fileName));//将截图放到新建的文件里
}
public static String getTime(){//用来做测试数据
SimpleDateFormat simple1=new SimpleDateFormat("hh:mm:ss");
return simple1.format(System.currentTimeMillis())+"test";
}
}
registerTest--注册测试
public void registerFailAll() throws IOException {
//注册失败
String alertTest = register("zhangsan", "123");
assertEquals("用户名被占用!",alertTest);//断言判断
alertTest=register("","123");
assertEquals("用户名和密码不能为空! ",alertTest);
alertTest=register("zhangsan","");
assertEquals("用户名和密码不能为空! ",alertTest);
alertTest=register("","");
assertEquals("用户名和密码不能为空! ",alertTest);
}
public void registerSucAll() throws IOException {
//注册成功
String alertTest = register(testUserName, "123");
assertEquals("注册成功",alertTest);
driver.get("http://localhost:8080/login.html");
driver.findElement(By.cssSelector("#submit2")).click();
alertTest=register(testUserName+1,"123");
assertEquals("注册成功",alertTest);
}
public String register(String username,String password) throws IOException {//注册
driver.findElement(By.cssSelector("#userName")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#userName")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
getScreenShot(getClass().getName());
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
String text = driver.switchTo().alert().getText();
alert.accept();
return text;
}
loginTest--登录测试
public void LoginFailAll() throws IOException, InterruptedException {
//登录失败
String alertTest = loginUtil("zhang", "123");
assertEquals("用户名或密码错误! ",alertTest);
alertTest=loginUtil("zhangsan","1");
assertEquals("用户名或密码错误! ",alertTest);
alertTest=loginUtil("zhangsan","");
assertEquals("当前输入用户名或密码为空! ",alertTest);
alertTest=loginUtil("","123");
assertEquals("当前输入用户名或密码为空! ",alertTest);
alertTest=loginUtil("","");
assertEquals("当前输入用户名或密码为空! ",alertTest);
}
public void LoginSucAll() throws IOException, InterruptedException {
//成功
loginSucUtil("zhangsan", "123");
loginSucUtil("wangwu","123");
loginSucUtil("lisi","123");
}
public void loginSucUtil(String username,String password) throws IOException, InterruptedException {
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("123");
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.urlToBe("http://localhost:8080/client.html"));
getScreenShot(getClass().getName());//截图最好放在最后一步,不然容易卡
driver.navigate().back();
}
public String loginUtil(String username,String password) throws IOException, InterruptedException {
driver.findElement(By.cssSelector("#userName")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#userName")).sendKeys(username);
driver.findElement(By.cssSelector("#password")).sendKeys(password);
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.alertIsPresent());
String text = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
getScreenShot(getClass().getName());
return text;
}
登录和注册的自动化效果展示
friendListTest--主页和好友列表测试
主要检查主页点击用户是否会跳转到聊天页
好友列表点击是否会跳到聊天页
@Slf4j
public class friendListTest extends Utils {
public static WebDriver driver=Utils.createDriver();
WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(5));
public void ListTest() throws IOException {
openWeb();
ChatListUI();
FriendListUI();
}
public void openWeb() throws IOException {
driver.get("http://localhost:8080/login.html");
driver.findElement(By.cssSelector("#userName")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#userName")).sendKeys("lisi");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.urlToBe("http://localhost:8080/client.html"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-session"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button"));
getScreenShot(getClass().getName());
log.info("打开主页");
}
public void ChatListUI() throws IOException {
wait.until(ExpectedConditions.urlToBe("http://localhost:8080/client.html"));
driver.findElement(By.cssSelector("#session-list"));//
String friendName = driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3")).getText();
driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3")).click();
String friendChat = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
assertEquals(friendName,friendChat);//断言一下
getScreenShot(getClass().getName());
friendName = driver.findElement(By.cssSelector("#session-list > li:nth-child(2) > h3")).getText();
driver.findElement(By.cssSelector("#session-list > li:nth-child(2) > h3")).click();
friendChat = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
assertEquals(friendName,friendChat);//断言一下
getScreenShot(getClass().getName());
friendName = driver.findElement(By.cssSelector("#session-list > li:nth-child(3) > h3")).getText();
driver.findElement(By.cssSelector("#session-list > li:nth-child(3) > h3")).click();
friendChat = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
assertEquals(friendName,friendChat);//断言一下
getScreenShot(getClass().getName());
log.info("主页用户点击切换到聊天测试");
}
public void FriendListUI() throws IOException {//查看是否点击可以跳转
driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend")).click();
driver.findElement(By.cssSelector("#friend-list"));
String friendName = driver.findElement(By.cssSelector("#friend-list > li:nth-child(1)")).getText();
driver.findElement(By.cssSelector("#friend-list > li:nth-child(1)")).click();
String friendChat = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
assertEquals(friendName,friendChat);//断言一下
getScreenShot(getClass().getName());
log.info("好友列表点击跳转测试");
}
addFriendTest--搜索好友,发送申请,新好友聊天测试
主页加载完整
搜索好友功能测试
发送申请功能测试
收到申请测试
同意申请,拒绝申请测试
新好友聊天测试
public class addFriendTest extends Utils {
public static WebDriver driver=Utils.createDriver();
public String addRequest=getTime()+"addFriend";
WebDriverWait wait=new WebDriverWait(driver, Duration.ofSeconds(5));
public void addFriendTest() throws IOException {
openWeb();
addFriendSend(registerTest.testUserName);//搜索刚注册的账号
addFriendAccept(registerTest.testUserName);
addFriendChat();
addFriendSend("test");//模糊搜索
addFriendSend("0000");
addFriendSend("");
addFriendSend(" ");
addFriendSend("lisi");//搜本人
}
public void openWeb() throws IOException {
driver.get("http://localhost:8080/login.html");
driver.findElement(By.cssSelector("#userName")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#userName")).sendKeys("lisi");
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.urlToBe("http://localhost:8080/client.html"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-session"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea"));
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button"));
getScreenShot(getClass().getName());
System.out.println("加好友UI");
}
public void addFriendSend(String user) throws IOException {
//搜索刚刚注册的账号
driver.findElement(By.cssSelector("#userNmae")).clear();
driver.findElement(By.cssSelector("#userNmae")).sendKeys(user);
driver.findElement(By.cssSelector("#sumbit")).click();
// driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show > div"));
try{
//查看搜到的用户是否正确
// System.out.println(registerTest.testUserName+"查看搜到的用户是否正确");
// assertEquals(register_test.testStr,driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div[2]/div/span")).getText());
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show > div>input")).sendKeys(addRequest);
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show > div:nth-child(1) > button")).click();
assertEquals(driver.switchTo().alert().getText(),"添加成功");
driver.switchTo().alert().accept();
getScreenShot(getClass().getName());
System.out.println("添加好友请求发送成功");
}catch (Exception e){
System.out.println("没找到好友:"+user);
}
}
public void addFriendAccept(String user) throws IOException {
//切号
//登录聊天对象账号查看短信发送情况
driver.get("http://localhost:8080/login.html");
driver.findElement(By.cssSelector("#userName")).clear();
driver.findElement(By.cssSelector("#password")).clear();
// System.out.println(receiver+" login");
driver.findElement(By.cssSelector("#userName")).sendKeys(user);
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
System.out.println("切号查看请求是否正确");
//提示好友申请发来了
driver.findElement(By.cssSelector("#session-list > li"));
//发送申请的对象是对的
assertEquals("lisi",driver.findElement(By.cssSelector("#session-list > li > h3")).getText());
//发送申请的话是对的,因为展示的是部分内容所以不能断言
// assertEquals(addRequest,driver.findElement(By.cssSelector("#session-list > li > p")).getText());
//接受申请
driver.findElement(By.cssSelector("#session-list > li > button:nth-child(3)")).click();
//添加成功提示正确
assertEquals("添加成功",driver.switchTo().alert().getText());
driver.switchTo().alert().accept();
System.out.println("添加好友成功");
getScreenShot(getClass().getName());
}
public void addFriendChat() throws IOException {
System.out.println("发短信测试");
//测试新好友发短信
//找到列表里的那个
driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend")).click();
//可以Xpath选择好友列表页的指定对象
driver.findElement(By.xpath("//ul[@id='friend-list']/li[h4[text()='"+"lisi"+"']]")).click();
//点击发短信
String testChat = getTime();
//点击聊天输入框
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).click();
//发送方
String sender = driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user")).getText();
//接收方
String receiver = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys(testChat);
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();
driver.navigate().refresh();
driver.findElement(By.cssSelector("#session-list > li:first-child")).click();
String checkTest = driver.findElement(By.cssSelector("div.message-show div.message:last-child div.box p")).getText();
assertEquals(testChat,checkTest);
getScreenShot(getClass().getName());
}
chat---给好友发短信和接收短信的功能测试
测发送不同的内容
测试能否接收到
public class chatTest extends Utils {
WebDriver driver=Utils.createDriver();
WebDriverWait wait=new WebDriverWait(driver, Duration.ofSeconds(5));
public void chatTest() throws IOException {
driver.get("http://localhost:8080/login.html");
driver.findElement(By.cssSelector("#userName")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#userName")).sendKeys(testUserName);//发送方
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
wait.until(ExpectedConditions.urlToBe("http://localhost:8080/client.html"));
//发短信功能,选择最上面的聊天对象,编辑,发送,刷新(必须,加载新短信)重新选择最上面的聊天对象,
// 选择最下面最新的短信,注意父类包含多个子类情况下,可以问ai
// String testChat = getTime();
// System.out.println(testChat);
// System.out.println(testChat);
//点击好友列表第一位
driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend")).click();
driver.findElement(By.cssSelector("#friend-list > li:nth-child(1)")).click();
chat("哈哈哈哈");
chat("hhhhha");
chat("111112");
chat("!@#$%^&*蛤ha");
String longStr ="在思想上,我具有较高的政治觉悟,积极向党组织靠拢,始终保持着进取之心,以坚定的信念追求进步。 在学习上,我态度严谨认真,勤奋刻苦。凭借不懈的努力,连续两年荣获国家励志奖学金,并且在英语赛事中多次获奖,扎实的专业功底让我能够自信地应对各种学业挑战。";
String receiver=chat(longStr);
driver.navigate().back();//退出登录
checkMessage(longStr,receiver);//检查短信是否收到
}
public String chat(String message) throws IOException {//发送短信并返回接受方
driver.findElement(By.cssSelector("#session-list > li")).click();//找到好友列表第一个,点击聊天并记录
String receiver = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
//编辑短信发送,并返回接受短信的用户
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys(message);
driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();
log.info("发送短信并刷新");
return receiver;
}
public void checkMessage(String message,String receiver) throws IOException {//登录接收方账号,检查短信是否正确
driver.findElement(By.cssSelector("#userName")).clear();
driver.findElement(By.cssSelector("#password")).clear();
driver.findElement(By.cssSelector("#userName")).sendKeys(receiver);
driver.findElement(By.cssSelector("#password")).sendKeys("123");
driver.findElement(By.cssSelector("#submit")).click();
//点击好友列表
driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend")).click();
//可以Xpath选择好友列表页的指定对象
driver.findElement(By.xpath("//ul[@id='friend-list']/li[h4[text()='"+testUserName+"']]")).click();
//报错无效选择器,前端找到对应的元素后再次查找发现是元素不可交互导致的
log.info("切换账号查看短信是否收到");
String test2 = driver.findElement(By.cssSelector("div.message-show div.message:last-child div.box p")).getText();//查看短信
assertEquals(message,test2);//断言一下
getScreenShot(getClass().getName());
log.info("短信成功收到");
}
noLoginTest--没有登录测试
查看没有登录直接访问是否会强制返回
public class noLoginTest {
public void NoLoginOpen() throws IOException {
WebDriver driver= Utils.createDriver();
driver.get("http://localhost:8080/client.html");
WebDriverWait wait=new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.alertIsPresent());
assertEquals("当前用户未登录!",driver.switchTo().alert().getText());
driver.switchTo().alert().accept();
getScreenShot(getClass().getName());
}
}
整个自动化测试展示
网页自动化测试