东莞网站网络推广公司,怎么做商业网站模板,给公司做网站销售怎样啦,郑州网站建设讯息个人博客系统测试 一、项目背景1.1 技术背景1.2 功能背景 二、自动化测试2.1 什么是自动化测试2.2 通过使用selenium进行自动化测试的编写#xff08;Java实现#xff09;2.3 编写测试用例#xff0c;执行自动化测试2.3.1 输入用户名:test,密码:123#xff0c;登录成功2.3.… 个人博客系统测试 一、项目背景1.1 技术背景1.2 功能背景 二、自动化测试2.1 什么是自动化测试2.2 通过使用selenium进行自动化测试的编写Java实现2.3 编写测试用例执行自动化测试2.3.1 输入用户名:test,密码:123登录成功2.3.2 登录成功后没有文章点击写博客发布博客2.3.3 发布文章后文章列表页文章数不为02.3.4 校验博客2.4.5 在文章列表页发布者可以进行文章修改操作2.4.6 在文章列表页发布者可以进行文章删除操作2.4.7 注销账号退出登录2.4.8 输入用户名:testt,密码:123登录失败2.4.9 输入用户名:test,密码:1234登录失败 三、测试用例通过 一、项目背景
1.1 技术背景 我的博客系统主要是通过前端HTML后端SpringBoot实现了一个博客的基本的功能。前端主要用到了HTMLCSS通过Jquery的方式向后端请求数据。后端主要用到了SpringBoot的框架整合了MyBatis通过MyBatis从数据库中查询数据响应给前端。项目中用户在注册保存密码的时候后端会进行md5加盐算法进行加密处理保证了密码的安全性。相应数据的时候封装了一个统一的返回格式便于前后端的交互及数据的获取。用户登录后为了保持用户在线的持久化在用户登录成功时会将用户的身份信息存储在session中这样在每次访问一个页面的时候会对当前用户的身份信息进行校验每次访问时前端会传来一个seeionId后端通过这个sessionId拿到用户的信息然后校验通过后响应给前端数据否则提示用户登录。 1.2 功能背景 注册新用户进行注册后端会进行校验如果注册的用户已存在会提示用户已存在如果两次密码不一致会提示用户重新输入如果注册成功会跳转到登录页面。登录用户输入用户名和密码登录成功跳转到个人博客列表页显示当前用户已发布的博客信息。发布博客点击发布博客会跳转到博客添加页输入博客内容发布博客然后跳转到博客列表页展示刚刚发布的博客。博客详情页点击博客详情跳转到博客详情页展示了博客的标题、内容、发布时间、阅读量、作者信息作者用户名、作者发布文章数。总博客列表页显示所有用户发布的博客会以分页的形式展示我设置的默认是每页显示两条会有总页数当前所在页。修改博客在个人博客列表中可以点击修改某一篇文章进入博客修改页重新进行编辑然后发布修改。删除博客在个人博客列表中可以点击删除某一篇文章。游客登录如果用户未登录作为游客可以访问博客列表页主页可以查看所有用户发布的博客内容及博客详情但是不能发布文章。注销点击注销会退出当前账号。 二、自动化测试
2.1 什么是自动化测试 自动化测试简单来说就是使用自动测试工具和自动测试脚本来完成指定的测试任务测试启动过程不需要人为参与但自动化测试之前的准备需要人工手动配置好。它是一种将重复性的、繁琐的测试任务交给计算机自身来执行它可以大幅度提高测试效率、减少测试人员的成本、提高测试覆盖率和准确性。 2.2 通过使用selenium进行自动化测试的编写Java实现 添加依赖到pom.xml文件
!--selenium控制浏览器--
dependencygroupIdorg.seleniumhq.selenium/groupIdartifactIdselenium-java/artifactIdversion4.7.2/version
/dependency
dependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.11.0/version
/dependency
!--测试--
dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-params/artifactIdversion5.10.1/version
/dependency封装初始化浏览器驱动
/*** 初始化浏览器驱动*/
BeforeAll //在所有测试方法执行之前执行
static void setUp() {//允许所有请求chromeOptions.addArguments(--remote-allow-origins*);//取消 chrome正受到自动测试软件的控制的信息栏ChromeOptions options new ChromeOptions();options.setExperimentalOption(excludeSwitches, new String[]{enable-automation});//创建浏览器驱动webDriver new ChromeDriver(chromeOptions);
}关闭浏览器
/*** 关闭浏览器*/
AfterAll // 在所有测试方法执行完之后执行
static void tearDown() {webDriver.quit();
}2.3 编写测试用例执行自动化测试 前提说明 我的博客是在本地运行的如果要测试线上的某个系统可以把获取页面的url换成相应的网址即可。 2.3.1 输入用户名:test,密码:123登录成功
获取元素 测试代码
/*** 输入用户名:test,密码:123登录成功*/
Order(1)//设置执行顺序但我用着好像不管用
ParameterizedTest
CsvFileSource(resources loginSuccess.csv)
void loginTest(String username,String password,String blogListUrl) {// 1. 打开博客登录页面webDriver.get(http://localhost:58081/login.html);//智能等待如果在这次等待期间错误则会抛出异常webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 2. 输入用户名testwebDriver.findElement(By.cssSelector(#username)).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 3. 输入密码123webDriver.findElement(By.cssSelector(#password)).sendKeys(password);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 4. 点击提交按钮webDriver.findElement(By.cssSelector(#submit)).click();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//可能会有弹窗webDriver.switchTo().alert().accept();// 5. 跳转到列表页// 5.1 获取当前页urlString currentUrl webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 5.2 与预期urlhttp://localhost:58081/myblog_list.html对比一致则测试通过Assertions.assertEquals(blogListUrl, currentUrl);// 6. 列表页展示用户名是test// 6.1 用户名是test测试通过否则不通过webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);String currentUerName webDriver.findElement(By.cssSelector(#username)).getText();Assertions.assertEquals(currentUerName,username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}2.3.2 登录成功后没有文章点击写博客发布博客 点击写文章 校验url 测试代码
Test
void publishTest() {// 1.打开文章列表页webDriver.get(http://localhost:58081/myblog_list.html);// 2. 没有发布博客点击添加webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);webDriver.findElement(By.cssSelector(body div.nav a:nth-child(5))).click();// 3. 添加标题 test1webDriver.findElement(By.cssSelector(#title)).sendKeys(test1);// 5. 点击发布文章webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);webDriver.findElement(By.cssSelector(body div.blog-edit-container div.title button)).click();// 6. 跳转到博客列表页 “http://localhost:58081/myblog_list.html”webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);String currentUrl webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);Assertions.assertEquals(http://localhost:58081/myblog_list.html,currentUrl);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 7. 判断若第一条博客标题为test1则测试通过String newTitle webDriver.findElement(By.cssSelector(#artListDiv div div.title)).getText();Assertions.assertEquals(test1,newTitle);
}2.3.3 发布文章后文章列表页文章数不为0
获取元素 测试代码
Test
void blogCountTest() {//判断当前页文章数量int size webDriver.findElements(By.cssSelector(#artListDiv)).size();// 不为0则测试通过webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);Assertions.assertNotEquals(0,size);
}2.3.4 校验博客
获取元素 测试代码 Testvoid checkBlogTest() {// 1.打开文章列表页webDriver.get(http://localhost:58081/myblog_list.html);//博客标题String title webDriver.findElement(By.cssSelector(#artListDiv div:nth-child(1) div.title)).getText();//博客发布时间String time webDriver.findElement(By.cssSelector(#artListDiv div:nth-child(1) div.date)).getText();Assertions.assertEquals(test1,title);//如果是2024年发布的则测试通过if (time.contains(2024)) {System.out.println(测试通过);} elseSystem.out.println(测试未通过);}2.4.5 在文章列表页发布者可以进行文章修改操作 测试代码
Test
void updateBlogTest() throws InterruptedException {// 1.打开文章列表页webDriver.get(http://localhost:58081/myblog_list.html);// 2.判断当前登录用户是否是test// 根据列表页个人信息进行判断webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String currentUerName webDriver.findElement(By.cssSelector(#username)).getText();Assertions.assertEquals(test,currentUerName);webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);// 3.执行到这里说明当前登录用户是test可以进行修改操作// 点击修改开始修改文章webDriver.findElement(By.cssSelector(#artListDiv div:nth-child(1) a:nth-child(5))).click();// 4.跳转到修改文章页面 http://localhost:58081/blog_edit.html?blogId// 获取修改页面urlwebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String editPageUrl webDriver.getCurrentUrl();if (editPageUrl.contains(http://localhost:58081/blog_edit.html?blogId)) {System.out.println(测试通过);} else {System.out.println(测试不通过);}webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);// 5. 修改文章内容// 通过JS将标题进行修改((JavascriptExecutor)webDriver).executeScript(document.getElementById(\title\).value \自动化测试\);webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);// 6. 点击修改文章webDriver.findElement(By.cssSelector(body div.blog-edit-container div.title button)).click();// 7. 跳转到博客列表页 “http://localhost:58081/myblog_list.html”// 会有弹窗// 这里强制睡3s要不然这个弹窗会检测不出来sleep(3000);webDriver.switchTo().alert().accept();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);String currentUrl webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);Assertions.assertEquals(http://localhost:58081/myblog_list.html,currentUrl);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 8. 判断若第一条博客标题为自动化测试则测试通过String newTitle webDriver.findElement(By.cssSelector(#artListDiv div div.title)).getText();Assertions.assertEquals(自动化测试,newTitle);
}2.4.6 在文章列表页发布者可以进行文章删除操作
操作步骤 Test
void deleteBlogTest() throws InterruptedException {// 1.打开文章列表页webDriver.get(http://localhost:58081/myblog_list.html);// 2.判断当前登录用户是否是test// 根据列表页个人信息进行判断webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String currentUerName webDriver.findElement(By.cssSelector(#username)).getText();Assertions.assertEquals(test,currentUerName);webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);// 3.执行到这里说明当前登录用户是test可以进行删除操作// 点击修改开始删除文章webDriver.findElement(By.cssSelector(#artListDiv div:nth-child(1) a:nth-child(6))).click();webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);//可能有弹窗sleep(3000);webDriver.switchTo().alert().accept();// 4. 校验页面 http://localhost:58081/myblog_list.htmlwebDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);String currentUrl webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);Assertions.assertEquals(http://localhost:58081/myblog_list.html,currentUrl);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 5. 判断若第一条博客标题不是自动化测试则测试通过String newTitle webDriver.findElement(By.cssSelector(#artListDiv div div.title)).getText();Assertions.assertNotEquals(自动化测试,newTitle);
}2.4.7 注销账号退出登录
操作步骤 测试代码
Test
void logOffTest() throws InterruptedException {// 1. 点击注销按钮退出博客webDriver.findElement(By.cssSelector(body div.nav a:nth-child(6))).click();//可能有弹窗直接通过跳转到登录页面sleep(3000);webDriver.switchTo().alert().accept();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 2. 判断当前页面是否是登录页面String currentUrl webDriver.getCurrentUrl();Assertions.assertEquals(http://localhost:58081/login.html,currentUrl);
}2.4.8 输入用户名:testt,密码:123登录失败
操作步骤 测试代码
ParameterizedTest
CsvFileSource(resources loginFail1.csv)
void loginFail1(String username,String password,String blogListUrl) {// 1. 打开博客登录页面webDriver.get(http://localhost:58081/login.html);//智能等待webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 2. 输入用户名testtwebDriver.findElement(By.cssSelector(#username)).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 3. 输入密码123webDriver.findElement(By.cssSelector(#password)).sendKeys(password);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 4. 点击提交按钮webDriver.findElement(By.cssSelector(#submit)).click();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//可能会有弹窗webDriver.switchTo().alert().accept();// 5. 不进行跳转// 5.1 获取当前页urlString currentUrl webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 5.2 与预期urlhttp://localhost:58081/login.html对比一致则测试通过Assertions.assertEquals(blogListUrl, currentUrl);
}2.4.9 输入用户名:test,密码:1234登录失败
操作步骤 测试代码
ParameterizedTest
CsvFileSource(resources loginFail2.csv)
void loginFail2(String username,String password,String blogListUrl) {// 1. 打开博客登录页面webDriver.get(http://localhost:58081/login.html);//智能等待webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 2. 输入用户名testwebDriver.findElement(By.cssSelector(#username)).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 3. 输入密码1234webDriver.findElement(By.cssSelector(#password)).sendKeys(password);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 4. 点击提交按钮webDriver.findElement(By.cssSelector(#submit)).click();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//可能会有弹窗webDriver.switchTo().alert().accept();// 5. 不进行跳转// 5.1 获取当前页urlString currentUrl webDriver.getCurrentUrl();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);// 5.2 与预期urlhttp://localhost:58081/login.html对比一致则测试通过Assertions.assertEquals(blogListUrl, currentUrl);
}三、测试用例通过