【自动化项目实战】博客系统

news2024/10/6 11:55:44

目录

1.博客自动化测试用例

2.准备工作 

3.将手工测试用例转化为自动化测试用例

3.1 初始化动作

3.2 登录

3.3 博客列表博客数量

3.4 查看全文

3.5 写博客&发表博客

3.6 删除

3.7 注销

4.总代码


🌈这节文章我们讲解一个实战项目——博客系统。首先我们需要熟悉项目、针对核心流程设计测试用例(手工测试用例)、将手工测试用例转换成自动化测试、部署

1.博客自动化测试用例

2.准备工作 

1️⃣首先需要创建一个 maven 项目

2️⃣接下来引入依赖:selenium、commons-io、junit、suite、engine;记得放在标签 dependencies 里面

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
    <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite</artifactId>
    <version>1.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.9.1</version>
</dependency>

3.将手工测试用例转化为自动化测试用例

3.1 初始化动作

1️⃣在 java 文件下创建一个 Blog 包(进行初始化):创建 InitAndEnd.java 文件

在初始化当中,首先我们创建一个驱动,并且使用一个注释 @BeforeAll 打开浏览器;使用 @AfterAll 关闭浏览器

InitAndEnd.java 代码:

package Blog;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class InitAndEnd {
    //创建驱动
    static WebDriver webDriver;
    //初始化
    @BeforeAll
    static void SetUp() {
        //打开浏览器
        webDriver = new ChromeDriver();
    }

    //关闭浏览器
    @AfterAll
    static void TearDown() {
        webDriver.quit();
    }
}

3.2 登录

1️⃣在Blog 包下进行测试:创建 BlogCases.java 文件

2️⃣在 resources 下创建 LoginSuccess.csv

登录页面的测试用例:

  1. 打开登录页面
  2. 输入账号 admin:我们通过 css 选择器寻找用户名的 id 元素
  3. 输入密码 123:在这里我们继续通过 css 选择器寻找密码框的 id 元素
  4. 点击提交按钮:通过 css 选择器提交按钮的 id 元素
  5. 跳转到列表页面:获取当前页面的 url 如果 url 与 登陆页面的地址相同,那么测试用例通过,否则测试用例不通过
  6. 列表页展示用户信息是 admin:同样的测试方式用户名是 admin ,测试通过,否则测试不通过

在这里每一步操作我们进行一个显式等待:webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

LoginSuccess.csv 代码

admin, 123, http://42.192.83.143:8563/blog_system/blog_list.html

BlogCases.java 代码

package Blog;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;

//继承  InitAndEnd,让每个测试用例有驱动
public class BlogCases extends InitAndEnd{
    //第一个测试用例:登录
    /**
     * 输入正确的账号密码,登录成功
     */
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url) {
        //1.打开登陆页面
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //2.输入账号admin——通过css选择器找用户名框的id元素
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //3.输入密码123——通过css选择器寻找密码框的id元素
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //4.点击提交按钮——通过css选择器寻找提交按钮的id元素
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待

        //5.跳转到列表页
        //获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        //如果 url = http://42.192.83.143:8563/blog_system/blog_list.html,测试通过;否则测试不通过
        Assertions.assertEquals(blog_list_url, cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待

        //6.列表页展示用户信息是admin
        //用户名是 admit 测试通过,否则测试不通过——通过css选择器获取admin元素
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
        Assertions.assertEquals(username, cur_admin);
    }
}

3.3 博客列表博客数量

 博客列表博客数量测试用例:

  1. 打开博客列表页
  2. 获取页面上所有博客标题对应的元素:注意找的是一批元素,使用 findElements 获取
  3. 如果元素数量不为0,测试通过

此时我们在 BlogCases.java 文件中继续编写代码

BlogCases.java 代码:

package Blog;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;
import static java.lang.Thread.sleep;

//继承  InitAndEnd,让每个测试用例有驱动
public class BlogCases extends InitAndEnd{
    //第一个测试用例:登录
    /**
     * 输入正确的账号密码,登录成功
     */
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url) {
        //1.打开登陆页面
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //2.输入账号admin——通过css选择器找用户名框的id元素
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //3.输入密码123——通过css选择器寻找密码框的id元素
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //4.点击提交按钮——通过css选择器寻找提交按钮的id元素
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待

        //5.跳转到列表页
        //获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        //如果 url = http://42.192.83.143:8563/blog_system/blog_list.html,测试通过;否则测试不通过
        Assertions.assertEquals(blog_list_url, cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待

        //6.列表页展示用户信息是admin
        //用户名是 admit 测试通过,否则测试不通过——通过css选择器获取admin元素
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
        Assertions.assertEquals(username, cur_admin);
    }


    /**
     * 博客列表博客数量
     */
    @Test
    void BlogList() {
        //1.打开博客列表页
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        //2.获取页面上所有博客标题对应的元素——找的是一批元素,所以是 findElements
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //如果元素数量不为0,测试通过(断言)
        Assertions.assertNotEquals(0, title_num);
    }
}

3.4 查看全文

查看全文测试用例:

  1. 找到第一篇博客对应的查看全文按钮:使用xpath 定位
  2. 获取当前页面的 url
  3. 获取当前页面的 title
  4. 获取博客标题:使用css 选择器
  5. 检验(断言)

在这里我们使用 顺序执行:@TestMethodOrder(MethodOrderer.OrderAnnotation.class)

此时我们在 BlogCases.java 文件中继续编写代码

BlogCases.java 代码:

package Blog;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
//继承  InitAndEnd,让每个测试用例有驱动
public class BlogCases extends InitAndEnd{
    //第一个测试用例:登录
    /**
     * 输入正确的账号密码,登录成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url) {
        //1.打开登陆页面
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //2.输入账号admin——通过css选择器找用户名框的id元素
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //3.输入密码123——通过css选择器寻找密码框的id元素
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //4.点击提交按钮——通过css选择器寻找提交按钮的id元素
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待

        //5.跳转到列表页
        //获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        //如果 url = http://42.192.83.143:8563/blog_system/blog_list.html,测试通过;否则测试不通过
        Assertions.assertEquals(blog_list_url, cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待

        //6.列表页展示用户信息是admin
        //用户名是 admit 测试通过,否则测试不通过——通过css选择器获取admin元素
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
        Assertions.assertEquals(username, cur_admin);
    }


    /**
     * 博客列表博客数量
     */
    @Order(2)
    @Test
    void BlogList() {
        //1.打开博客列表页
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        //2.获取页面上所有博客标题对应的元素——找的是一批元素,所以是 findElements
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //如果元素数量不为0,测试通过(断言)
        Assertions.assertNotEquals(0, title_num);
    }


    /**
     * 博客详情页校验
     * url
     * 博客标题
     * 页面 title 是博客详情页
     */
    @Order(4)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url, String expected_title, String expected_blog_title) {
        //1.找到第一篇博客对应的查看全文按钮——xpath 定位
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
        //2.获取当前页面的 url
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        String cur_url = webDriver.getCurrentUrl();
        //3.获取当前页面的 title
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        String cur_title = webDriver.getTitle();
        //4.获取博客标题——css 选择器
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        String cur_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.right > div > h3")).getText();
        //5.检验(断言)
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        Assertions.assertEquals(expected_url, cur_url);
        Assertions.assertEquals(expected_title, cur_title);
        Assertions.assertEquals(expected_blog_title, cur_blog_title);

    }

    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://42.192.83.143:8563/blog_system/blog_detail.html",
                "博客详情页", "自动化测试"));
    }
}

3.5 写博客&发表博客

    /**
     * 写博客
     */
    @Order(3)
    @Test
    void EditBlog() throws InterruptedException {
        //1.找到写博客按钮并点击——css选择器
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //2.通过 Js 将标题进行输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        sleep(3000);
        webDriver.findElement(By.cssSelector("title"));
        //3.点击发布
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //4.获取当前页面的 url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://42.192.83.143:8563/blog_system/blog_list.html", cur_url);
    }

    /**
     * 检验已发布博客标题
     * 校验已发布博客时间
     */
    @Order(5)
    @Test
    void BlogInfoChecked() {
        //1.打开博客列表页
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        //2.获取第一篇博客标题
        String first_blog_title = 
                webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
        //3. 获取第一篇博客发表时间
        String first_blog_time = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();
        //4.校验博客标题是不是自动化测试
        Assertions.assertEquals("自动化测试", first_blog_title);
        //5.如果时间是 2023-05-31 发布的,测试通过
        if (first_blog_title.contains("2023-05-31")) {
            System.out.println("测试通过");
        } else {
            System.out.println("当前时间是:" + first_blog_time);
            System.out.println("测试不通过");
        }
    }

3.6 删除

    /**
     *
     * 删除和刚才发布的博客
     */
    @Order(6)
    @Test
    void DeleteBlog() throws InterruptedException {
        // 1.打开博客列表页面
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        // 2.点击全文按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
        // 3.点击删除按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).click();
        sleep(3000);
        // 4.博客列表页第一篇博客标题不是“自动化测试”
        String first_blog_title = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]")).getText();
        // 5.校验当前博客标题不等于“自动化测试”
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertNotEquals(first_blog_title, "自动测试");
    }

3.7 注销

注销验证包含:当前页面的 url 和 提交按钮

    /**
     * 注销
     */
    @Order(7)
    @Test
    void Logout() {
        //1.找到注销按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        //2.校验 url(登录的 url)
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://42.192.83.143:8563/blog_system/blog_login.html", cur_url);
        //3.检验提交按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull(webElement);
    }

4.总代码

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>blogging</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <version>1.9.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

LoginSuccess.csv:

admin,123,http://42.192.83.143:8563/blog_system/blog_list.html

InitAndEnd.java:

package Blog;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class InitAndEnd {
    //创建驱动
    static WebDriver webDriver;
    //初始化
    @BeforeAll
    static void SetUp() {
        //打开浏览器
        webDriver = new ChromeDriver();
    }

    //关闭浏览器
    @AfterAll
    static void TearDown() {
        webDriver.quit();
    }
}

BlogCases.java:

package Blog;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)

//继承  InitAndEnd,让每个测试用例有驱动
public class BlogCases extends InitAndEnd{
    //第一个测试用例:登录
    /**
     * 输入正确的账号密码,登录成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url) {
        //1.打开登陆页面
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //2.输入账号admin——通过css选择器找用户名框的id元素
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //3.输入密码123——通过css选择器寻找密码框的id元素
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //4.点击提交按钮——通过css选择器寻找提交按钮的id元素
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待

        //5.跳转到列表页
        //获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        //如果 url = http://42.192.83.143:8563/blog_system/blog_list.html,测试通过;否则测试不通过
        Assertions.assertEquals(blog_list_url, cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待

        //6.列表页展示用户信息是admin
        //用户名是 admit 测试通过,否则测试不通过——通过css选择器获取admin元素
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
        Assertions.assertEquals(username, cur_admin);
    }


    /**
     * 博客列表博客数量
     */
    @Order(2)
    @Test
    void BlogList() {
        //1.打开博客列表页
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        //2.获取页面上所有博客标题对应的元素——找的是一批元素,所以是 findElements
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //如果元素数量不为0,测试通过(断言)
        Assertions.assertNotEquals(0, title_num);
    }


    /**
     * 博客详情页校验
     * url
     * 博客标题
     * 页面 title 是博客详情页
     */
    @Order(4)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url, String expected_title, String expected_blog_title) {
        //1.找到第一篇博客对应的查看全文按钮——xpath 定位
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
        //2.获取当前页面的 url
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        String cur_url = webDriver.getCurrentUrl();
        //3.获取当前页面的 title
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        String cur_title = webDriver.getTitle();
        //4.获取博客标题——css 选择器
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        String cur_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.right > div > h3")).getText();
        //5.检验(断言)
        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(cur_url);
            System.out.println("测试不通过 ");
        }
    }

    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://42.192.83.143:8563/blog_system/blog_detail.html",
                "博客详情页", "自动化测试"));
    }

    /**
     * 写博客
     */
    @Order(3)
    @Test
    void EditBlog() throws InterruptedException {
        //1.找到写博客按钮并点击——css选择器
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//显式等待
        //2.通过 Js 将标题进行输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        sleep(3000);
        webDriver.findElement(By.cssSelector("title"));
        //3.点击发布
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //4.获取当前页面的 url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://42.192.83.143:8563/blog_system/blog_list.html", cur_url);
    }

    /**
     * 检验已发布博客标题
     * 校验已发布博客时间
     */
    @Order(5)
    @Test
    void BlogInfoChecked() {
        //1.打开博客列表页
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        //2.获取第一篇博客标题
        String first_blog_title = 
                webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
        //3. 获取第一篇博客发表时间
        String first_blog_time = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();
        //4.校验博客标题是不是自动化测试
        Assertions.assertEquals("自动化测试", first_blog_title);
        //5.如果时间是 2023-05-31 发布的,测试通过
        if (first_blog_title.contains("2023-05-31")) {
            System.out.println("测试通过");
        } else {
            System.out.println("当前时间是:" + first_blog_time);
            System.out.println("测试不通过");
        }
    }

    /**
     *
     * 删除和刚才发布的博客
     */
    @Order(6)
    @Test
    void DeleteBlog() throws InterruptedException {
        // 1.打开博客列表页面
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        // 2.点击全文按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
        // 3.点击删除按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).click();
        sleep(3000);
        // 4.博客列表页第一篇博客标题不是“自动化测试”
        String first_blog_title = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]")).getText();
        // 5.校验当前博客标题不等于“自动化测试”
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertNotEquals(first_blog_title, "自动测试");
    }


    /**
     * 注销
     */
    @Order(7)
    @Test
    void Logout() {
        //1.找到注销按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        //2.校验 url(登录的 url)
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://42.192.83.143:8563/blog_system/blog_login.html", cur_url);
        //3.检验提交按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull(webElement);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/593208.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

在前公司年薪38W,经人内推腾讯居然被拒了···

末流院校&#xff0c;带17人研发团队&#xff0c;到手38w股票20w&#xff0c;过硬的技术让我觉得可以出去“闯闯”;内推到某大厂&#xff0c;电话里聊得挺好&#xff0c;结果第二天说不给安排面试了…… 被拒绝很正常&#xff0c;想必应该是能力不足&#xff0c;不能满足公司的…

太难了,00后求求你们别这么卷了....

在程序员职场上&#xff0c;什么样的人最让人反感呢? 是技术不好的人吗?并不是。技术不好的同事&#xff0c;我们可以帮他。 是技术太强的人吗?也不是。技术很强的同事&#xff0c;可遇不可求&#xff0c;向他学习还来不及呢。 真正让人反感的&#xff0c;是技术平平&#x…

网络传输(传输介质、通信方式、交换方式)

目录 一、传输介质1.双绞线2.网线安装3.光纤4.无线信道 二、通信方式、交换方式1.通信方式2.同步方式3.交换方式 一、传输介质 1.双绞线 双绞线&#xff1a;将多根铜线按规则缠绕在一起&#xff0c;能够减少干扰&#xff1b;分为无屏蔽双绞线UTP和屏蔽双绞线STP&#xff0c;都…

PCIE学习

目录 一、PCIE结构1、层次结构2、数据包TLPDLLP PCIE寄存器配置1、基址寄存器的作用2、基址寄存器的位置 三、PCIE读取数据 一、PCIE结构 1、层次结构 绝大多数的总线或者接口&#xff0c;都是采用分层实现的。PCIe也不例外&#xff0c;它的层次结构如下&#xff1a; PCIe定…

MAYA绳子和铁链动画(3个例子)

一两条边中间加定位器 // Copyright (C) 2000-2001 Michael Bazhutkin - Copyright (C) 2000 studio Klassika // www.geocites.com/bazhutkin // bazhutkinmail.ru // // Rivet (button) 1.0 // Script File // MODIFY THIS AT YOUR OWN RISK // // Creation Date: Apri…

【Unittest】自动化测试框架核心要素

1、什么是Unittest框架&#xff1f; python自带一种单元测试框架 2、为什么使用UnitTest框架&#xff1f; >批量执行用例 >提供丰富的断言知识 >可以生成报告 3、核心要素&#xff1a; 1). TestCase&#xff08;测试用例&#xff09; 2). TestSuite(测试套件)…

系统分析师:七、软件工程(含系统规划)

一、软件生命周期 软件生命周期分为5个&#xff1a;获取过程、供应过程、开发过程、运行过程、维护过程&#xff0c;具体如下&#xff1a; 二、软件开发方法 2.1 形式化方法 该方法的思想是利用形式化语言&#xff0c;严格定义需求&#xff0c;并用数据推演的方法证明需求的性…

隐藏在Microsoft Designer背后的新科技,让人人都是设计师

编者按&#xff1a;在视觉图像设计中&#xff0c;用户的需求与最终的设计成品往往是“想象很美好&#xff0c;现实很骨感”。这通常是因为用户在与设计师沟通时&#xff0c;双方理解不一致&#xff0c;导致最终设计结果不尽如人意。但是&#xff0c;如果能够“自给自足”&#…

COMSOL晶体取向多晶材料Voronoi泰森多边形力学模拟

多晶材料几何模型模型构建采用的CAD Voronoi V2.3版本&#xff0c;可分图层对晶格进行绘制&#xff0c;分别导入有限元软件后实现三种晶体取向的差异性。 将构建好的Voronoi多晶体几何模型文件导入到COMSOL内&#xff0c;构建好晶体结构模型后&#xff0c;进行材料赋值操作&am…

Oracle常用傻瓜问题100问

大家在应用ORACLE的时候可能会遇到很多看起来不难的问题, 特别对新手来说, 今天我简单把它总结一下, 发布给大家, 希望对大家有帮助! 和大家一起探讨, 共同进步! 对ORACLE高手来说是不用看的. 1. Oracle安装完成后的初始口令? internal/oracle sys/change_on_install system/m…

Spring6《学习笔记(22版尚硅谷)》

Spring6 1、概述 1.1、Spring是什么&#xff1f; Spring 是一款主流的 Java EE 轻量级开源框架 &#xff0c;Spring 由“Spring 之父”Rod Johnson 提出并创立&#xff0c;其目的是用于简化 Java 企业级应用的开发难度和开发周期。Spring的用途不仅限于服务器端的开发。从简单…

io之socket编程

写在前面 本文通过socket编程来实现一个简单的HttpServer。 1&#xff1a;单线程版本 我们使用单线程来实现一个HttpServer&#xff0c;如下&#xff1a; package dongshi.daddy.io.httpserver;import java.io.PrintWriter; import java.net.ServerSocket; import java.net.…

何为儒家的四书五经?

中国古代的四书五经是儒家经典之一&#xff0c;是中国古代最为重要的经典之一。它们包括了四书&#xff1a;《大学》、《中庸》、《论语》、《孟子》以及五经&#xff1a;《诗经》、《尚书》、《礼记》、《周易》、《春秋》&#xff0c;被誉为“经国之宝”、“德育之本”。 四书…

java服务接入SkyWalking时生成TraceId信息(基于logback)

java服务生成TraceId 一、背景二、配置2.1 pom文件引入依赖2.2 logback-spring.xml配置 三、启动项目 一、背景 springboot服务接入SkyWalking时&#xff0c;想要在控制台输出TraceId信息&#xff0c;如下图的效果&#xff1a; 二、配置 参考文章&#xff1a; https://juej…

2023最全性能测试学习指南【建议收藏】

浅谈软件测试中的性能测试 很多时候&#xff0c;我们都知道软件有黑白盒测试&#xff0c;但往往还遗漏掉了一个性能测试。 在下面的这篇文章中&#xff0c;就带领大家来了解性能测试。一起来学习吧~ 学习目录 一、 性能测试概念 二、 性能测试指标 三、 性能测试种类 四、 性能…

“政会银企”齐聚纵目科技,探索四方合作新模式

近日&#xff0c;纵目科技携手浦东新区工商业联合会、浦东新区金融工作局、上海市人工智能行业协会、交通银行张江支行、招商银行上海分行外滩支行、中信银行上海漕河泾支行、中国建设银行张江分行举办了一场别开生面的“政会银企”座谈会&#xff0c;深入交流、探讨了推动四方…

并发编程学习(十四):tomcat线程池

1、Tomcat 功能组件结构 Tomcat 的核心功能有两个&#xff0c;分别是负责接收和反馈外部请求的连接器 Connector&#xff0c;和负责处理请求的容器 Container。 其中连接器和容器相辅相成&#xff0c;一起构成了基本的 web 服务 Service。每个 Tomcat 服务器可以管理多个 Servi…

同声传译方法有哪些?我来给你介绍三个同声传译的好方法

假设有一场国际性的会议&#xff0c;参会者来自不同的国家和语言背景。在会议中&#xff0c;主要的演讲和讨论都是以主持人或演讲者的母语进行&#xff0c;这个时候场中的其他人很可能因为语言不通而无法理解演讲的内容&#xff0c;而翻译人员人数不足的时候&#xff0c;就更难…

Nmap常用基础命令详解

阅读目录 Nmap 主机发现扫描 Nmap 使用扫描脚本 Nmap 内网服务扫描 Nmap 是免费开放源代码实用程序&#xff0c;用于网络发现和安全审核。许多系统和网络管理员还发现它对于诸如网络清单&#xff0c;管理服务升级计划以及监视主机或服务正常运行时间之类的任务很有用。Nmap以…

2023全国科技工作者日——回顾2023小蛮腰科技大会暨AIGC人工智能峰会,致敬全国科技工作者

文章目录 一、前言二、2023全国科技工作者日三、回顾2023小蛮腰科技大会暨AIGC人工智能峰会3.1 关于小蛮腰科技大会暨AIGC人工智能峰会的背景3.2 2023小蛮腰科技大会&#xff1a;探寻AIGC新未来3.2.1 开幕式暨主论坛3.2.2 平行论坛1&6&#xff1a;迈向智能时代&#xff0c;…