• 博客自动化测试


    1、熟悉项目

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

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

    4、部署


    1、熟悉项目 

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

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

    代码结构如何设计:

    初始化动作:BeforeAll        创建驱动

    退出动作:AfterAll              退出浏览器

    登录

    1. public static Stream Generator() {
    2. return Stream.of(Arguments.arguments("http://localhost:8080/blog_content.html?id=1",
    3. "博客正文", "如何学习Java?"));
    4. }
    5. // 输入正确的账号,密码登录成功
    6. @ParameterizedTest
    7. // @ValueSource(strings = {"zhangsan","123","http://localhost:8080/login.html"})
    8. @CsvFileSource(resources = "LoginSuccess.csv")
    9. public void LoginSuccess(String username,String password,String blog_list_url) throws InterruptedException {
    10. //打开博客登录页面
    11. webDriver.get("http://localhost:8080/login.html");
    12. //sleep(3000);
    13. webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); //智能等待
    14. //输入账号admin
    15. webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
    16. webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); //智能等待
    17. //输入密码123
    18. webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
    19. webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); //智能等待
    20. //点击提交按钮
    21. webDriver.findElement(By.cssSelector("#submit")).click();
    22. webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); //智能等待
    23. //跳转到列表页
    24. //获取当前页面url
    25. String cur_url = webDriver.getCurrentUrl();
    26. //如果url=http://localhost:8080/login.html,测试通过,否则测试用例不通过
    27. Assertions.assertEquals(blog_list_url,cur_url);
    28. webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); //智能等待
    29. //列表页展示用户信息是admin
    30. //用户名是admin测试通过,否则测试不通过
    31. String cur_admin = webDriver.findElement(By.cssSelector("#username")).getText();
    32. Assertions.assertEquals(username,cur_admin);
    33. }

    博客数量

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

    查看全文

    1. /*
    2. * 博客详情页校验
    3. * url
    4. * 博客标题
    5. * 页面title是“博客详情页”*/
    6. @ParameterizedTest
    7. @MethodSource("Generator")
    8. void BlogDetail(String expected_url,String expected_title,String expected_blog_title) {
    9. //找到第一篇博客对应的查看全面按钮
    10. webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    11. webDriver.findElement(By.xpath("//*[@id=\"artDiv\"]/div[1]/a[1]")).click();
    12. //获取当前页面url
    13. webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    14. String cur_url = webDriver.getCurrentUrl();
    15. //获取当前页面的title
    16. webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    17. String cur_title = webDriver.getTitle();
    18. //获取博客标题
    19. String cur_blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
    20. webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    21. Assertions.assertEquals(expected_title,cur_title);
    22. Assertions.assertEquals(expected_blog_title,cur_blog_title);
    23. if (cur_url.contains(expected_blog_title)){
    24. System.out.println("测试通过");
    25. } else {
    26. System.out.println("测试不通过");
    27. }
    28. }

    写博客,发博客

    1. //发布博客
    2. @Test
    3. void EditBlog() throws InterruptedException {
    4. //找到写博客按钮,点击
    5. webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
    6. webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    7. //通过JS将标题直接输入
    8. ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"\n");
    9. webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    10. //点击发布
    11. webDriver.findElement(By.cssSelector("#submit")).click();
    12. webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    13. //获取当前页面url
    14. String cur_url = webDriver.getCurrentUrl();
    15. Assertions.assertEquals("http://localhost:8080/myblog_list.html",cur_url);
    16. }
    17. /*
    18. * 校验已发布博客的标题
    19. * 校验已发布博客时间*/
    20. @Test
    21. void BlogInfoChecked() {
    22. webDriver.get("http://localhost:8080/myblog_list.html");
    23. //获取第一篇博客的标题
    24. String first_blog_title = webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > div.title")).getText();
    25. //获取第一篇博客发布时间
    26. String first_blog_time = webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > div.date")).getText();
    27. //校验博客标题是不是自动化测试
    28. Assertions.assertEquals("自动化测试",first_blog_title);
    29. //如果时间是2023-10-21发布的,测试通过
    30. if(first_blog_time.contains("2023-10-21")) {
    31. System.out.println("测试通过");
    32. } else {
    33. System.out.println("当前时间是:"+first_blog_time);
    34. System.out.println("测试不通过");
    35. }
    36. }

    删除博客

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

    注销


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

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

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

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

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

  • 相关阅读:
    [swift刷题模板] 树状数组(BIT/FenwickTree)
    详解:取地址操作符(&)及解引用操作符(*)
    双十一游戏党必备的数码好物有哪些?2022双11游戏党必备外设清单
    多线程查询,效率翻倍
    基于工业路由器的物流车辆无线视频监控系统
    基础数学知识
    知识产权恶意侵权是什么意思
    maven本地仓库存在jar导包时依然试图远程仓库下载问题解决
    Spring IOC 常用注解与使用
    C++类设计:一个比较复杂的日志类 支持多线程、文件切换、信息缓存(源码)
  • 原文地址:https://blog.csdn.net/m0_67247641/article/details/133959452