基于博客系统的测试

目录

1.测试用例

2.编写代码

2.1InitAndEnd

2.2BlogCases 编写测试用例

2.2.1.登录

2.2.2博客列表页

2.2.3写博客

          2.2.4博客详情页校验

2.2.5写博客后,校验博客列表页

2.2.6删除刚才测试发布的博客

2.2.7注销


1.测试用例

2.编写代码

创建两个类

2.1InitAndEnd

用于打开和关闭浏览器

方法:在所有用例最开始和最后执行所以用@BeforeAll @AfterAll

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author KYF
 * @create 2023-05-27
 */
public class InitAndEnd {
    static WebDriver webDriver;
    @BeforeAll
    static void SetUp(){
        webDriver=new ChromeDriver();
    }
    @AfterAll
    static void TearDown(){
        webDriver.quit();
    }
    
}

2.2BlogCases 编写测试用例

//规定执行测试用例的顺序
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)

2.2.1.登录

要求:输入正确的username和password,点击提交按钮,登录成功

步骤:

  1. 打开博客登录页面 get()
  2. 输入账号和密码 semdKeys()
  3. 点击跳转按钮 click()
  4. 自动跳转到列表页后,校验
    1. 获取当前页面的url-->判断是否正确 getCurrentUrl()
    2. 获取列表页显示用户账号-->判断是否正确 getText()
@Order(1)
@ParameterizedTest
@CsvFileSource(resources ="LoginSuccess.csv" )
void LoginSuccess(String username,String password,String list_url) {
    //打开博客登录页面
    webDriver.get("http://192.168.182.128:8080/blog_system/blog_login.html");
    webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    //输入账号--zhang
    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://192.168.182.128:8080/blog_system/blog_list.html,测试通过
    Assertions.assertEquals(list_url,cur_url);
    webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    //列表页展示用户信息是zhang
    //用户名是张,测试通过
    String cur_name=webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
    Assertions.assertEquals(username,cur_name);
}

2.2.2博客列表页

要求:博客数量不为0

步骤:

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

2.2.3写博客

要求:输入自动化测试,点击发布按钮,跳转到博客列表页

步骤:

  1. 在导航栏中找到写博客按钮,点击-->进入写博客
  2. 通过js把标题输入
  3. 点击发布
  4. 获取当前页面的url,判断是否是博客列表页 getCurrentUrl()
/**
 * 写博客
 */
@Order(3)
@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=\"自动化测试\"");
    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://192.168.182.128:8080/blog_system/blog_list.html",cur_url);


}

2.2.4博客详情页校验

要求:验证url,title,博客标题是否正确

步骤:

  1. 找到第一篇博客查看全文的按钮-->点击,跳转到博客详情页
  2. 获取当前页面的url,title,博客标题 getCurrenrUrl() getTitle() By.cssSelector
  3. 校验
/**
 * 博客详情页校验
 * url
 * 博客标题
 * 页面title是"博客详情页"
 */
@Order(4)
@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("/html/body/div[2]/div[2]/div[1]/a")).click();
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    //获取当前页面的url
    String cur_url= webDriver.getCurrentUrl();
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    //获取当前页面的title
    String cur_title= webDriver.getTitle();
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    //获取博客标题
    String blog_title=webDriver.findElement(By.cssSelector("body > div.container > div.right > div > h3")).getText();
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    //校验
    Assertions.assertEquals(expected_title,cur_title);
    Assertions.assertEquals(expected_blog_title,blog_title);
    if(cur_url.contains(expected_url)){
        System.out.println("测试通过");
    }else{
        System.out.println("测试不通过");
    }
}

2.2.5写博客后,校验博客列表页

要求:博客列表页,检验第一个博客标题和时间是否正确

步骤:

        1.获取第一个博客的标题,检验是否是"自动化测试"

        2.获取第一篇的发布时间

/**
 *校验已发布博客标题
 * 检验已发布博客时间2.2
 */
@Order(5)
@Test
void BlogInfoChecked(){
    webDriver.get("http://192.168.182.128:8080/blog_system/blog_list.html");
    //获取第一个博客标题
    String first_blog_title=webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
    //获取第一篇发布时间
    String first_blog_time=webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();
    //校验博客标题是不是自动化测试
    Assertions.assertEquals("自动化测试",first_blog_title);
    //如果时间是2023-5-26发布,测试通过
    if(first_blog_title.contains("2023-05-26")){

        System.out.println("测试通过");
    }else{
        System.out.println("当前时间是:"+first_blog_time);
        System.out.println("测试不通过");
    }
}

2.2.6删除刚才测试发布的博客

要求:点击删除,检测是否已经删除(列表页第一篇博客不是测试时刚发布的博客)

步骤:

  1. 打开博客列表页
  2. 点击查看全文按钮
  3. 点击删除
  4. 校验第一篇博客标题是不是自动化测试
  5. 如果不是,通过
/**
 * 删除刚才发布的博客
 */
@Order(6)
 @Test
void DeleteBlog() throws InterruptedException {
    //打开博客列表页
    webDriver.get("http://192.168.182.128:8080/blog_system/blog_list.html");
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    //点击查看全文按钮
    webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    //点击删除
    webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).click();//点击删除后,会自动跳转到博客列表页
    sleep(3000);
    //校验博客列表第一篇博客标题不是"自动化测试"
    String first_blog_title=webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
    webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    //校验当前博客标题不等于"自动化测试"
    Assertions.assertNotEquals("自动化测试",first_blog_title);

}

2.2.7注销

要求:点击注销,检测是否跳转到登录页面

步骤:

  1. 找到注销按钮,点击
  2. 跳转到登录页后,检测url和提交按钮
/**
 * 注销
 */
@Order(7)
@Test
void Logout() throws InterruptedException {
    //复制注销按钮
    webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();

   sleep(3000);
   //校验url
    String cur_url= webDriver.getCurrentUrl();
    Assertions.assertEquals("http://192.168.182.128:8080/blog_system/blog_login.html",cur_url);
    sleep(3000);
    //校验提交按钮
    WebElement webElement =webDriver.findElement(By.cssSelector("#submit"));
    Assertions.assertNotNull(webElement);
    sleep(3000);

}

猜你喜欢

转载自blog.csdn.net/qq_53190275/article/details/130966577