1、熟悉项目
2、针对核心流程设计测试用例(手工测试用例)
3、将手工测试用例转化成自动化测试用例
4、部署
代码结构如何设计:
初始化动作:BeforeAll 创建驱动
退出动作:AfterAll 退出浏览器
- public static Stream
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); //智能等待
- //输入账号admin
- webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
- webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); //智能等待
- //输入密码123
- webDriver.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); //智能等待
- //跳转到列表页
- //获取当前页面url
- String 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*/
- @Test
- void 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();
- //获取当前页面url
- webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
- String cur_url = webDriver.getCurrentUrl();
- //获取当前页面的title
- webDriver.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("测试不通过");
- }
- }
- //发布博客
- @Test
- void 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);
- //获取当前页面url
- String cur_url = webDriver.getCurrentUrl();
- Assertions.assertEquals("http://localhost:8080/myblog_list.html",cur_url);
- }
- /*
- * 校验已发布博客的标题
- * 校验已发布博客时间*/
- @Test
- void 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("测试不通过");
- }
- }
- /*
- * 删除发布的博客*/
- @Test
- void 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,"自动化测试");
- }
问题:你为什么要对你这个项目做自动化测试?
我学了自动化测试,相对我所学的知识进行一个应用。
问题:你针对哪些点做了测试用例?为什么只针对这些模块做了测试用例?
对登录,博客数量,查看全文,写博客等做了测试。
我是针对我的项目的主流程和核心做了自动化测试。