自学内容网 自学内容网

博客项目自动化测试(二)

1. 断言

1.1 断言不相等

    @Test
    void Test07() {
        int x = 10;
        int y = 20;
        Assertions.assertEquals(x, y);

        测试结果如下:

1.2 断言不相等

 @Test
    void Test07() {
        int x = 10;
        int y = 20;
//        Assertions.assertEquals(x, y);
        Assertions.assertNotEquals(x,y);

1.3 断言为空

    @Test
    void Test07() {
        int x = 10;
        int y = 20;
        String temp = null;
        Assertions.assertNull(temp);
}

1.4 断言不为空

 @Test
    void Test07() {
        int x = 10;
        int y = 20;
        String temp = null;
        Assertions.assertNotNull(temp);
    }

结果如下:

        接下来是项目的自动化测试的测试;

2. 发布博客

  步骤一:首先进入到博客列表页

步骤二:进入到写博客页面

步骤三:在该页面的输入框输入博客名字,点击发布;

步骤四:返回到博客列表页,根据当前博客的标题是否为空判断是否博客发布成功;

代码如下:

@Test
    void PublishBlog() throws InterruptedException {
        // 打开博客列表页
        webDriver.get("http://49.235.129.183:8080/java109_blog_system/blog_list.html");
        // 点击写博客按钮  body > div.nav > a:nth-child(5)
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        sleep(3000);
        // document.querySelector("#title").value="测试"
        // 输入标题(通过Selenium执行JS脚本完成)
        ((JavascriptExecutor)webDriver).executeScript("document.querySelector(\"#title-input\").value=\"黑喵\"");
        sleep(2000);
        // 点击发布博客按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        // 校验
        // 1、找到发布成功的博客标题  body > div.container > div.container-right > div:nth-child(1) > div.title
        WebElement title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title"));
        sleep(2000);
        // 2、校验标题对应的元素是否为空,如果不为空,测试通过,如果为空,测试不通过
        Assertions.assertNotNull(title);
    }

测试结果如下:

        删除博客功能由于没有这个功能就暂时不测试了; 

        当一个页面在进行验证删除的功能是,页面没有元素

 3. 退出系统

 @Order(3)
    @Test
    void LogOut() throws InterruptedException {
        webDriver.get("http://49.235.129.183:8080/java109_blog_system/blog_list.html");
        //body > div.nav > a:nth-child(6)
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        sleep(2000);
        String url = "http://49.235.129.183:8080/java109_blog_system/login.html";
        sleep(3000);
        //body > div.login-container > div > form > div:nth-child(1) > span
        String username = webDriver.findElement(By.cssSelector("body > div.login-container > div > form > div:nth-child(1) > span")).getText();
        Assertions.assertEquals("http://49.235.129.183:8080/java109_blog_system/login.html", url);
        Assertions.assertEquals("用户名", username);
    }

        以包为整体进行测试;

@Suite
//通过class运行测试用例
@SelectClasses({JunitTest1.class,JunitTest.class})
//通过包运行测试用例
@SelectPackages(value = {"BlogAutoTests"})
public class RunTest {
}

测试结果如下所示:

ps:本文就写到这里了,谢谢观看。


原文地址:https://blog.csdn.net/2202_76101487/article/details/142851226

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!