分享几个自己封装的一些断言!
觉得有帮助的同学可以点个赞!传递给更多人!
前置条件是已经导入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);
}
以上断言基本包含常用的场景,也可以根据思路魔改适合自己需求的断言.