国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9)

這篇具有很好參考價值的文章主要介紹了博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

目錄

前言:

1.博客前端頁面測試用例圖

2.測試用例的代碼實現(xiàn)

2.1登錄頁面的測試

2.2博客列表頁面的測試

2.3寫博客測試

2.4博客詳情頁面的測試

2.5已發(fā)布博客的標題和時間的測試

2.6注銷用戶的測試

結束語:


前言:

之前小編給大家講解了有關于Selenium和Junit5自動化測試的一些基礎知識,那么下面我們就針對于我們自己做的一個項目來使用Junit來進行一下自動化測試。

博客項目的前后端博客鏈接:前端?http://t.csdn.cn/jZkQd? 后端?http://t.csdn.cn/sN1Uq

博客項目的源碼Gitee鏈接?https://gitee.com/YAUGAOLELE/project

1.博客前端頁面測試用例圖

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

2.測試用例的代碼實現(xiàn)

代碼側邊的展示:

我們一共創(chuàng)建兩個類一個是BlogCase另一個是InitAndEnd。具體代碼的創(chuàng)建請看下邊。
博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

初始化代碼的實現(xiàn):

package BlogTest;

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 {
    //創(chuàng)建驅動
    static WebDriver webDriver;
    //初識化操作,打開瀏覽器
    @BeforeAll
    static void SetUp() {
        webDriver = new ChromeDriver();
    }
    //關閉瀏覽器
    @AfterAll
    static void TearDown() {
        webDriver.quit();
    }
}

2.1登錄頁面的測試

代碼展示:

package BlogTest;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;

import java.util.concurrent.TimeUnit;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{
    /**
     * 輸入正確的賬號,密碼,登錄成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打開博客登錄頁面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //輸入賬號:zhangsan /lisi
        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();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,測試通過,否則測試不通過
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表頁展示的是zhangsan/lisi
        //用戶名是zhangsan測試通過,否則測試不通過
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }
}


結果展示:

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

2.2博客列表頁面的測試

代碼展示:

package BlogTest;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;

import java.util.concurrent.TimeUnit;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{
    /**
     * 輸入正確的賬號,密碼,登錄成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打開博客登錄頁面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //輸入賬號:zhangsan /lisi
        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();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,測試通過,否則測試不通過
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表頁展示的是zhangsan/lisi
        //用戶名是zhangsan測試通過,否則測試不通過
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }

    /**
     *博客列表頁面的博客數(shù)量不為0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打開博客列表頁
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //獲取頁面上的所有博客標題對應的元素,如果這個元素的數(shù)量不為0,則認為測試通過
        //加上只能等待,如果不加的話可能前端頁面沒有渲染出來,就可能導致獲取元素失敗
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //斷言:如果這個元素的數(shù)量不為0,則認為測試通過
        Assertions.assertNotEquals(0,title_num);
    }
}


結果展示:

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

2.3寫博客測試

代碼展示:

package BlogTest;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{
    /**
     * 輸入正確的賬號,密碼,登錄成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打開博客登錄頁面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //輸入賬號:zhangsan /lisi
        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();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,測試通過,否則測試不通過
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表頁展示的是zhangsan/lisi
        //用戶名是zhangsan測試通過,否則測試不通過
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }

    /**
     *博客列表頁面的博客數(shù)量不為0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打開博客列表頁
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //獲取頁面上的所有博客標題對應的元素,如果這個元素的數(shù)量不為0,則認為測試通過
        //加上只能等待,如果不加的話可能前端頁面沒有渲染出來,就可能導致獲取元素失敗
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //斷言:如果這個元素的數(shù)量不為0,則認為測試通過
        Assertions.assertNotEquals(0,title_num);
    }

    /**
     * 寫博客
     */
    @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);
        //找到對應的輸入框,輸入對應的標題。
        webDriver.findElement(By.cssSelector("#title-input"));
        //通過JS進行標題輸入
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自動化測試\"");
        sleep(3000);
        //點擊提交
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //校驗
        //獲取當前頁面的url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);

    }
}


結果展示:

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

2.4博客詳情頁面的測試

代碼展示:

package BlogTest;

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 java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{
    private static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments(
                "http://43.138.29.216:8080/blog_system/blog_detail.html",
                "博客詳情頁",
                "自動化測試"
        ));
    }
    /**
     * 輸入正確的賬號,密碼,登錄成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打開博客登錄頁面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //輸入賬號:zhangsan /lisi
        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();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,測試通過,否則測試不通過
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表頁展示的是zhangsan/lisi
        //用戶名是zhangsan測試通過,否則測試不通過
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }

    /**
     *博客列表頁面的博客數(shù)量不為0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打開博客列表頁
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //獲取頁面上的所有博客標題對應的元素,如果這個元素的數(shù)量不為0,則認為測試通過
        //加上只能等待,如果不加的話可能前端頁面沒有渲染出來,就可能導致獲取元素失敗
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //斷言:如果這個元素的數(shù)量不為0,則認為測試通過
        Assertions.assertNotEquals(0,title_num);
    }

    /**
     * 寫博客
     */
    @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);
        //找到對應的輸入框,輸入對應的標題。
        webDriver.findElement(By.cssSelector("#title-input"));
        //通過JS進行標題輸入
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自動化測試\"");
        sleep(3000);
        //點擊提交
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //校驗
        //獲取當前頁面的url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);

    }

    /**
     * 博客詳情頁面的校驗
     * 1.校驗url
     * 2.校驗博客標題
     * 3.頁面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[2]/a")).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();
        //獲取博客標題
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String cur_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > h3")).getText();
        //校驗
        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);
        if (cur_url.contains(expected_url)) {
            System.out.println("測試通過");
        }else {
            System.out.println(cur_url);
            System.out.println("測試不通過");
        }
    }
}


結果展示:

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

2.5已發(fā)布博客的標題和時間的測試

代碼展示:

package BlogTest;

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)
public class BlogCase extends InitAndEnd{
    private static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments(
                "http://43.138.29.216:8080/blog_system/blog_detail.html",
                "博客詳情頁",
                "自動化測試"
        ));
    }
    /**
     * 輸入正確的賬號,密碼,登錄成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打開博客登錄頁面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //輸入賬號:zhangsan /lisi
        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();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,測試通過,否則測試不通過
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表頁展示的是zhangsan/lisi
        //用戶名是zhangsan測試通過,否則測試不通過
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }

    /**
     *博客列表頁面的博客數(shù)量不為0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打開博客列表頁
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //獲取頁面上的所有博客標題對應的元素,如果這個元素的數(shù)量不為0,則認為測試通過
        //加上只能等待,如果不加的話可能前端頁面沒有渲染出來,就可能導致獲取元素失敗
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //斷言:如果這個元素的數(shù)量不為0,則認為測試通過
        Assertions.assertNotEquals(0,title_num);
    }

    /**
     * 寫博客
     */
    @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);
        //找到對應的輸入框,輸入對應的標題。
        webDriver.findElement(By.cssSelector("#title-input"));
        //通過JS進行標題輸入
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自動化測試\"");
        sleep(3000);
        //點擊提交
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //校驗
        //獲取當前頁面的url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);

    }

    /**
     * 博客詳情頁面的校驗
     * 1.校驗url
     * 2.校驗博客標題
     * 3.頁面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[2]/a")).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();
        //獲取博客標題
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String cur_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > h3")).getText();
        //校驗
        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);
        if (cur_url.contains(expected_url)) {
            System.out.println("測試通過");
        }else {
            System.out.println(cur_url);
            System.out.println("測試不通過");
        }
    }

    @Order(5)
    @Test
    /**
     * 校驗已發(fā)布博客的標題
     * 校驗已發(fā)布博客時間
     */
    void BlogInfoChecked() {
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //獲取第一篇博客的標題
        String first_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title")).getText();
        //獲取第一篇博客的發(fā)布時間
        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-09-03發(fā)布的,測試通過
        if (first_blog_time.contains("2023-09-03")) {
            System.out.println("測試通過");
        }else {
            System.out.println("當前時間是:" + first_blog_time);
            System.out.println("測試不通過");
        }
    }
    
}


結果展示:?

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

2.6注銷用戶的測試

代碼展示:

package BlogTest;

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)
public class BlogCase extends InitAndEnd{
    private static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments(
                "http://43.138.29.216:8080/blog_system/blog_detail.html",
                "博客詳情頁",
                "自動化測試"
        ));
    }
    /**
     * 輸入正確的賬號,密碼,登錄成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打開博客登錄頁面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //輸入賬號:zhangsan /lisi
        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();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,測試通過,否則測試不通過
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表頁展示的是zhangsan/lisi
        //用戶名是zhangsan測試通過,否則測試不通過
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }

    /**
     *博客列表頁面的博客數(shù)量不為0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打開博客列表頁
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //獲取頁面上的所有博客標題對應的元素,如果這個元素的數(shù)量不為0,則認為測試通過
        //加上只能等待,如果不加的話可能前端頁面沒有渲染出來,就可能導致獲取元素失敗
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //斷言:如果這個元素的數(shù)量不為0,則認為測試通過
        Assertions.assertNotEquals(0,title_num);
    }

    /**
     * 寫博客
     */
    @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);
        //找到對應的輸入框,輸入對應的標題。
        webDriver.findElement(By.cssSelector("#title-input"));
        //通過JS進行標題輸入
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自動化測試\"");
        sleep(3000);
        //點擊提交
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //校驗
        //獲取當前頁面的url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);

    }

    /**
     * 博客詳情頁面的校驗
     * 1.校驗url
     * 2.校驗博客標題
     * 3.頁面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[2]/a")).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();
        //獲取博客標題
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String cur_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > h3")).getText();
        //校驗
        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);
        if (cur_url.contains(expected_url)) {
            System.out.println("測試通過");
        }else {
            System.out.println(cur_url);
            System.out.println("測試不通過");
        }
    }

    @Order(5)
    @Test
    /**
     * 校驗已發(fā)布博客的標題
     * 校驗已發(fā)布博客時間
     */
    void BlogInfoChecked() {
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //獲取第一篇博客的標題
        String first_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title")).getText();
        //獲取第一篇博客的發(fā)布時間
        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-09-03發(fā)布的,測試通過
        if (first_blog_time.contains("2023-09-03")) {
            System.out.println("測試通過");
        }else {
            System.out.println("當前時間是:" + first_blog_time);
            System.out.println("測試不通過");
        }
    }

    /**
     * 注銷
     */
    @Order(6)
    @Test
    void Logout() {
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        //校驗url(登錄)
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.29.216:8080/blog_system/login.html",cur_url);
        //校驗提交按鈕
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        Assertions.assertNotNull(webElement);
    }
}


結果展示:

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試

結束語:

好了這節(jié)小編就給大分享到這里啦,希望這節(jié)對大家有關于使用Junit5的自動化測試有一定幫助,想要學習的同學記得關注小編和小編一起學習吧!如果文章中有任何錯誤也歡迎各位大佬及時為小編指點迷津(在此小編先謝過各位大佬啦?。?mark hidden color="red">文章來源:http://www.zghlxwxcb.cn/news/detail-693328.html

博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9),測試,項目,Junit5,自動化測試文章來源地址http://www.zghlxwxcb.cn/news/detail-693328.html

到了這里,關于博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 個人博客系統(tǒng)-測試用例+自動化測試

    個人博客系統(tǒng)-測試用例+自動化測試

    ? ? ? ? 使用selenium4 + Junit5單元測試框架,來進行簡單的自動化測試。 1.?準備工作 (1)引入依賴,此時的pom.xml文件: (2)創(chuàng)建公共類 創(chuàng)建common包,存放公共類。首先創(chuàng)建CommonDriver類來獲取驅動。 ? ? ? ? 如果代碼中使用到了 進行截圖、存儲文件 的操作以及使用了 參數(shù)

    2024年02月09日
    瀏覽(16)
  • 【軟件測試】基于博客系統(tǒng)的自動化測試

    【軟件測試】基于博客系統(tǒng)的自動化測試

    目錄 1.我的博客系統(tǒng)鏈接 2.使用selenium對博客系統(tǒng)進行自動化測試 1.引入依賴 2.創(chuàng)建公共類 3.創(chuàng)建測試套件類 4.測試登陸界面 5. 測試博客列表頁 6.測試寫博客頁面 7.測試刪除博客 8.最終運行結果 用戶登錄 創(chuàng)建一個maven項目,在pop.xml中引入以下依賴 因為對每一個頁面進行測試

    2024年02月15日
    瀏覽(22)
  • 對個人博客系統(tǒng)進行web自動化測試(包含測試代碼和測試的詳細過程)

    對個人博客系統(tǒng)進行web自動化測試(包含測試代碼和測試的詳細過程)

    目錄 一、總述 二、登錄頁面測試 一些準備工作? 驗證頁面顯示是否正確 ?驗證正常登錄的情況 該過程中出現(xiàn)的問題 驗證登錄失敗的情況 ?關于登錄界面的總代碼? 測試視頻 三、注冊界面的自動化測試 測試代碼 過程中出現(xiàn)的bug? 測試視頻 四、博客列表頁測試 登錄情況下的

    2024年02月05日
    瀏覽(25)
  • Python+Selenium自動化測試項目實戰(zhàn)

    Python+Selenium自動化測試項目實戰(zhàn)

    第 1 章 自動化測試 1.1、自動化測試介紹 自動化測試就是通過自動化測試工具幫我們打開瀏覽器,輸入網(wǎng)址,輸入賬號密碼登錄,及登錄后的操作,總的說來自動化測試就是通過自動化測試腳本來幫我們從繁瑣重復的手工測試里面解脫出來,把時間和精力花到更好的地方去,

    2023年04月17日
    瀏覽(26)
  • po+selenium+unittest自動化測試項目實戰(zhàn)

    po+selenium+unittest自動化測試項目實戰(zhàn)

    1、新建一個包名:common(用于存放基本函數(shù)封裝) (1)在common包下新建一個base.py文件,作用:頁面操作封裝。base.py文件代碼如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

    2024年01月17日
    瀏覽(24)
  • Python Selenium3 自動化測試實戰(zhàn):構建高效測試項目

    Python Selenium3 自動化測試實戰(zhàn):構建高效測試項目

    ?? 個人網(wǎng)站:【 海擁】【神級代碼資源網(wǎng)站】【辦公神器】 ?? 基于Web端打造的:??輕量化工具創(chuàng)作平臺 ?? 想尋找共同學習交流的小伙伴,請點擊【全棧技術交流群】 在當今軟件開發(fā)領域,自動化測試成為確保軟件質量和提高開發(fā)效率的關鍵步驟。本文將深入探討如何使

    2024年02月05日
    瀏覽(30)
  • 【python+selenium自動化測試實戰(zhàn)項目】全面、完整、詳細

    【python+selenium自動化測試實戰(zhàn)項目】全面、完整、詳細

    項目名稱:**公司電子零售會員系統(tǒng) 項目目的:實現(xiàn)電子零售會員系統(tǒng)項目自動化測試執(zhí)行 項目版本:v1.0 項目目錄 項目環(huán)境 本版 python 36 pip insatll selenium PyCharm 2017.2.4 Windows 10 10.0 HTMLTestRunner.py 項目框架 unittest單元測試框架 pageobject 設計模式 UI對象庫思想 項目設計 1.一個模

    2024年02月06日
    瀏覽(35)
  • Python+selenium自動化測試實戰(zhàn)項目(全面,完整,詳細)

    前言 之前的文章說過, 要寫一篇自動化實戰(zhàn)的文章, 這段時間比較忙再加回家過清明一直沒有更新,今天整理一下實戰(zhàn)項目的代碼共大家學習。(注:項目是針對我們公司內(nèi)部系統(tǒng)的測試,只能內(nèi)部網(wǎng)絡訪問,外部網(wǎng)絡無法訪問) 問: 1.外部網(wǎng)絡無法訪問,代碼也無法運行

    2024年02月13日
    瀏覽(27)
  • Day924.自動化測試 -系統(tǒng)重構實戰(zhàn)

    Day924.自動化測試 -系統(tǒng)重構實戰(zhàn)

    Hi,我是 阿昌 ,今天學習記錄的是關于 自動化測試 的內(nèi)容。 自動化測試是一個很容易產(chǎn)生“爭議”的話題,也經(jīng)常會有一些很有意思的問題。 自動化測試不是應該由測試同學來編寫嗎,開發(fā)是不是沒有必要學吧? 之前一個自動化測試都沒寫過,怎么開始落地呢? 編寫自動

    2023年04月14日
    瀏覽(19)
  • selenium+python web自動化測試框架項目實戰(zhàn)實例教程

    selenium+python web自動化測試框架項目實戰(zhàn)實例教程

    自動化測試對程序的回歸測試更方便。 由于回歸測試的動作和用例是完全設計好的,測試期望的結果也是完全可以預料的,將回歸測試自動運行... 可以運行更加繁瑣的測試 自動化測試的一個明顯好處就是可以在很短的時間內(nèi)運行更多的測試。學習自動化測試最終目的是應用到

    2024年02月06日
    瀏覽(36)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包