Java版UI自动化测试的断言方法/selenium Testng方法封装断言

6 篇文章 0 订阅
3 篇文章 0 订阅

分享几个自己封装的一些断言!
觉得有帮助的同学可以点个赞!传递给更多人!


前置条件是已经导入TestNg的包了

场景一:判断真与假

 /**
     * 断言预期的结果的真假
     *
     * @param bool
     */
    protected void assertTrue(boolean bool) {
        try {
            Assert.assertTrue(bool);
            logger.info("断言结果:" + bool + " 验证成功");
        } catch (Exception e) {
            logger.info("断言结果:" + bool + " 验证失败");
            throw e;
        }
    }
    //这里重新封装下 Assert.assertTrue();增加日志顺便方便后面使用.

---- — ------------------ ----------------------- ----------------------- -------- --------------

/**
     * 断言预期的结果的真假(取反值)
     *
     * @param bool
     */
    protected void assertFalse(boolean bool) {
        try {
            Assert.assertFalse(bool);
            logger.info("断言结果:" + bool + " 验证成功");
        } catch (Exception e) {
            logger.info("断言结果:" + bool + " 验证失败");
            throw e;
        }
    }

---- — ------------------ ----------------------- ----------------------- -------- --------------

/**
     * 判断实际值与预期值是否一致
     *
     * @param actual 实际值
     * @param expect 预期值
     */
    protected void assertEquals(Object actual, Object expect) {
        try {
            Assert.assertEquals(actual, expect);
            logger.info("断言实际值:" + actual + ";预期值:" + expect + "  验证成功");
        } catch (AssertionError e) {
            logger.error("断言实际值:" + actual + "   " + "预期值:" + expect + "  验证失败,报错:" + e);
            throw e;
        }
    }

---- — ------------------ ----------------------- ----------------------- -------- --------------

protected void assertNotEquals(Object actual, Object expect) {
        try {
            assertFalse(actual.equals(expect));
            logger.info("断言实际值:" + actual + ";不为,预期值:" + expect + "  验证成功");
        } catch (AssertionError e) {
            logger.error("断言实际值:" + actual + "   " + "预期值:" + expect + "  验证失败,报错:" + e);
            throw e;
        }
    }

---- — ------------------ ----------------------- ----------------------- -------- --------------

/**
     * 断言实际值是否包含预期值
     *
     * @param actual
     * @param expect
     */
    protected void assertContains(String actual, String expect) {
        try {
            Assert.assertTrue(actual.contains(expect));
            logger.info("断言实际值:" + actual + "   " + ";是否包含预期值:" + expect + ";验证成功");
        } catch (AssertionError e) {
            logger.error("断言实际值:" + actual + "   " + ";是否包含预期值:" + expect + ";验证失败,报错:" + e);
            throw e;
        }
    }

---- — ------------------ ----------------------- ----------------------- -------- --------------

/**
 * 断言实际值是否不包含预期值
 *
 * @param actual
 * @param expect
 */
protected void assertNotContains(String actual, String expect) {
    try {
        Assert.assertFalse(actual.contains(expect));
        logger.info("断言实际值:" + actual + "   " + ";是否不包含预期值:" + expect + "  验证成功");
    } catch (AssertionError e) {
        logger.error("断言实际值:" + actual + "   " + ";是否不包含预期值:" + expect + "  验证失败,报错:" + e);
        throw e;
    }
}

场景二:判断元素的文本内容存在/包含预期内容

/**
     * 判断可见的(且具有实际的可视面积)文本元素的文本(getText))是否等于预期文本
     *
     * @param by
     * @param expect
     */
    protected void assertElementTextEquals(By by, String expect) {
        String actual = getElementText(by);//这里获取元素的文本重新封装了
        assertEquals(actual, expect);
    }

---- — ------------------ ----------------------- ----------------------- -------- --------------

 protected void assertElementTextContains(By by, String expect) {
        String actual = getElementText(by);
        assertContains(actual, expect);
    }

---- — ------------------ ----------------------- ----------------------- -------- --------------
配合真假断言可以断言非空

/**
 * @param param 对象
 * @return 是否不为空
 */
protected static boolean notEmpty(Object param) {
    return null != param && !"".equals(param) && !"null".equals(param);
}

---- — ------------------ ----------------------- ----------------------- -------- --------------

protected void assertNotEquals(Object actual, Object expect) {
        try {
            assertFalse(actual.equals(expect));
            logger.info("断言实际值:" + actual + ";不为,预期值:" + expect + "  验证成功");
        } catch (AssertionError e) {
            logger.error("断言实际值:" + actual + "   " + "预期值:" + expect + "  验证失败,报错:" + e);
            throw e;
        }
    }

场景三:判断预期的元素是否可见

 protected void assertElementVisible(By by, int timeout) {
        logger.info("断言元素:" + by.toString() + ";是否可见;" + "超时时间:" + timeout);
        assertTrue(waitElementIsVisible(by, timeout));
    }
protected void assertElementNotVisible(By by, int timeout) {
        try {
            logger.info("断言元素:" + by.toString() + ";是否不可见;" + "超时时间:" + timeout + "秒.");
            assertFalse(waitElementIsVisible(by, timeout));
        } catch (AssertionError e) {
            //重写断言异常,方便查看.在报告中也能直观体现
            throw new AssertionError("元素:" + by.toString() + ";可见!");
        }
    }

前置条件是已经导入selenium的包了

protected WebElement getVisibleElement(By by, long timeout) {
        WebDriverWait wait = new WebDriverWait(getThreadLocalDriver(), timeout);
        try {
            return wait.until(ExpectedConditions.visibilityOfElementLocated(by));
        } catch (TimeoutException e) {
            throw new TimeoutException("元素:" + by.toString() + ";智能等待可点击的元素超时报错:" + e.getMessage());
        }

    }

/**
     * @param by
     * @param timeout
     * @return 智能等待元素可见并返回结果, 亦可作智能等待用
     */
    protected Boolean waitElementIsVisible(By by, int timeout) {
        //指定元素可见则返回真
        try {
            getVisibleElement(by, timeout);
            logger.info("断言元素" + by + "是否可见:可见");
            return true;
        } catch (Exception e) {
            logger.error("断言元素" + by + "是否可见:不可见,报错:" + e.getMessage());
            return false;
        }
    }

场景四:判断预期的文件(导出的文件)是否已存在

/**
*每间隔2s扫描一次,直到超时时间结束没有文件就抛出断言异常
*/
protected void assertFileWhetherExisted(String file_path, int timeout) {
        int times;
        int pauseTime = 2000;
        if (timeout % 2 != 0 || timeout < 2) {
            times = (timeout + 1) * 1000 / pauseTime;
        } else {
            times = timeout * 1000 / pauseTime;
        }
        for (int i = 0; i < times; i++) {
            sleep(pauseTime);
            try {
                assertTrue(fileWhetherExisted(file_path));
                logger.info("文件:" + file_path + " 是否存在:" + fileWhetherExisted(file_path));
                logger.info("文件:" + file_path + " 的最后修改时间为" + getFileLastModifiedTime(file_path));
                break;
            } catch (AssertionError e) {
                logger.info("文档验证次数:" + i + ";等待时间:" + (i + 1) * pauseTime / 1000 + "秒");
                if (i == times - 1) {
                    throw e;
                }
            }

        }

    }
 
 //或者
     protected void assertOutputFileWhetherExisted(String file_path, int timeout) {
        Clock clock = Clock.systemDefaultZone();
        Instant end = clock.instant().plusSeconds(timeout);
        int poll = 5; //轮询间隔/秒
        while (true) {
            sleep(poll * 1000);
            try {
                assertTrue(fileIfExist(file_path));
                logger.info("文件:" + file_path + " 是否存在:" + fileIfExist(file_path) + ";等待时间:" + (poll++) + "秒");
                logger.info("文件:" + file_path + " 的最后修改时间为" + getFileLastModifiedTime(file_path));
                break;
            } catch (AssertionError e) {
                if (end.isBefore(clock.instant())) {
                    throw new AssertionError("等待" + timeout + "秒后仍未发现预期导出文件:" + file_path + "," + e);
                }
            }

        }
    }

/**
     * @param file_path
     * @return 文件是否存在
     */
    public static Boolean fileWhetherExisted(String file_path) {
        File file = new File(file_path);
        return file.exists();
    }

    protected void sleep(int timeout) {
        try {
            Thread.sleep(timeout);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

场景五:判断预期的文本有多个(模糊断言)

    /**
     * 模糊断言,若匹配到其中一个断言即通过否则返回最后一个不满足断言的信息
     * @param actual
     * @param expects
     */
    protected void assertEquals(String actual, String[] expects) {
        String expect = null;
        for (String text : expects
        ) {
            expect = text;
            if (actual.equals(text)) {
                break;
            }
        }
        assertEquals(actual, expect);
    }

以上断言基本包含常用的场景,也可以根据思路魔改适合自己需求的断言.

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
Java Selenium TestNG中,可以封装断言方法来验证测试结果。根据提供的引用内容,可以看到有三个封装断言方法。 第一个引用\[1\]中的方法是assertElementTextContains,它用于验证指定元素的文本是否包含预期的内容。该方法首先获取元素的文本,然后使用assertContains方法进行断言。 第二个引用\[2\]中的方法是assertNotEquals,它用于验证两个对象是否不相等。该方法首先使用assertFalse方法判断实际值和预期值是否相等,如果相等则抛出AssertionError异常。 第三个引用\[2\]中的方法是assertElementVisible和assertElementNotVisible,它们分别用于验证元素是否可见和不可见。这两个方法使用waitElementIsVisible方法等待元素的可见性,并使用assertTrue和assertFalse方法进行断言。 因此,可以根据需要选择合适的断言方法来验证测试结果。 #### 引用[.reference_title] - *1* *2* [JavaUI自动化测试断言方法/selenium Testng方法封装断言](https://blog.csdn.net/Franciz777/article/details/114063739)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [JAVA+selenium+testng 断言封装及调用](https://blog.csdn.net/weixin_44242153/article/details/117709979)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Franciz小测测

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

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

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

打赏作者

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

抵扣说明:

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

余额充值