自动化测试之网页聊天室

一、测试背景

随着互联网的快速发展,即时通讯工具已成为人们日常生活和工作中不可或缺的一部分。网页聊天室作为一种轻量级的即时通讯平台,因其无需安装额外软件、跨平台兼容性强、使用便捷等特点,受到了广大用户的青睐。本测试报告旨在对网页聊天室进行全面测试,以确保其稳定性、安全性、易用性等方面均能满足用户需求。


二、测试用例设计 

 


三、自动化测试

1. 在 pom.xml 中添加相关依

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>webChatAutoTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.1.0</version>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

 


2. 编写自动化脚本

2.1 创建驱动工具类

package common;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
public class DriverUtils {
    public static WebDriver driver;
    public static WebDriver createDriver() {
        if (driver == null) {
            WebDriverManager.chromedriver().setup();
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--remote-allow-origins=*");
            driver = new ChromeDriver(options);
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        }
        return driver;
    }
    public DriverUtils(String url) {
        driver = createDriver();
        driver.get(url);
    }
    public void takeScreenshot(String fileName) throws IOException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String timestamp = dateFormat.format(System.currentTimeMillis());
        String filePath = "./screenshots/" + timestamp + "_" + fileName + ".png";
        File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotFile, new File(filePath));
    }
}

 


 

2.2 登录页面测试

package tests;
import common.DriverUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import java.io.IOException;
public class LoginPageTest extends DriverUtils {
    public static String url = "http://127.0.0.1:8080/login.html";
    public LoginPageTest() {
        super(url);
    }
    public void testLoginPageSuccess() throws InterruptedException, IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("validUser");
        driver.findElement(By.cssSelector("#password")).sendKeys("validPassword");
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(3000);
        Alert alert = driver.switchTo().alert();
        alert.accept();
        Thread.sleep(5000);
        String expectedText = "Send";
        String actualText = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).getText();
        assert actualText.equals(expectedText);
        takeScreenshot("LoginPageSuccess");
    }
    public void testLoginPageFailure() throws InterruptedException {
        driver.findElement(By.cssSelector("#username")).sendKeys("invalidUser");
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(3000);
        Alert alert = driver.switchTo().alert();
        alert.accept();
        driver.findElement(By.cssSelector("#password")).sendKeys("wrongPassword");
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(3000);
        Alert alert1 = driver.switchTo().alert();
        alert1.accept();
    }
}

 

 


 

2.3 聊天主页测试 

package tests;
import common.DriverUtils;
import org.openqa.selenium.By;
import java.io.IOException;
public class ChatClientPageTest extends DriverUtils {
    public static String url = "http://127.0.0.1:8080/client.html";
    public ChatClientPageTest() {
        super(url);
    }
    public void testChatClientPage() throws IOException, InterruptedException {
        driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3"));
        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"));
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend"));
        takeScreenshot("ChatClientPage");
    }
}

2.4、添加好友功能测试

package tests;
import common.DriverUtils;
import org.openqa.selenium.By;
import java.io.IOException;

public class FriendManagementTest extends DriverUtils {
    public static String url = "http://127.0.0.1:8080/client.html";
    public FriendManagementTest() {
        super(url);
    }

    public void testAddFriend() throws IOException, InterruptedException {
        // 添加好友
        driver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > button")).click();
        Thread.sleep(3000);
        // 验证好友是否添加成功
        String expectedText = "NewFriend";
        String actualText = driver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > span")).getText();
        assert actualText.equals(expectedText);
        takeScreenshot("AddFriendSuccess");
    }

    public void testRemoveFriend() throws IOException, InterruptedException {
        // 删除好友
        driver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > button")).click();
        Thread.sleep(3000);
        // 验证好友是否删除成功
        String expectedText = "Add Friend";
        String actualText = driver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > button")).getText();
        assert actualText.equals(expectedText);
        takeScreenshot("RemoveFriendSuccess");
    }
}

2.5 、聊天消息测试

package tests;
import common.DriverUtils;
import org.openqa.selenium.By;
import java.io.IOException;

public class ChatMessageTest extends DriverUtils {
    public static String url = "http://127.0.0.1:8080/client.html";
    public ChatMessageTest() {
        super(url);
    }

    public void testSendMessage() throws IOException, InterruptedException {
        // 发送消息
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys("Hello, how are you?");
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();
        Thread.sleep(3000);
        // 验证消息是否发送成功
        String expectedText = "Hello, how are you?";
        String actualText = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-list > div:last-child > div.message-content")).getText();
        assert actualText.equals(expectedText);
        takeScreenshot("SendMessageSuccess");
    }

    public void testReceiveMessage() throws IOException, InterruptedException {
        // 接收消息
        // 假设其他用户发送消息
        Thread.sleep(3000);
        // 验证消息是否接收成功
        String expectedText = "Hello, how are you?";
        String actualText = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-list > div:last-child > div.message-content")).getText();
        assert actualText.equals(expectedText);
        takeScreenshot("ReceiveMessageSuccess");
    }
}

 2.6、搜索功能测试

package tests;
import common.DriverUtils;
import org.openqa.selenium.By;
import java.io.IOException;

public class SearchTest extends DriverUtils {
    public static String url = "http://127.0.0.1:8080/client.html";
    public SearchTest() {
        super(url);
    }

    public void testSearchUser() throws IOException, InterruptedException {
        // 搜索用户
        driver.findElement(By.cssSelector("#search-input")).sendKeys("testUser");
        driver.findElement(By.cssSelector("#search-button")).click();
        Thread.sleep(3000);
        // 验证搜索结果是否正确
        String expectedText = "testUser";
        String actualText = driver.findElement(By.cssSelector("#search-results > li > span")).getText();
        assert actualText.equals(expectedText);
        takeScreenshot("SearchUserSuccess");
    }

    public void testSearchMessage() throws IOException, InterruptedException {
        // 搜索消息
        driver.findElement(By.cssSelector("#search-input")).sendKeys("Hello");
        driver.findElement(By.cssSelector("#search-button")).click();
        Thread.sleep(3000);
        // 验证搜索结果是否正确
        String expectedText = "Hello, how are you?";
        String actualText = driver.findElement(By.cssSelector("#search-results > li > div.message-content")).getText();
        assert actualText.equals(expectedText);
        takeScreenshot("SearchMessageSuccess");
    }
}

 2.7、弱网测试

package tests;
import common.DriverUtils;
import org.openqa.selenium.By;
import java.io.IOException;

public class WeakNetworkTest extends DriverUtils {
    public static String url = "http://127.0.0.1:8080/client.html";
    public WeakNetworkTest() {
        super(url);
    }

    public void testSendMessageUnderWeakNetwork() throws IOException, InterruptedException {
        // 模拟弱网环境
        driver.manage().network().emulateNetworkConditions(true, 100, 100, 100);
        // 发送消息
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys("Hello, how are you?");
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();
        Thread.sleep(3000);
        // 验证消息是否发送成功
        String expectedText = "Hello, how are you?";
        String actualText = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-list > div:last-child > div.message-content")).getText();
        assert actualText.equals(expectedText);
        takeScreenshot("SendMessageUnderWeakNetwork");
    }

    public void testReceiveMessageUnderWeakNetwork() throws IOException, InterruptedException {
        // 模拟弱网环境
        driver.manage().network().emulateNetworkConditions(true, 100, 100, 100);
        // 假设其他用户发送消息
        Thread.sleep(3000);
        // 验证消息是否接收成功
        String expectedText = "Hello, how are you?";
        String actualText = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-list > div:last-child > div.message-content")).getText();
        assert actualText.equals(expectedText);
        takeScreenshot("ReceiveMessageUnderWeakNetwork");
    }
}

 

三、测试总结

3.1、测试范围

本次测试涵盖了网页聊天室的主要功能模块,包括但不限于

  • 登录功能 :验证用户登录的正确性和异常情况处理。

  • 聊天功能 :测试消息发送、接收以及不同格式消息的显示。

  • 会话管理 :检查会话列表和好友列表的显示与交互。

  • 搜索功能 :验证用户和会话的搜索功能。

  • 界面测试 :评估聊天室页面的布局和样式在不同浏览器和分辨率下的表现。

  • 性能测试 :测试消息延迟和页面加载时间。

  • 兼容性测试 :确保在不同浏览器和设备上都能正常工作。

  • 安全测试 :检查数据加密和用户权限控制。

  • 易用性测试 :评估操作流程和提示信息的清晰度。

  • 弱网测试 :模拟网络不稳定情况下的消息发送和接收,以及断网重连功能。

3.2、问题发现

  • 登录功能

  • 当输入错误密码时,提示信息不够明确,用户无法快速定位问题。

  • 聊天功能

  • 发送超长消息时,系统没有进行有效的截断处理,导致消息显示异常。

  • 特殊字符消息在某些浏览器上显示不正确。

  • 会话管理

  • 会话列表在大量会话情况下出现滚动卡顿现象。

  • 好友列表更新不及时,当好友状态发生变化时,需要刷新页面才能看到最新状态。

  • 搜索功能

  • 搜索用户时,搜索结果有时会出现重复项。

  • 搜索消息功能对大小写敏感,无法搜索到包含相同字母但大小写不同的消息。

  • 界面测试

  • 在某些低分辨率屏幕上,页面元素会出现重叠现象。

  • 不同浏览器下,部分样式存在细微差异,影响美观。

  • 安全测试

  • 聊天消息的加密强度有待提高,存在被破解的风险。

  • 未登录用户在某些情况下可以通过直接输入 URL 访问聊天室部分页面。

  • 易用性测试

  • 操作流程较为复杂,新用户上手困难,需要提供更详细的引导。

  • 系统提示信息有时不够清晰,容易引起用户误解。

 

3.3、改进建议

  1. 优化提示信息 :确保所有提示信息准确、清晰,帮助用户快速理解问题并采取相应措施。

  2. 加强数据验证 :对输入数据进行严格验证,避免无效数据导致系统异常。

  3. 提升性能 :优化服务器性能,减少消息延迟,压缩页面资源,加快加载速度。

  4. 完善兼容性 :针对不同浏览器和设备进行专项测试和优化,确保兼容性。

  5. 增强安全性 :采用更高级的加密算法,加强对用户权限的控制。

  6. 优化界面布局 :针对不同分辨率屏幕进行适配,确保页面元素布局合理,样式统一。

  7. 改进弱网处理 :优化网络请求逻辑,提高弱网环境下消息传输的可靠性,完善断网重连机制。

  8. 简化操作流程 :对操作流程进行梳理,简化步骤,提供新手引导和帮助文档。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值