博客自动化测试

编程入门 行业动态 更新时间:2024-10-19 10:26:07

博客自动化<a href=https://www.elefans.com/category/jswz/34/1771117.html style=测试"/>

博客自动化测试

1、熟悉项目

2、针对核心流程设计测试用例(手工测试用例)

3、将手工测试用例转化成自动化测试用例

4、部署


1、熟悉项目 

2、针对核心流程设计测试用例(手工测试用例)

3、将手工测试用例转化成自动化测试用例

代码结构如何设计:

初始化动作:BeforeAll        创建驱动

退出动作:AfterAll              退出浏览器

登录

public static Stream<Arguments> Generator() {return Stream.of(Arguments.arguments("http://localhost:8080/blog_content.html?id=1","博客正文", "如何学习Java?"));}//    输入正确的账号,密码登录成功@ParameterizedTest// @ValueSource(strings = {"zhangsan","123","http://localhost:8080/login.html"})@CsvFileSource(resources = "LoginSuccess.csv")public void LoginSuccess(String username,String password,String blog_list_url) throws InterruptedException {//打开博客登录页面webDriver.get("http://localhost:8080/login.html");//sleep(3000);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);  //智能等待//输入账号adminwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);  //智能等待//输入密码123webDriver.findElement(By.cssSelector("#password")).sendKeys(password);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);  //智能等待//点击提交按钮webDriver.findElement(By.cssSelector("#submit")).click();webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);  //智能等待//跳转到列表页//获取当前页面urlString cur_url = webDriver.getCurrentUrl();//如果url=http://localhost:8080/login.html,测试通过,否则测试用例不通过Assertions.assertEquals(blog_list_url,cur_url);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);  //智能等待//列表页展示用户信息是admin//用户名是admin测试通过,否则测试不通过String cur_admin = webDriver.findElement(By.cssSelector("#username")).getText();Assertions.assertEquals(username,cur_admin);}

博客数量

/** 博客列表页博客数量不为0*/@Testvoid BlogList() {//打开博客列表页webDriver.get("http://localhost:8080/myblog_list.html");//获取页面上所有博客标题对应的元素webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);int title_num = webDriver.findElements(By.cssSelector(".title")).size();//如果元素数据不为0,测试通过Assertions.assertNotEquals(0,title_num);}

查看全文

/** 博客详情页校验* url* 博客标题* 页面title是“博客详情页”*/@ParameterizedTest@MethodSource("Generator")void BlogDetail(String expected_url,String expected_title,String expected_blog_title) {//找到第一篇博客对应的查看全面按钮webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);webDriver.findElement(By.xpath("//*[@id=\"artDiv\"]/div[1]/a[1]")).click();//获取当前页面urlwebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_url = webDriver.getCurrentUrl();//获取当前页面的titlewebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_title = webDriver.getTitle();//获取博客标题String cur_blog_title = webDriver.findElement(By.cssSelector("#title")).getText();webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);Assertions.assertEquals(expected_title,cur_title);Assertions.assertEquals(expected_blog_title,cur_blog_title);if (cur_url.contains(expected_blog_title)){System.out.println("测试通过");} else {System.out.println("测试不通过");}}

写博客,发博客

//发布博客@Testvoid EditBlog() throws InterruptedException {//找到写博客按钮,点击webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);//通过JS将标题直接输入((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"\n");webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);//点击发布webDriver.findElement(By.cssSelector("#submit")).click();webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);//获取当前页面urlString cur_url = webDriver.getCurrentUrl();Assertions.assertEquals("http://localhost:8080/myblog_list.html",cur_url);}/** 校验已发布博客的标题* 校验已发布博客时间*/@Testvoid BlogInfoChecked() {webDriver.get("http://localhost:8080/myblog_list.html");//获取第一篇博客的标题String first_blog_title = webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > div.title")).getText();//获取第一篇博客发布时间String first_blog_time = webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > div.date")).getText();//校验博客标题是不是自动化测试Assertions.assertEquals("自动化测试",first_blog_title);//如果时间是2023-10-21发布的,测试通过if(first_blog_time.contains("2023-10-21")) {System.out.println("测试通过");} else {System.out.println("当前时间是:"+first_blog_time);System.out.println("测试不通过");}}

删除博客

/** 删除发布的博客*/@Testvoid DeleteBlog() {//打开博客列表页面webDriver.get("http://localhost:8080/myblog_list.html");
//        //点击查看全文按钮
//        webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > a:nth-child(4)")).click();//点击删除按键webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > a:nth-child(6)")).click();//博客列表页第一篇标题不是“自动化测试”webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String first_blog_title = webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > div.title")).getText();//校验当前博客标题不等于“自动化测试”Assertions.assertNotEquals(first_blog_title,"自动化测试");}

注销


问题:你为什么要对你这个项目做自动化测试?

我学了自动化测试,相对我所学的知识进行一个应用。

问题:你针对哪些点做了测试用例?为什么只针对这些模块做了测试用例?

对登录,博客数量,查看全文,写博客等做了测试。

我是针对我的项目的主流程和核心做了自动化测试。 

更多推荐

博客自动化测试

本文发布于:2023-12-05 21:46:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1665400.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   博客

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!