自动化项目测试(个人博客系统)

自动化项目测试(个人博客系统)

一、测试环境搭建

Java+Selenium环境搭建

前提:已经安装了JDK8环境。

1.初始工作:安装Chrome

Google Chrome 网络浏览器

下载安装完成即可(建议安装低一点的版本,方便后续依赖版本兼容),下载完成后打开查看自己的版本(先记住)
在这里插入图片描述
在这里插入图片描述

2.下载Chrome浏览器驱动

https://chromedriver.chromium.org/downloads

在这里插入图片描述

安装与谷歌浏览器版本一致的版本驱动,点击去选择下图所示安装即可

在这里插入图片描述

3.配置系统环境变量

将下载好的对应驱动安装包解压放在浏览器的安装目录下

在这里插入图片描述

回到桌面右击此电脑选择属性,进入关于页面

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

这样就配置好了环境。然后去到idea中创建一个Maven项目,再pom.xml中添加依赖即可。

<!-- 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>

二、项目主要功能

1.注册登录

2.我的个人博客列表

3.查看全文

4.删除文章

5.添加文章

6.修改文章

7.首页列表页显示所有文章

8.首页分页功能

9.退出登录

三、功能测试

1.测试用例设计(手工测试用例)

下图看不清可以点击此链接查看。
测试用例设计
在这里插入图片描述

2.将手工测试用例转换成自动化测试

代码结构设计

(1)初始化动作:BeforeAll       创建对应驱动

(2)退出动作:AfterAll              退出浏览器
package blogs;

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();
    }

}

开始我们可以不急着使用@AfterAll在每次测试结束后关闭会话,我们可以手动关闭,方便查看测试效果。

(1)用户注册

//    用户注册
    @Test
    void RegistSuccess() throws InterruptedException {
//        打开注册页面
        webDriver.get("http://localhost:8080/html/blog_login.html");
//        点击切换按钮进入注册页面
        webDriver.findElement(By.cssSelector("body > div > div.login > div.login_right > input")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div > div.login > div.login_right > input")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//        输入用户注册信息
        webDriver.findElement(By.cssSelector("#username")).sendKeys("果果");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#password1")).sendKeys("123");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#password2")).sendKeys("123");
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);
//        点击注册提交
//        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        // 处理JavaScript的alert弹窗
        Alert alert = webDriver.switchTo().alert();
        String alertText = alert.getText();

//        断言来验证alert的内容
//        验证alert的文本内容是否包含”注册成功“文本
        assert (alertText.contains("注册成功"));
        // 关闭alert弹窗
        alert.accept();

    }

这里的话在注册成功后会有一个弹窗提示注册成功,所以这里话需要去对这个弹窗进行处理,然后使用Alert去处理弹窗,然后捕获到弹窗的内容,通过使用断言去验证alert的内容,如果包含了”注册成功“文本,那么就关闭弹窗,显示登录页。
在这里插入图片描述
因为我们使用@Test注解的方式的实用性不是很好,对于一些共有的量我都需要不断地去重复写,而且更改的话也不方便,所以这里@ParameterizedTest + @CsvFileSource(resources = “LoginSuccess.csv”)将信息存储到该.csv文件中,webDriver自己去拿获取就行。

(2)用户登录

//    用户登录
    @Test
    void LoginSuccess() throws InterruptedException {
        webDriver.get("http://localhost:8080/html/blog_login.html");
        webDriver.findElement(By.cssSelector("#username3")).sendKeys("胡大锤");
        webDriver.findElement(By.cssSelector("#password3")).sendKeys("123");
        sleep(3000);
//        点击登陆按钮提交
        webDriver.findElement(By.cssSelector("#submit1")).click();
    }

在这里插入图片描述

同样的我们处理弹框信息点击确定后进入首页所有用户的文章列表页,也是使用一样的方式通过Alert去获取弹窗的内容,然后使用断言去判断,弹窗内容与我们期待的是否一致。这里要注意的是弹窗的加载响应可能会很慢,如果我们不加时间等待页面弹窗响应完成的话,可能会出org.openqa.selenium.NoAlertPresentException: no such alert"错误,它是在 Selenium WebDriver 中的常见异常,当我们尝试与不存在或当前不在网页上的警告对话框(例如警告、确认或提示对话框)进行交互时会出现,所以通过延时等待3秒操作(sleep(3000))即可通过测试用例。

        sleep(3000);
        Alert alert = webDriver.switchTo().alert();
        String logtext = alert.getText();

//        使用断言验证alert内容
        assert (logtext.contains("登录成功"));
//        关闭弹窗
        alert.accept();

关闭弹窗后验证进入的页面是不是我的个人文章列表页

//        关闭弹窗后进入个人博客列表页
        String cur_url = webDriver.getCurrentUrl();
        String expect_url = "http://localhost:8080/html/blog_list.html";
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        如果url等于http://localhost:8080/html/blog_list.html测试通过,否则不通过
        Assertions.assertEquals(expect_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

在这里插入图片描述

(3)个人博客列表

这里的话有两种情况,一种是我登陆的用户没有发布过一篇文章,一种是我登陆的用户发布的有文章,如果有发布过文章,那么通过获取每篇文章的标题,确认数量不为0即可通过测试用例,如果没有发布过文章,那么就会显示”添加暂无文章,请先添加文章!“文字,同时点击添加的话就能够跳转到文章添加页面,测试通过。

我们以胡大锤用户登录,可以发现胡大锤用户是通过测试的,有文章的。

//    个人博客列表页
//    如果有发布的文章数量不为0
    @Test
    void MyBlogList() throws InterruptedException {
//        1.首先是需要通过登录进入访问所以我们直接调用登录成功的方法
        LoginSuccess();
//        2.获取页面上所有博客标题对应的元素
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int mytitle_num = webDriver.findElements(By.cssSelector("#artListDiv > div:nth-child(2) > div.zong_title > h2")).size();
//        如果元素数量不为0,而且也能显示出来,那么测试通过
        if(mytitle_num != 0) {
            System.out.println("有文章测试通过!");
        }else {
//            如果元素为0那么就要显示(添加暂无文章,请先添加文章!)
           String elemText = webDriver.findElement(By.cssSelector("#artListDiv > h3")).getText();
           if(elemText.contains("添加暂无文章,请先添加文章!")) {
//               判定添加是否能够跳转到添加文章页面
               webDriver.findElement(By.cssSelector("#artListDiv > h3 > a")).click();
               webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
               String addurl = "http://localhost:8080/html/blog_add.html";
               String cururl = webDriver.getCurrentUrl();
               Assertions.assertEquals(addurl,cururl);
               System.out.println("没文章测试通过!");
           }else {
               System.out.println("没文章测试不通过!");
           }

        }
    }

在这里插入图片描述
通过qingshi登录进去,可以发现是没有文章的,那么就会显示添加暂无文章,请先添加文章!“文字,同时点击添加的话就能够跳转到文章添加页面,测试通过。
在这里插入图片描述

(4)查看全文

点击查看全文能够进入文章的详情页,通过获取当前页与点击进去后的页面是否一致,进入查看全文后的页面标题是不是文章详情页,判定是否测试通过。

//    查看全文按钮
    @Test
    void SeeBtn() throws InterruptedException {
//        从个人列表博客页点击查看全文按钮
        LoginSuccess();

        webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(5)")).click();
//        获取当前页面
        String cur_url = webDriver.getCurrentUrl();
//        期待的页面
        String exp_url = "http://localhost:8080/html/blog_detail.html?aid=21";
        Assertions.assertEquals(exp_url,cur_url);
//        获取当前页博客标题
        String cur_title = webDriver.getTitle();
//        Assertions.assertEquals(cur_title,curs_title);

//        判断标题一致
        if(cur_title.contains("文章详情页")) {
            System.out.println("测试通过!");
        }else {
//            System.out.println("上一页标题:" + curs_title + "\n" + "下一页标题:" + cur_title);
            System.out.println("测试不通过!");
        }
        
    }

在这里插入图片描述

(5)删除文章

通过点击我的博客列表中的删除文章按钮,然后提示是否要删除该篇文章,点击确定的话就删除成功,刷新当前文章列表页,刚才的文章已经没了,点击取消的话就不做任何操作。
在这里插入图片描述

我们删除第一篇博客为例
在这里插入图片描述

我们点击删除文章按钮,获取当前要删除的标题,然后出现弹窗提示是否要删除,如果确认删除,那么就会刷新当前页面,再次获取当前第一篇博客的标题,已经不是刚才删除的标题了,证明删除成功,如果取消了删除,那么就不操作。测试通过。

//    删除文章
    @Test
    void DeleteBlog() throws InterruptedException {
//        通过登录进入到个人博客列表页
        LoginSuccess();
//        我们以删除第一篇博客(我要删除)为例
//        获取当前文章的标题
        String title1 = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.zong_title > h2")).getText();
//        点击删除按钮
        webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(4)")).click();
//        等待弹窗
        sleep(3000);
//        处理弹窗
        Alert alert = webDriver.switchTo().alert();
//        获取弹窗的文本
        String alertText = alert.getText();
        if(alertText.contains("是否确认要删除该篇文章?")) {
//            如果是的话,那么可以点击取消,也可以点击确认
            boolean btn = true;  //true表示确认删除,false表示取消
            if (btn) {
                alert.accept();  //确认
//                确认删除后刷新当前页面,但是没有了刚才的博客
//                在获取此时的第一篇博客标题看是不是刚才的,不是证明测试通过
                String title2 = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.zong_title > h2")).getText();
                Assertions.assertEquals(title2,title1);
                System.out.println("点击确认删除:测试通过");
                alert.accept();
            }else {
                alert.dismiss();  //取消
//                取消后不做任何操作
                System.out.println("取消操作:测试通过");
            }


        }else {
            System.out.println("没有显示弹窗文本内容:测试不通过!");
        }
    }

在这里插入图片描述

(6)添加文章

添加文章是在每个页面的导航栏上都会有一个写博客的按钮,点击写博客就会进入到添加文章页面。然后我们以用户(qingshi _123)登陆进去后的我的博客列表页的写博客按钮进行触发测试用例。
在这里插入图片描述

我们在写博客的时候通过JS进行输入(document.getElementById)获取标题表单输入,去设置值。因为正文默认是有值的,所以我们可以直接去获取按钮点击发布,此时会弹出添加成功,是否要继续添加文章,可以选择继续,可以选择取消,继续的话就是又跳转到当前的写文章,取消的话就会跳转到我的个人博客列表。可以查看到已经发布的文章(由于列表是根据时间降序的,这里可以通过看获取的第一篇标题是不是刚才写的)。
在这里插入图片描述
通过js来实现对文本的输入
在这里插入图片描述

//    添加文章
//    以登陆后进入到我的个人博客列表点击导航栏的写博客按钮为例
    @Test
    void AddBLog() throws InterruptedException {
//        调用登录方法,进入我的个人博客页面
        LoginSuccess();

//        找到写博客按钮,点击
        webDriver.findElement(By.cssSelector("body > div > div.nav > div.right > ul > li:nth-child(2) > a")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String write_url = webDriver.getCurrentUrl();

//        通过JS进行输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value = \"测试添加博客1\";");
        sleep(3000);
//        点击发布按钮
        webDriver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        sleep(3000);
//        出现了弹框
//        处理弹框,是否要继续添加和取消操作
        Alert alert = webDriver.switchTo().alert();
//        获取弹窗的文本
        String alertText = alert.getText();
        if(alertText.contains("恭喜添加成功!是否继续添加文章?")) {
//            继续添加
            boolean btn = false;  //表示确认呢继续添加
            if(btn) {
                alert.accept();
//                确认继续添加的话,页面还是当前页面
                //        获取当前页面
                String cur_url = webDriver.getCurrentUrl();
                Assertions.assertEquals(write_url,cur_url);
                System.out.println("继续添加文章:测试通过");
            }else {
                alert.dismiss();
                sleep(3000);
//                取消添加的话那么就跳转到我的个人博客列表页,并且能够看到我刚才发布的文章
                String cur_url = webDriver.getCurrentUrl();
                Assertions.assertEquals("http://localhost:8080/html/blog_list.html",cur_url);
                System.out.println("取消添加:测试通过!");
            }
        }else {
            System.out.println("文章发布失败!测试不通过!");
        }

    }

在这里插入图片描述

(7)校验发布的文章标题和发布时间

在这里插入图片描述
返回到个人博客列表页,因为列表页是按照发布的时间降序排列的,所以可以通过获取第一篇博客标题,和时间确认和我们刚才发的是否一致,测试通过即可。

//    校验发布文章的标题,和时间
    @Test
    void CheckBlog() throws InterruptedException {
        LoginSuccess();
//        1.获取当前页面
        webDriver.get("http://localhost:8080/html/blog_list.html");
//        获取第一篇博客的标题
        String first_blog_title = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.zong_title > h2")).getText();
//        获取第一篇博客发布的时间
        String first_blog_time = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.zong_title > span")).getText();
        Assertions.assertEquals("测试添加博客1",first_blog_title);
//        如果时间是2023-09-08则测试通过
        if(first_blog_time.contains("2023-09-08")) {
            System.out.println("测试通过");
        }else {
            System.out.println("当前时间是:" + first_blog_time);
            System.out.println("测试不通过!");
        }

    }

在这里插入图片描述

(8)修改文章

在我的个人博客列表页,点击修改文章按钮,然后进入到文章修改页面,这里的话要验证是不是能够将对应的标题以及文章内容显示在对应的标题输入框和markdown区域中,然后点击修改按钮,能够实时更新数据,跳转到我的个人博客列表页,查看刚才修改的内容。以第一篇博客为例进行修改测试。
在这里插入图片描述

//    修改文章
    @Test
    void UpdateBlog() throws InterruptedException {
//        1.首先也是一样的登录进入到个人博客列表页
        LoginSuccess();
//        2.获取第一篇博客的标题
        String first_title = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.zong_title > h2")).getText();

//        3.获取修改按钮并点击
        webDriver.findElement(By.xpath("//*[@id=\"artListDiv\"]/div[1]/a[1]")).click();
        sleep(3000);
//        4.获取当前的页面看是不是我要进入的修改页面
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://localhost:8080/html/blog_edit.html?aid=34",cur_url);
//        5.获取此时输入框中的标题看是不是在列表页中第一篇的博客标题
//        String cur_title = webDriver.findElement(By.cssSelector("#title")).getText();
//        sleep(3000);
//        System.out.println(cur_title);
//        Assertions.assertEquals(first_title,cur_title);
//        通过js操作修改文章标题
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value = \"测试修改博客1\";");
        sleep(3000);
//        点击修改按钮
        webDriver.findElement(By.cssSelector("body > div > div.context > div.head > button")).click();
        sleep(3000);
//        看修改后跳转的地址是不是我的个人博客列表页
        String update_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://localhost:8080/html/blog_list.html",update_url);
        String update_title = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.zong_title > h2")).getText();
//        看修改的和博客第一篇是不是一样的
        Assertions.assertEquals("测试修改博客1",update_title);
    }

在这里插入图片描述

(9)首页及分页

首页能够显示所有用户的文章,包括文章标题,发布日期,简介等,同时每篇文章都有一个能够查看全文的按钮,点击按钮后能够进入到文章详情页。底部的话会有一个分页的按钮,点击这些按钮能够进行对应按钮操作。
在这里插入图片描述

首页

由于在首页显示这块,通过文章标题就可以把简介全部带过来显示,加之查看全文按钮在上述已经测试通过了,点击它会跳转到文章详情页面,所以这里以显示文章标题数量统计进行测试。

   @Test
    void BlogIndex() throws InterruptedException {
//        1.进入首页
        webDriver.get("http://localhost:8080/html/blog_index.html");
//        2.获取页面上所有博客标题对应的元素
        int title_num = webDriver.findElements(By.cssSelector("#articleDiv > div:nth-child(1) > div.zong_title > h2")).size();
//        如果元素数量不为0,而且也能显示出来,测试通过
        if(title_num != 0) {
//            说明有点击查看全文按钮
            webDriver.findElement(By.cssSelector("#articleDiv > div:nth-child(1) > a")).click();
            sleep(3000);
//            获取进入后的页面看是不是文章详情页
            String cur_url = webDriver.getCurrentUrl();
            Assertions.assertEquals("http://localhost:8080/html/blog_detail.html",cur_url);
            System.out.println("测试通过!");
        }else {
            System.out.println("测试失败!");
        }
    }

因为我对页面进行了拦截,所以还是需要先登录进入我的个人博客页面,然后通过点击导航栏的首页,即可跳进首页页面进行测试。点击进入查看全文一样的是以第一篇博客为例测试。

    @Test
    void BlogIndex() throws InterruptedException {
        LoginSuccess();
//        获取个人博客列表页的首页按钮
        webDriver.findElement(By.cssSelector("body > div > div.nav > div.right > ul > li:nth-child(1) > a")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//        1.进入首页
//        webDriver.get("http://localhost:8080/html/blog_index.html");
//        2.获取页面上所有博客标题对应的元素
        int title_num = webDriver.findElements(By.cssSelector("#articleDiv > div:nth-child(1) > div.zong_title > h2")).size();
//        如果元素数量不为0,而且也能显示出来,测试通过
        if(title_num != 0) {
//            说明有点击查看全文按钮
            webDriver.findElement(By.cssSelector("#articleDiv > div:nth-child(1) > a")).click();
            sleep(3000);
//            获取进入后的页面看是不是文章详情页(以第一篇博客)点击进入
            String cur_url = webDriver.getCurrentUrl();
            Assertions.assertEquals("http://localhost:8080/html/blog_detail.html?aid=34",cur_url);
            System.out.println("测试通过!");
        }else {
            System.out.println("测试失败!");
        }
    }

在这里插入图片描述

分页按钮点击测试

续上述步骤操作,我们可以去分别点击分页的几个按钮,然后通过url地址可以匹配页码是否正确。
因为对于这几个按钮都会有可能触发弹窗提示(当前已是首页,或者当前已是末页)所以我们要先去处理一下弹窗的,我这里使用的是当我们点击了每一个按钮后,都给一定的等待时间,使用WebDriverWait wait = new WebDriverWait(webDriver, 5);去等待5秒看弹窗是否会出现,然后呢通过try-catch抛出异常,如果等待后没有弹窗出现的异常捕获(TimeoutException),那么就会进入catch后面的操作。

//        (1)首页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(1)")).click();

        //        尝试等待弹窗出现
//        因为这里涉及到每个按钮都有可能触发弹窗,并且触发后的操作也是符合预期的所以这里我们要判断是否会有弹窗的出现
        WebDriverWait wait = new WebDriverWait(webDriver,5);  //设置最长等待时间为10秒
点击首页

如果当前页码就是第一页,那么就会显示弹窗提示当前已是首页,否则就会跳转到首页显示

    @Test
    void PageIndex() throws InterruptedException {
//        1.进入首页,通过登录后进入博客列表页,点击导航栏的首页进入
        LoginSuccess();
//        获取个人博客列表页的首页按钮
        webDriver.findElement(By.cssSelector("body > div > div.nav > div.right > ul > li:nth-child(1) > a")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

//        分别点击分页的按钮
//        (1)首页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(1)")).click();

        //        尝试等待弹窗出现
//        因为这里涉及到每个按钮都有可能触发弹窗,并且触发后的操作也是符合预期的所以这里我们要判断是否会有弹窗的出现
        WebDriverWait wait = new WebDriverWait(webDriver,5);  //设置最长等待时间为10秒

        try {
            wait.until(ExpectedConditions.alertIsPresent());
//        弹窗出现了
//        处理弹窗问题
            Alert alert = webDriver.switchTo().alert();
//        获取弹窗文本内容
            String alertText = alert.getText();
//        验证是否包含"当前已是第一页!"内容
            if(alertText.contains("当前已是第一页!")) {
                alert.accept();
                System.out.println("测试通过");
            }else {
                System.out.println("测试不通过");
            }

        }catch (org.openqa.selenium.TimeoutException e) {
//            如果弹窗没有出现,对于首页按钮,那么就会跳转到首页(从其他页跑到首页)
//            如果就是首页
            System.out.println("当前就是第一页,没有显示弹窗:测试不通过");
//            如果当前是第二页
//            String cur_url = webDriver.getCurrentUrl();
//            Assertions.assertEquals("http://localhost:8080/html/blog_index.html?pindex=2",cur_url);
        }

    }

如果当前就是首页,那么就会出现弹窗提示,如果没有那么就是测试不通过。
在这里插入图片描述
在这里插入图片描述
如果当前在第二页或者其它页面(首页)那么就不会显示弹窗,就继续后续的操作,也就是页面地址的pindex的值是2,然后我们去获取此时的地址,看是不是和预期的一样即可。
我们进来时候因为默认是第一页,所以我们点击下一页让此时是第二页(其它页),接着就点击首页,看不是首页的时候,点击首页进行跳转的情况。
在这里插入图片描述

在这里插入图片描述

测试可以看到是已经通过的。
在这里插入图片描述

以下其他三个按钮都是一样的,都是要现去判断是否会出现弹窗的情况,然后再分别对抛出异常后的,以及有弹窗的情况进行后续操作。

点击末页

如果当前页码就是最后一页,那么就会显示弹窗提示当前已是末页,否则就会跳转到末页显示
因为页面默认是第一页,所以进来之后我们要点到最后一页,看能否顺利到最后一页。

    @Test
    void PageLast() throws InterruptedException {
        //        1.进入首页,通过登录后进入博客列表页,点击导航栏的首页进入
        LoginSuccess();
//        获取个人博客列表页的首页按钮
        webDriver.findElement(By.cssSelector("body > div > div.nav > div.right > ul > li:nth-child(1) > a")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

//        点击末页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(4)")).click();
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);
        String last_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://localhost:8080/html/blog_index.html?pindex=10",last_url);

测试是通过的.
在这里插入图片描述

那如果是本身就是最后一页,那么这里就要测试弹窗出现的情况.进入博客列表页后,去往最后一页,然后等待3秒再次点击最后一页,此时就会有弹窗提示最后一页,我们也是利用上面的方法去等待弹窗出现,然后处理对应的操作.

//    点击末页
    @Test
    void PageLast() throws InterruptedException {
        //        1.进入首页,通过登录后进入博客列表页,点击导航栏的首页进入
        LoginSuccess();
//        获取个人博客列表页的首页按钮
        webDriver.findElement(By.cssSelector("body > div > div.nav > div.right > ul > li:nth-child(1) > a")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

//        点击末页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(4)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);
//        String last_url = webDriver.getCurrentUrl();
//        Assertions.assertEquals("http://localhost:8080/html/blog_index.html?pindex=10",last_url);

//        再次点击末页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(4)")).click();

        WebDriverWait wait = new WebDriverWait(webDriver,5);  //设置最长等待时间为10秒

        try {
            wait.until(ExpectedConditions.alertIsPresent());
//        弹窗出现了
//        处理弹窗问题
            Alert alert = webDriver.switchTo().alert();
//        获取弹窗文本内容
            String alertText = alert.getText();
//        验证是否包含"当前已是第一页!"内容
            if(alertText.contains("当前已是最后一页!")) {
                alert.accept();
                System.out.println("测试通过");
            }else {
                System.out.println("测试不通过");
            }

        }catch (org.openqa.selenium.TimeoutException e) {
//            如果弹窗没有出现,对于末页按钮,那么就会跳转到末页(从其他页跑到末页)
//            如果就是末页
            System.out.println("当前就是最后一页,没有显示弹窗:测试不通过");
//            如果当前是其它页(首页)
//            String cur_url = webDriver.getCurrentUrl();
//            Assertions.assertEquals("http://localhost:8080/html/blog_index.html?pindex=10",cur_url);
        }

    }

测试是通过的.
在这里插入图片描述

点击下一页

会先判断下一页还有没有,没有就直接处理弹窗提示末页显示即可,如果还有那么就跳转到下一页
对于下一页还有的情况,那么就会进入到下一页(没有弹窗的情况),和上面代码类似的,就是我们一开始进入首页列表,点击的是下一页按钮

//        点击下一页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(3)")).click();

处理没有弹窗的情况
在这里插入图片描述
对于下一页没有的情况,那么就还是当前页(有弹窗的情况),再点击下一页之前点击末页

       sleep(3000);
//        点击末页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(4)")).click();
        sleep(3000);
//        点击下一页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(3)")).click();

在这里插入图片描述

点击上一页:

会先判断上一页还有没有,没有就直接处理弹窗提示首页显示即可,如果还有那么就跳转到上一页
测试代码也是和上面也是一样的,通过去看有没有弹窗情况去测试即可。
对于上一页的情况,如果当前就是首页,那么点击上一页的话就还是显示当前页,同时给出弹窗提示(有弹窗情况)
就是进入首页列表页后,然后直接点击首页,就会提示弹窗了,然后看有没有正确显示。

//        点击首页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(1)")).click();
        sleep(3000);

在这里插入图片描述

如果是对于末页或者其它页,那么点击上一页,就能够进入上一页的页面(没有弹窗情况),这里以最后一页为例.进入到首页列表就点击末页,然后再点击上一页,是可以跳过去的,看是不是预期的上一页即可。

//    上一页
    @Test
    void PageBefore() throws InterruptedException {
        //        1.进入首页,通过登录后进入博客列表页,点击导航栏的首页进入
        LoginSuccess();
//        获取个人博客列表页的首页按钮
        webDriver.findElement(By.cssSelector("body > div > div.nav > div.right > ul > li:nth-child(1) > a")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);
        点击首页
//        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(1)")).click();

        //        点击末页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(4)")).click();
        sleep(3000);

//        点击上一页
//        点击首页
        webDriver.findElement(By.cssSelector("body > div > div.content > div > div.page > button:nth-child(2)")).click();
        WebDriverWait wait = new WebDriverWait(webDriver,5);  //设置最长等待时间为10秒

        try {
            wait.until(ExpectedConditions.alertIsPresent());
//        弹窗出现了
//        处理弹窗问题
            Alert alert = webDriver.switchTo().alert();
//        获取弹窗文本内容
            String alertText = alert.getText();
//        验证是否包含"当前已是第一页!"内容
            if(alertText.contains("当前已是第一页!")) {
                alert.accept();
                System.out.println("测试通过");
            }else {
                System.out.println("测试不通过");
            }

        }catch (org.openqa.selenium.TimeoutException e) {

//            如果当前是其末页(10),点击上一页的话是没有提示弹窗
            String cur_url = webDriver.getCurrentUrl();
            Assertions.assertEquals("http://localhost:8080/html/blog_index.html?pindex=9",cur_url);
            System.out.println("当前是末页,点击上一页是没有弹窗的:测试通过!");
        }
    }

在这里插入图片描述

(10)退出登录

退出的话也是以用户登录进入到个人博客列表后,导航栏中的注销按钮为例测试,当我们点击了注销按钮,那么提示是否确认注销,确认的话就会跳转到登陆页面,取消的话就还是没有任何操作。
在这里插入图片描述

//    注销
    @Test
    void Logout() throws InterruptedException {
        LoginSuccess();
//        获取个人博客列表页的注销按钮
        webDriver.findElement(By.cssSelector("body > div > div.nav > div.right > ul > li:nth-child(3) > a")).click();
//        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);
//        弹窗处理
        Alert alert = webDriver.switchTo().alert();
        String alertText = alert.getText();

        if(alertText.contains("是否确认注销?")) {
            boolean btn = true;  //true表示确认注销
            if(btn) {
                alert.accept();
//                回到登录页
//        获取当前页面看是不是登录页
                String cur_url = webDriver.getCurrentUrl();
                Assertions.assertEquals("http://localhost:8080/html/blog_login.html",cur_url);
                // 校验提交按钮
                WebElement webElement = webDriver.findElement(By.cssSelector("#submit1"));
                Assertions.assertNotNull(webElement);
                System.out.println("测试通过");
            }else {
                System.out.println("取消注销,不做任何操作:测试通过");
            }
        }else {
            System.out.println("没有触发弹窗确认:测试不通过!");
        }


    }

在这里插入图片描述
以上就是我对自己做的这个博客系统的简单的功能测试,通过这些测试可以看到写的这个项目还是有很多的不足,比如在用户登录进入系统后,没有地方法显示用户的信息等,这个项目还是需要继续优化升级。
测试代码源码:测试源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小哈不会玩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值