学习笔记-断言

2019-12-10  本文已影响0人  sofiiii

1. 元素是否存在

find_elements

2. 元素属性是否正确

get_attribute
assert 'xxxx' in element.get_attribute('resource-id')

3. 查找元素文本

assert float(find_element_by_id('xxxx).text)>20

4. hamcrest断言

安装
pip install PyHamcrest
String str1 = "text";
String str2 = " text ";
assertThat(str1, is(equalToIgnoringWhiteSpace(str2)));
String str1 = "text";
String str2 = "text";
assertThat(str1, is(str2));
String str1 = "text";
String str2 = " text ";
assertThat(str1, not(equalToIgnoringWhiteSpace(str2)));

String str1 = "text";
String str2 = "text";
assertThat(str1, not(str2));
String str1 = "text123";
String str2 = "text";
assertThat(str1, containsString(str2));
String str1 = "text123";
assertThat(str1, startsWith("text"));// 以某个字符开头
assertThat(str1, endsWith("text"));// 以某个字符开头
Cat cat=new Cat();
assertThat(cat, sameInstance(cat));
String str = "calligraphy";
String start = "call";
String end = "foo";
assertThat(str, anyOf(startsWith(start), containsString(end)));
String str = "calligraphy";
String start = "call";
String end = "phy";
assertThat(str, allOf(startsWith(start), endsWith(end)));
assertThat(1, greaterThan(0)); // 大于
assertThat(5, greaterThanOrEqualTo(5)); //大于等于
assertThat(-1, lessThan(0)); // 小于
assertThat(-1, lessThanOrEqualTo(5)); // 小于等于
String str = "";
assertThat(str, isEmptyString()); // 空字符串
assertThat(str, isEmptyOrNullString()); // 空字符串或者null
String str1 = "text";
String str2 = " text ";
assertThat(str1, equalToIgnoringWhiteSpace(str2)); // 忽略左右空白
assertThat(a, equalToIgnoringCase(b)); //忽略大小写
List<String> collection = Lists.newArrayList("ab","cd","ef");
assertThat(collection, hasItem("cd"));
assertThat(collection, not(hasItem("zz")));
assertThat(collection, hasItems("cd", "ab")); // 检查多个元素是否在集合中
List<String> collection = Lists.newArrayList("ab","cd","ef");
assertThat(collection, hasItems("ab","cd","ef"));
assertThat(collection, hasItems("cd", "ab","ef")); // 错误
assertThat(collection, hasItems("ab","cd")); // 错误
assertThat(collection, containsInAnyOrder("cd", "ab","ef")); //正确,不区分顺序
List<String> collection = Lists.newArrayList("ab","cd","ef");
assertThat(collection, empty()); // false, 用于检查集合类型
String[] array = new String[] { "ab" };
assertThat(array, emptyArray()); // false

5. 总结

核心:
    anything - 总是匹配,如果你不关心测试下的对象是什么是有用的
    describedAs - 添加一个定制的失败表述装饰器
    is - 改进可读性装饰器 - 见下 “Sugar”
逻辑:
    allOf - 如果所有匹配器都匹配才匹配,像Java里的&&
    anyOf - 如果任何匹配器匹配就匹配,像Java里的||
    not - 如果包装的匹配器不匹配器时匹配,反之亦然
对象:
    equalTo - 测试对象相等使用Object.equals方法
    hasToString - 测试Object.toString方法
    instanceOf, isCompatibleType - 测试类型
    notNullValue, nullValue - 测试null
    sameInstance - 测试对象实例
Beans:
    hasProperty - 测试JavaBeans属性
集合:
    array - 测试一个数组元素test an array’s elements against an array of matchers
    hasEntry, hasKey, hasValue - 测试一个Map包含一个实体,键或者值
    hasItem, hasItems - 测试一个集合包含一个元素
    hasItemInArray - 测试一个数组包含一个元素
数字:
    closeTo - 测试浮点值接近给定的值
    greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo - 测试次序
文本:
    equalToIgnoringCase - 测试字符串相等忽略大小写
    equalToIgnoringWhiteSpace - 测试字符串忽略空白
    containsString, endsWith, startsWith - 测试字符串匹配
上一篇下一篇

猜你喜欢

热点阅读