网页版聊天室--自动化测试项目实战

       

目录

一、编写测试用例

​编辑 二、创建maven项目

 三、根据页面及功能分别进行自动化测试

 1.注册页面的自动化测试

 2.登录页面的自动化测试

3.主界面的自动化测试

4.好友列表的自动化测试 

5.会话列表的自动化测试 

6.聊天编辑框的自动化测试 

总代码:


       本次自动化测试主要是功能测试,我面对注册、登录、主界面、会话列表、好友列表、消息传输区域分别进行测试。

一、编写测试用例

 二、创建maven项目

在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>org.example</groupId>
    <artifactId>chartroom_Test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.10.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.10.2</version>
        </dependency>



    </dependencies>

</project>

 三、根据页面及功能分别进行自动化测试

 1.注册页面的自动化测试

 

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;



import static java.lang.Thread.sleep;


/**
 * 注册界面的自动化测试
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class registerTest {
    /**
     * 初始化
     */
    static WebDriver webDriver;

    @BeforeAll
    static void SetUp(){
        webDriver = new ChromeDriver();
        webDriver.get("http://127.0.0.1:8081/register.html");
    }

    /**
     * 测试页面完整性
     */
    @Test
    @Order(1)
    public void registerPageTest(){
        //断言显示的文本正确
        String except = "注册";
        String actual = webDriver.findElement(By.cssSelector("body > div.login-container > div > h3")).getText();
        Assertions.assertEquals(except,actual);

        //注册按钮是否存在
        webDriver.findElement(By.cssSelector("#submit"));
    }

    /**
     * 成功注册
     */
    @ParameterizedTest
    @Order(2)
    @CsvSource({"teacher,111"})
    public void registerSuccess(String username,String password) throws InterruptedException {
        //输入前先清空输入框
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        sleep(3000);
        //输入正确的用户名和密码
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        //强制休息3秒
        sleep(3000);
        //点击注册按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);

        // 等待弹窗出现
        Alert alert = webDriver.switchTo().alert();
        alert.accept();//点击弹窗中的确定,使程序继续执行下去
        sleep(3000);

        //注册成功跳转到登录页面
        String expectUrl = "http://127.0.0.1:8081/login.html";
        String actualUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(expectUrl,actualUrl);

        //后续测试还需要在注册界面,回退到注册界面
        webDriver.navigate().back();
        sleep(3000);
    }
    /**
     * 注册失败
     */
    @ParameterizedTest
    @Order(3)
    @CsvSource({"zhangsan,123","cool,''","'',123","'',''"})
    void registerFailed(String username,String password) throws InterruptedException {
            // 输入前先清空输入框
            webDriver.findElement(By.cssSelector("#username")).clear();
            webDriver.findElement(By.cssSelector("#password")).clear();


            // 输入用户名和密码
            webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
            webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
            sleep(3000);
            // 点击注册按钮
            webDriver.findElement(By.cssSelector("#submit")).click();

            sleep(6000);
            // 处理弹窗
            Alert alert = webDriver.switchTo().alert();
            String expectText = (username.isEmpty() || password.isEmpty())? "您输入的用户名或密码为空!" : "注册失败!";
            String actualText = alert.getText();
            Assertions.assertEquals(expectText, actualText);
            alert.accept();


    }


    @AfterAll
    static void close(){
        webDriver.quit();
    }
}

 测试通过

 2.登录页面的自动化测试

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.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import static java.lang.Thread.sleep;

/**
 * 登录页面的自动化测试
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class loginTest {
    /**
     * 初始化
     */
    static WebDriver webDriver;
    @BeforeAll
    static void SetUp(){
        webDriver = new ChromeDriver();
        webDriver.get("http://127.0.0.1:8081/login.html");
    }

    /**
     * 页面完整性检验
     */
    @Test
    @Order(1)
    public void loginPageTest() throws InterruptedException {
        //断言显示的文本正确
        String expectText = "登录";
        String actualText = webDriver.findElement(By.cssSelector("body > div.login-container > div > h3")).getText();
        Assertions.assertEquals(expectText,actualText);

        //登录按钮是否存在
        webDriver.findElement(By.cssSelector("#submit"));
        sleep(3000);
    }
    /**
     * 登录不成功
     * 输入未注册的用户名
     */
    @ParameterizedTest
    @Order(2)
    @CsvSource({"xiaomei,123"})
    public void loginFailed(String username,String password) throws InterruptedException {
        //先清空输入框
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        //输入未注册的用户名
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(6000);
        //断言登录失败
        Alert alert = webDriver.switchTo().alert();
        String expectLiterals = "登录失败!";
        String actualLiterals = alert.getText();
        Assertions.assertEquals(expectLiterals,actualLiterals);
        alert.accept();

    }

    /**
     * 登录成功
     */
    @ParameterizedTest
    @Order(3)
    @CsvSource({"zhangsan,123"})
    public void loginSuccess(String username,String password) throws InterruptedException {
        //先清空输入框
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        //输入正确的用户名和密码
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        //点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(6000);
        //获取弹窗
        Alert alert = webDriver.switchTo().alert();
        alert.accept();//点击弹窗中的确定,使代码继续运行
        sleep(3000);

        //登录成功跳转到会话界面
        String exceptUrl = "http://127.0.0.1:8081/client.html";
        String actualUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(exceptUrl,actualUrl);

    }

    @AfterAll
    static void close(){
        webDriver.quit();
    }


}

测试通过

3.主界面的自动化测试

 

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.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import static java.lang.Thread.sleep;

/**
 * 主界面的自动化测试
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class mainInterfaceTest {
    /**
     * 初始化
     */
    static WebDriver webDriver = new ChromeDriver();

    /**
     * 未登录状态下直接访问会话主界面"http://127.0.0.1:8081/client.html"
     */
    @Test
    @Order(1)
    public void NotLoggedIn() throws InterruptedException {
        //直接访问主界面
        webDriver.get("http://127.0.0.1:8081/client.html");
        sleep(6000);
        //获取弹窗
        Alert alert = webDriver.switchTo().alert();
        System.out.println(alert.getText());
        alert.accept();
        //此时应当跳转至登录界面,通过断言来判断
        String expectUrl = "http://127.0.0.1:8081/login.html";
        String actualUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(expectUrl,actualUrl);
        //等待后续登录状态下的测试
        sleep(6000);
    }

    /**
     * 登录状态下检测页面是否正常
     */
    @ParameterizedTest
    @Order(2)
    @CsvSource({"zhangsan,123"})
    public void Login(String username,String password) throws InterruptedException {
        webDriver.get("http://127.0.0.1:8081/login.html");
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        //输入正确的用户名和密码
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        //点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(6000);
        //获取弹窗
        Alert alert = webDriver.switchTo().alert();
        alert.accept();//点击弹窗中的确定,使代码继续运行
        sleep(3000);

        /**
         * 检测页面完整性
         */
        //当前登录的用户名
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user"));
        //会话图标
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-session"));
        //好友列表图标
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend"));
        //发送按钮
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button"));

    }
    /**
     * 退出
     */
    @AfterAll
    static void close(){
        webDriver.quit();
    }
}

测试通过

4.好友列表的自动化测试 

import org.junit.jupiter.api.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static java.lang.Thread.sleep;

/**
 * 好友列表的自动化测试
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class friendListTest {
    static WebDriver webDriver = new ChromeDriver();

    /**
     * 确保正常登录
     */
    @BeforeAll
    static void login() throws InterruptedException {
        webDriver.get("http://127.0.0.1:8081/login.html");
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        //输入正确的用户名和密码
        webDriver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        //点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(6000);
        //获取弹窗
        Alert alert = webDriver.switchTo().alert();
        alert.accept();//点击弹窗中的确定,使代码继续运行
    }
    /**
     * 确保点击好友列表图标跳转到好友列表,且通过截图获取好友用户名为“lisi”、“wangwu”、“zhaoliu”
     */
    @Test
    @Order(1)
    public void jumpSuccess() throws IOException {
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend")).click();
        //截图
        TakesScreenshot takesScreenshot = (TakesScreenshot) webDriver;
        File screenshot = takesScreenshot.getScreenshotAs(OutputType.FILE);
        //将截图保存到指定位置
        Files.copy(screenshot.toPath(), Paths.get("friendsList.png"));
        //好友列表第一个好友是lisi
        String expectFriend = "lisi";
        String actualFriend = webDriver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > h4")).getText();
        Assertions.assertEquals(expectFriend,actualFriend);
    }
    /**
     * 点击“lisi”这个好友跳转到会话列表,且右侧聊天编辑框显示与该好友的聊天
     */
    @Test
    @Order(2)
    public void FriendsJumpToAConversation(){
        webDriver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > h4")).click();

        //聊天编辑框最上方显示“lisi”
        String expectText = "lisi";
        String actualText = webDriver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
        Assertions.assertEquals(expectText,actualText);
        //会话列表第一个用户名是lisi
        String expect = "lisi";
        String actual = webDriver.findElement(By.cssSelector("#session-list > li.selected > h3")).getText();
        Assertions.assertEquals(expectText,actualText);
    }

    /**
     * 退出
     */
    @AfterAll
    static void close(){
        webDriver.quit();
    }
}

测试通过 

5.会话列表的自动化测试 

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static java.lang.Thread.sleep;

/**
 * 会话列表的自动化测试
 */
public class conversationListTest {
    static WebDriver webDriver = new ChromeDriver();
    /**
     * 确保登录状态
     */
    @BeforeAll
    static void login() throws InterruptedException {
        webDriver.get("http://127.0.0.1:8081/login.html");
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        //输入正确的用户名和密码
        webDriver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        //点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(6000);
        //获取弹窗
        Alert alert = webDriver.switchTo().alert();
        alert.accept();//点击弹窗中的确定,使代码继续运行
    }

    /**
     *点击会话图标会跳转到会话列表,通过截图获取
     *会话列表上面是用户名,下面是消息文本
     *点击“lisi”,右面会跳转到与lisi的聊天编辑框
     */
    @Test
    public void jumpSuccess() throws IOException {
        //点击会话图标会跳转到会话列表,通过截图获取
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-session")).click();
        //截图
        TakesScreenshot screenshotTaker = (TakesScreenshot) webDriver;
        File screenshot = screenshotTaker.getScreenshotAs(OutputType.FILE);
        //将截图保存到指定位置
        Files.copy(screenshot.toPath(), Paths.get("sessionList.png"));

        //会话列表上面是用户名,下面是消息文本
        System.out.println(webDriver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3")).getText());
        System.out.println(webDriver.findElement(By.cssSelector("#session-list > li:nth-child(1) > p")).getText());

        //点击“lisi”,右面会跳转到与lisi的聊天编辑框
        webDriver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3")).click();
        //聊天框最上方显示lisi
        String expectTitle = "zhaoliu";
        String actualTitle = webDriver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
        Assertions.assertEquals(expectTitle,actualTitle);

    }

    /**
     * 退出
     */
    @AfterAll
    static void close(){
        webDriver.quit();
    }
}

测试通过 

 

6.聊天编辑框的自动化测试 

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import static java.lang.Thread.sleep;

/**
 * 消息传输区域的自动化测试
 */
public class messageTransmissionAreaTest {
    //创建两个驱动
    static WebDriver driver1 = new ChromeDriver();
    static WebDriver driver2 = new ChromeDriver();
    @BeforeAll
    static void login() throws InterruptedException {
        //登录zhangsan这个账号
        driver1.get("http://127.0.0.1:8081/login.html");
        driver1.findElement(By.cssSelector("#username")).clear();
        driver1.findElement(By.cssSelector("#password")).clear();
        //输入正确的用户名和密码
        driver1.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver1.findElement(By.cssSelector("#password")).sendKeys("123");
        //点击登录按钮
        driver1.findElement(By.cssSelector("#submit")).click();
        sleep(6000);
        //获取弹窗
        Alert alert1 = driver1.switchTo().alert();
        alert1.accept();
        //登录lisi这个账号
        driver2.get("http://127.0.0.1:8081/login.html");
        driver2.findElement(By.cssSelector("#username")).clear();
        driver2.findElement(By.cssSelector("#password")).clear();
        //输入正确的用户名和密码
        driver2.findElement(By.cssSelector("#username")).sendKeys("lisi");
        driver2.findElement(By.cssSelector("#password")).sendKeys("123");
        //点击登录按钮
        driver2.findElement(By.cssSelector("#submit")).click();
        sleep(6000);
        //获取弹窗
        Alert alert2 = driver2.switchTo().alert();
        alert2.accept();
    }
    @Test
    public void transferMessages() throws InterruptedException {
        //zhangsan给lisi发消息:你最近过的还好吗?
        driver1.get("http://127.0.0.1:8081/client.html");
        driver1.findElement(By.cssSelector("#session-list > li:nth-child(3) > h3")).click();
        driver1.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys("你最近过的还好吗?");
        driver1.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();
        sleep(6000);

        //lisi查看zhangsan的消息,并回复:我最近蛮好的
        driver2.get("http://127.0.0.1:8081/client.html");
        driver2.findElement(By.cssSelector("#session-list > li > h3")).click();
        String expectMessage = "你最近过的还好吗?";
        //这个地方需要根据消息条数调整
        String actualMessage = driver2.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show > div:nth-child(23) > div > p")).getText();
        Assertions.assertEquals(expectMessage,actualMessage);
        sleep(6000);
        driver2.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys("我最近蛮好的");
        driver2.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();
        sleep(6000);

        //zhangsan查看lisi给自己发的消息
        driver1.get("http://127.0.0.1:8081/client.html");
        driver1.findElement(By.cssSelector("#session-list > li:nth-child(3) > h3")).click();
        String expectText = "我最近蛮好的";
        //这个地方需要根据消息条数调整
        String actualText = driver1.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show > div:nth-child(24) > div > p")).getText();
        Assertions.assertEquals(expectText,actualText);
    }

    @AfterAll
    static void close(){
        driver1.quit();
        driver2.quit();
    }
}

测试通过

总代码:

gitee:网页版聊天室自动化测试 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨染无尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值