对博客系统基本功能进行自动化测试(Junit + Selenium)

环境搭建:

  1. 浏览器:
    1. 本次测试使用Chrome浏览器
    2. 在jdk的bin目录下安装对应浏览器驱动(尽量选择与浏览器版本相近的驱动)chromedriver.storage.googleapis.com/index.html
  2. Junit依赖:
             <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
            <dependency>
                <groupId>org.junit.jupiter</groupId><!--编写用例的基本注解-->
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.9.1</version>
            </dependency>
    
            <dependency><!--参数化测试依赖-->
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-params</artifactId>
                <version>5.9.1</version>
            </dependency>
    
            <dependency><!--这个库提供JUnit平台的核心功能,比如共享的测试接口、注解和工具类-->
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-commons</artifactId>
                <version>1.9.1</version> <!-- 请根据需要调整为与JUnit 5.9.1兼容的版本 -->
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-suite</artifactId>
                <version>1.9.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-suite-api</artifactId>
                <version>1.9.1</version>
            </dependency>
    
    
            <!--JUnit Jupiter的测试引擎,实现了JUnit Platform的TestEngine接口。
            它负责发现和执行使用JUnit Jupiter API编写的测试。-->
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.9.1</version>
                <scope>test</scope>
            </dependency>
  3. Selenium依赖
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>3.141.59</version>
        </dependency>

测试用例:

网站:登陆页面

项目结构:

InitAndQuit进行测试初始化和收尾工作

TestItem包含对各个页面基本功能的自动化测试用例

初始化以及资源关闭:

/**
 * @ClassName InitAndQuit
 * @Description 初识化测试相关以及测试结束资源关闭
 * @Author 86153
 * @Date 2024/4/30 10:20
 * @Version 1.0
 **/
public class InitAndQuit {
    static WebDriver webDriver;
    @BeforeAll
    static void init() {
        webDriver = new ChromeDriver();
    }
    @AfterAll
    static void quit() {
        webDriver.quit();
    }
}

登录页面测试用例:

  • 输入给定邮箱点击获取验证码:
//验证码
    private static String emailCode;

    //在注册页面点击获取验证码按钮
    @ParameterizedTest
    @CsvFileSource(resources = "login.csv")
    @Order(0)
    void loginTest(String account) throws InterruptedException {
        webDriver.get("http://8.130.70.131:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#username")).sendKeys(account);
        WebDriverWait wait = new WebDriverWait(webDriver,10);
        //这里 获取验证码 和 提交按钮为俩个一样的button,所以需要进行按钮的选择
        WebElement clickElement = wait.until(ExpectedConditions
                .elementToBeClickable(By.cssSelector("#submit")));
        List<WebElement> elements = webDriver.findElements(By.cssSelector("#submit"));
        elements.get(0).click();
    }
  • 从邮箱中拿到验证码
 /*邮箱登录拿到验证码*/
    @Test
    @Order(1)
    void getCaptcha() throws InterruptedException {
        webDriver.get("https://www.baidu.com/");
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        webDriver.findElement(By.cssSelector(".s_ipt")).sendKeys("https://mail.qq.com/");
        webDriver.findElement(By.cssSelector("#su")).click();

        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //保存当前窗口的句柄
        String originalWindow = webDriver.getWindowHandle();
        webDriver.findElement(By.cssSelector("#\\31  > div > div:nth-child(1) > h3 > a")).click();
        //切换窗口
        Set<String> set = webDriver.getWindowHandles();
        for(String cur : set) {
            if(!cur.equals(originalWindow)) {
                webDriver.switchTo().window(cur);
            }
        }
        //登录邮箱
        //注意这里切换frame时要先去选择获取到frame
        WebElement iframe = webDriver.findElement(By.cssSelector("#QQMailSdkTool_login_loginBox_qq > iframe"));
        webDriver.switchTo().frame(iframe);
        WebElement iframe1 = webDriver.findElement(By.cssSelector("#ptlogin_iframe"));
        webDriver.switchTo().frame(iframe1);
        //点击用户头像进行登录(电脑登陆了QQ)
        webDriver.findElement(By.cssSelector("#img_out_3224881242")).click();
        sleep(10000);
        //进入对应邮件
        webDriver.findElement(By.cssSelector("#mailMainApp > div.frame_main.mail_app > div > div > div > div.mailList_listWrapper > div.mailList_group > div:nth-child(1) > div.mailList_group_item_cnt > table > tbody > tr:nth-child(1)")).click();
        //拿到验证码
        WebElement element = webDriver.findElement(By.cssSelector("#readmail_content_html > span"));
        String text = element.getText();
        emailCode = text;
    }
  • 登录
 //登陆页面测试
    @ParameterizedTest
    @CsvFileSource(resources = "login.csv")
    @Order(2)
    void loginTest(String account,String password) throws InterruptedException {
        //进入登陆页面
        webDriver.get("http://8.130.70.131:8080/login.html");
        //隐式等待
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //输入账号密码验证码
        webDriver.findElement(By.cssSelector("#username")).sendKeys(account);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#captcha")).sendKeys(emailCode);
        //显示等待
        WebDriverWait wait = new WebDriverWait(webDriver,10);
        //这里 获取验证码 和 提交按钮为俩个一样的button,所以需要进行按钮的选择
        WebElement clickElement = wait.until(ExpectedConditions
                .elementToBeClickable(By.cssSelector("#submit")));
        List<WebElement> elements = webDriver.findElements(By.cssSelector("#submit"));
        elements.get(1).click();
        //显示等待,等待弹窗出现
        //注意这里是个坑,弹窗不属于页面内的元素,不能使用隐式等待

        wait.until(ExpectedConditions.alertIsPresent());
        //弹窗选择
        webDriver.switchTo().alert().accept();
        //等待进入新页面
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //校验url
        String currentURL = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://8.130.70.131:8080/myblog_list.html",currentURL);
    }

列表页测试用例:

 //列表页自动化测试
    /**
     * 如果列表页博客数量不为0表示测试通过
     **/
    @Test
    @Order(3)
    void listTest() {
        webDriver.get("hhttp://8.130.70.131:8080/blog_list.html");
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        int size = webDriver.findElements(By.cssSelector(".title")).size();
        System.out.println(size);
        Assertions.assertNotEquals(0,size);
    }

个人列表页测试用例:

//个人列表页测试
    @Test
    @Order(4)
    void selfListTest() {
        webDriver.get("http://8.130.70.131:8080/myblog_list.html");
        //文章数量不为0测试
        List<WebElement> list = webDriver.findElements(By.cssSelector(".title"));
        int size = webDriver.findElements(By.cssSelector(".title")).size();
        System.out.println(size);
        Assertions.assertNotEquals(0,list.size());
        //测试“主页”按钮
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        String curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://8.130.70.131:8080/blog_list.html",curUrl);
        //回退到个人主页
        webDriver.navigate().back();
        //测试“写博客按钮”
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://8.130.70.131:8080/blog_add.html",curUrl);
        webDriver.navigate().back();
       /* //测试“注销”按钮
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();

        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://8.130.70.131:8080/login.html",curUrl);*/
    }

博客详情页测试用例:

    //博客详情页测试
    @ParameterizedTest
    @Order(5)
    @CsvFileSource(resources = "detail.csv")
    void detailTest(String destUrl,String destPageTitle,String destBlogTitle) {
        //进入列表页
        webDriver.get("http://8.130.70.131:8080/blog_list.html");
        //点击文章详情按钮,进入博客详情页
        webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a")).click();
        //校验页面url,页面title,博客题目
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        String url = webDriver.getCurrentUrl();
        String pageTitle = webDriver.getTitle();
        String blogTitle = webDriver.findElement(By.cssSelector("#title")).getText();
        Assertions.assertEquals(destUrl,url);
        Assertions.assertEquals(destPageTitle,pageTitle);
        Assertions.assertEquals(destBlogTitle,blogTitle);
    }

编辑页测试用例:

//博客编辑页测试
    @ParameterizedTest
    @Order(6)
    @CsvFileSource(resources = "edit.csv")
    void editTest(String data,String title) {
        webDriver.get("http://8.130.70.131:8080/blog_add.html");
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
       /* //直接使用通过js设置文章标题
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");*/
        webDriver.findElement(By.cssSelector("#title")).sendKeys("自动化测试");
        //发布文章
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        //这里有一个”是否继续添加博客“的弹窗,进行选择后才跳转
        WebDriverWait wait = new WebDriverWait(webDriver,5);
        wait.until(ExpectedConditions.alertIsPresent());
        webDriver.switchTo().alert().dismiss();
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        //校验url跳转
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://8.130.70.131:8080/myblog_list.html",cur_url);
        //校验第一篇博客的发布时间,标题
        String publishDate = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.date")).getText();
        String publishTitle = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title")).getText();
        Assertions.assertEquals(title,publishTitle);
        if(publishDate.contains(data)) {
            System.out.println("测试通过");
        }else {
            System.out.println("发布时间错误");
        }
        //删除博客
        webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(6)")).click();
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        //弹窗选择
        wait.until(ExpectedConditions.alertIsPresent());
        webDriver.switchTo().alert().accept();
        //校验第一篇博客是否被删除
        WebElement after_delete_title = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title"));
        Assertions.assertNotEquals(title,after_delete_title);
    }

注销:

   //注销
    @Test
    @Order(7)
    void login_out() {
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        //点击注销按钮
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        //显示等待alter弹窗出现
        WebDriverWait wait = new WebDriverWait(webDriver,5);
        wait.until(ExpectedConditions.alertIsPresent());
        //选择弹框并进行确定
        webDriver.switchTo().alert().accept();
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://8.130.70.131:8080/login.html",cur_url);
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/622640.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【linux-IMX6ULL-RTC-IIC-SPI配置思路】

目录 1. RTC简介1.1 IMX6ULL中的RTC1.2 SNVS_LP中的SRTC配置流程1.3 程序实现 2. IIC通信协议2.1 IIC基础2.2 IIC通信协议2.2.1 IIC写时序2.2.2 IIC读时序 3. IIC通信的硬件框图及配置流程3.1 IMX6ULL的硬件IIC框图3.1 IIC配置流程3.2 硬件IIC代码实现 4. SPI通信4.1 SPI通信基…

【Python探索之旅】选择结构(条件语句)

文章目录 条件结构&#xff1a; 1.1 if单分支结构 1.2 if-else 多分支结构 1.3 if-elif 多重结构&#xff1a; 完结撒花​ 前言 Python条件语句是通过一条或多条语句的执行结果&#xff08;True或者False&#xff09;来决定执行的代码块。 Python提供了顺序、选择、循环三…

IP SSL怎么签发使用

IP证书的签发首先是需要有一个可供绑定的IP地址&#xff0c;作为常用数字证书之一&#xff0c;IP证书也因为其广泛的应用范围而深得用户的青睐和喜欢。 部署IP证书后&#xff0c;可以实现该IP地址的https访问&#xff0c;过程和域名证书相差不多。 IP证书和域名证书的区别 很…

针对关键 PuTTY 私钥恢复漏洞的 PoC 发布

安全研究人员针对广泛使用的 PuTTY SSH 和 Telnet 客户端中的一个关键漏洞发布了概念验证 (PoC) 漏洞利用。 该漏洞CVE-2024-31497允许攻击者恢复 PuTTY 版本 0.68 至 0.80 中使用 NIST P-521 椭圆曲线生成的私钥。 该漏洞源于 PuTTY在使用 P-521 曲线时偏向生成ECDSA随机数。…

算法课程笔记——自下而上树形DP

算法课程笔记——自下而上树形DP #include<bits/stdc.h>usingnamespacestd; constintN100005; intn,a[N]; longlongdp[N][2]; vector<int> e[N]; voiddfs(intu){for(autov:e[u]){dfs(v);dp[u][1]dp[v][0];dp[u][0]max(dp[v][0],dp[v][1]);}dp[u][1]a[u]; } intmain…

Vue中CSS动态样式绑定与注意事项

vue中css使用动态变量_vue css变量 动态-CSDN博客 需求&#xff1a; vue使用el-select&#xff0c;下拉选择值时‘输入框’的背景图片就改变为对应所选项的背景图 分析 &#xff1a; 每次下拉选择&#xff0c;值发生变化&#xff0c;背景图与值一一对应绑定&#xff0c;为动态…

[数据结构1.0]选择排序

鼠鼠前面的博客介绍过选择排序是常见的排序算法&#xff0c;选择排序有但不限于直接选择排序和堆排序&#xff01;那么鼠鼠今天浅谈一下选择排序&#xff01; 鼠鼠本博客用排升序来介绍选择排序&#xff01; 目录 1.直接选择排序 1.1.直接选择排序 1.2.直接选择排序特性 2…

FreeRTOS互斥量

目录 一、互斥量的概念 二、优先级翻转和优先级继承 1、优先级翻转 2、优先级继承 三、互斥量相关API 四、优先级实操 1、优先级翻转演示 1.1 CubeMX配置 1.2 代码实现 2、使用互斥量优化优先级翻转问题 2.1 CubeMX配置 2.2 代码实现 一、互斥量的概念 在多数情况…

数据可视化训练第6天(美国人口调查获得关于收入与教育背景的数据,并且可视化)

数据来源 https://archive.ics.uci.edu/dataset/2/adult 过程 首先&#xff1b;关于教育背景的部分翻译有问题。 本次使用字典嵌套记录数据&#xff0c;并且通过lambda在sorted内部进行对某个字典的排序&#xff0c;最后用plotly进行绘图 本次提取数据的时候&#xff0c;用到…

Cocos Creator 3.8.x 透明带滚动功能的容器

ScrollView 是一种带滚动功能的容器 1、删除ScrollView下Sprite组件的SpriteFrame 2、ScrollView下scrollBar的Sprite组件的Color设为&#xff1a;FFFFFF00 3、ScrollView下view的Graphics组件的FillColor设为&#xff1a;FFFFFF00

华火5.0台嵌式喷火电燃单灶,更懂未来生活需求

在厨电技术不断革新的今天&#xff0c;第五代华火电燃灶以其独特的技术升级和卓越性能&#xff0c;成功吸引了市场的广泛关注。作为华火品牌的最新力作&#xff0c;第五代电燃灶不仅继承了前代产品的优点&#xff0c;更在多个方面进行了显著的升级和创新。下面&#xff0c;我们…

Unity自定义动画-Animation动画数据-How is “fileIDToRecycleName“ generated

一般美术和程序分工明确的项目 fbx确实是和动画一一对应的&#xff1b; 但一些独立&#xff0c;或者小工作室的项目&#xff0c;就没法保证了&#xff0c;关键还是在于 Unity的 .meta 目录 查找和对比了一下 .fbx 和 .meta&#xff1a; 缓存和不缓存Animation 具体的Animat…

【Python】使用PyTorch训练一个手写数字识别模型(MNIST)

文章目录 1. 准备工作2. 训练网络3. 测试网络4. 训练和测试循环5. 模型保存6. 最终完整代码7. 结果截图使用PyTorch训练一个手写数字识别模型(MNIST) 在这篇博客中,使用了PyTorch构建一个简单的神经网络来识别手写数字。将使用MNIST数据集,这是一个经典的机器学习基准数据集…

电子学会C/C++编程等级考试2024年03月(一级)真题解析

C/C++编程(1~8级)全部真题・点这里 第1题:倒序输出 依次输入4个整数a、b、c、d,将他们倒序输出,即依次输出d、c、b、a这4个数。 时间限制:1000 内存限制:65536 输入 一行4个整数a、b、c、d,以空格分隔。 0 < a,b,c,d < 108 输出 一行4个整数d、c、b、a,整数之间以…

白话机器学习5:卷积神经网络(CNN)原理

1.神经元 激活函数f(z)的种类&#xff1a; 2.卷积方法种类 https://mp.weixin.qq.com/s/FXzTbMG64jr93Re31Db2EA 标准卷积&#xff08;Standard Convolution&#xff09;: 特点&#xff1a;每个卷积核在输入数据的整个深度上滑动&#xff0c;计算输出特征图的一个元素。应用场…

SQL_hive的连续开窗函数

SQL三种排序&#xff08;开窗&#xff09;第几名/前几名/topN 1三种排序&#xff08;开窗&#xff09;第几名/前几名/topN思路 4种排序开窗函数 1三种排序&#xff08;开窗&#xff09;第几名/前几名/topN 求每个学生成绩第二高的科目-排序思路 t2表&#xff1a;对每个学生 的…

python数据分析——seaborn绘图1

参考资料&#xff1a;活用pandas库 matplotlib库是python的和兴绘图工具&#xff0c;而seaborn基于matplotlib创建&#xff0c;它为绘制统计图提供了更高级的接口&#xff0c;使得只用少量代码就能生成更美观、更复杂的可视化效果。 seaborn库和pandas以及其他pydata库&#xf…

防火请技术基础篇:令牌桶机制的剖析与应用

防火墙中的令牌桶机制&#xff1a;深度剖析与应用 在现代网络通信中&#xff0c;防火墙技术发挥着至关重要的作用&#xff0c;它不仅能够实现网络安全防御&#xff0c;还能通过诸如令牌桶算法等机制来有效管理网络流量&#xff0c;保证网络服务的质量。本文将全面深入地探讨防…

Vue从入门到实战Day05

一、自定义指令 自定义指令&#xff1a;自己定义的指令&#xff0c;可以封装一些dom操作&#xff0c;扩展额外功能 需求&#xff1a;当页面加载时&#xff0c;让元素将获得焦点 (autofocus在safari浏览器有兼容性) 操作dom&#xff1a;dom元素.focus() mounted() {this.$ref…

深度剖析深度神经网络(DNN):原理、实现与应用

目录 引言 一、DNN基本原理 二、DNN核心算法原理 三、DNN具体操作步骤 四、代码演示 引言 在人工智能和机器学习的浪潮中&#xff0c;深度神经网络&#xff08;Deep Neural Network&#xff0c;简称DNN&#xff09;已经成为了一种非常重要的工具。DNN模仿人脑神经网络的结…