[5]python+selenium - UI自动框架之重写unittest.TestCase

重写unittest.TestCase 不仅继承了unittest的方法,还丰富不同断言方法,用起来更方便、简单。

import unittest
from common.BSBase import BasePage
from common.log import log
from common.browserDriver import getdriver
from common.publicFunc import *class BSTestCase(unittest.TestCase, BasePage):@classmethoddef setUpClass(cls):"""get driver"""log.info('[BS]_002:setUpClass')cls.driver = ]getdriver()if cls.driver is None:cls.driver = getdriver()def setUp(self):log.info('[BS]_002:setUp')@classmethoddef tearDownClass(cls):log.info('[BS]_002:tearDownClass')PublicDriver().setDriver(cls.driver)def __call__(self, *args, **kwargs):log.info('[BS]_001:To be run[%s]' % str(self._testMethodName))super().__call__(*args, **kwargs)def assertTrue(self, expr, msg=None):try:super().assertTrue(expr, msg)log.warning('[BS]_200:pass')except AssertionError:log.warning('[BS]_555:false is not true %s' % msg)raise self.failureException(msg)def assertEqual(self, first, second, msg=None):try:super().assertEqual(first, second, msg)log.warning('[BS]_200:pass')except AttributeError:log.warning('[BS]_555:%s is not equal %s' % (first, second))raise self.failureException(msg)def assertNotEqual(self, first, second, msg=None):try:super().assertNotEqual(first, second, msg)log.warning('[BS]_200:pass')except AttributeError:log.warning('[BS]_555:%s is equal %s' % (first, second))raise self.failureException(msg)def assertIn(self, member, container, msg=None, timeout=5):try:super().assertIn(member, container, msg)log.warning('[BS]_200:pass')except AssertionError:log.warning('[BS]_555: %s not in %s' % (member, str(container)))raise self.failureException(msg)def assertNotIn(self, member, container, msg=None, timeout=5):try:super().assertNotIn(member, container, msg)log.warning('[BS]_200:pass')except AssertionError:log.warning('[BS]_555: %s in %s' % (member, str(container)))raise self.failureException(msg)def assertIsNotNone(self, obj, msg=None):try:super().assertIsNotNone(object, msg)log.warning('[BS]_200:pass')except AssertionError:log.warning('[BS]_555: obj{obj} is none'.format(obj=obj))raise self.failureException(msg)def assertSelected(self, locator):result = self.is_element_located_selected(locator)self.assertTrue(result)def assertElementVisible(self, locator, msg=None):"""failed if element is not visible"""element = self.find_element_visible(locator)self.assertIsNotNone(element, msg)def assertElementNotVisible(self, locator, msg=None):"""failed if element is not visible"""result = self.find_element_not_visible(locator, 10, 0.1)self.assertTrue(result, msg)def assert_Element_Text_Equal(self, locator, expect_text, timeout=8):"""failed if 元素中的文字 != expect_text"""# 有问题!只要locator中包含了expect_text,就会判断通过,_, current_text = self.wait_until_element_located_contains_text(locator, expect_text, timeout)self.assertEqual(expect_text, current_text)def assert_Element_Text_Contained(self, locator, expect_text, msg=None, timeout=6, interval=0.5):"""获取元素的text,比较预期值是否在实际值里"""actual_text = self.get_element_text(locator, timeout=timeout, interval=interval)actual_text = '' if actual_text is None else actual_textprint("actual_text: " + actual_text)self.assertIn(expect_text, actual_text, msg=msg)def assert_Element_Attribute_Value_Contained(self, locator, attribute, expect_text, msg=None, timeout=8,interval=0.5):'''获取元素的Attribute的value,比较实际值和预期值是否相等:param locator: 元素:param attribute: 元素要获取的属性名:param expect_text: 预期值:param timeout::param msg:'''actual_text = self.get_element_Attribute_value(locator, attribute, timeout=timeout, interval=interval)self.assertIn(expect_text, actual_text, msg=msg)def assert_Element_Text_Not_Contained(self, locator, expect_text, timeout=8):"""failed if 元素中的文字 != expect_text"""_, current_text = self.wait_until_element_located_not_contains_text(locator, expect_text, timeout)self.assertNotEqual(expect_text, current_text)def assert_Text_In_Element(self, locator, expect_text, timeout=8):"""failed if 元素中的文字 != expect_text"""_, current_text = self.wait_until_element_located_contains_text(locator, expect_text, timeout)self.assertIn(expect_text, current_text)def assert_Text_Not_In_Element(self, locator, text, timeout=8):"""failed if 元素中的文字 != expect_text"""_, current_text = self.wait_until_element_located_not_contains_text(locator, text, timeout)self.assertNotIn(text, current_text)def assert_Selected_Option_Text_Equal(self, locator, text):"""failed if Selected元素中已选中的文字 != expect_text"""selected_text = self.get_selected_item_text(locator)self.assertEqual(text, selected_text)def assert_If_Checkbox_Selected(self, locator, msg=None):"""checkbox 是否被选中"""isSelected = self.checkbox_is_selected(locator)self.assertTrue(isSelected, msg)def assert_Text_Not_In_Selected(self, locator, text):"""failed if text in selected"""all_options_text = self.get_all_select_item(locator)self.assertNotIn(text, all_options_text)def assert_Text_In_Selected(self, locator, text):"""failed if text not in selected"""all_options_text = self.get_all_select_item(locator)self.assertIn(text, all_options_text)def assert_page_contains(self, text):page_source = self.get_page_source()self.assertIn(text, page_source)def assert_page_not_contains(self, text):page_source = self.get_page_source()self.assertNotIn(text, page_source)def assert_list_value_contained_in_basic_list(self, resultlist, basiclist):""" resultlist 中的所有值都在 basiclist 中"""if type(resultlist) != type(basiclist):log.warning('[BS]_554:type error!,type should be the same,currentType:{type1}-{type2}'.format(type1=type(resultlist), type2=type(basiclist)))return Falseif isinstance(resultlist, list):"""比较对象是字符串"""result = set(resultlist).issubset(basiclist)self.assertTrue(result, msg='not all value in {list1} contained in {list2}'.format(list1=resultlist,list2=basiclist))def assert_value_contained_in_lists(self, text, lists):""":param text:text = a:param lists: resultlist = [(a,b),(a,c,d),(,a)]:return:"""for i in range(len(lists)):for j in range(len(lists[0])):if text.lower() in lists[i][j].lower():breakelif j+1 == len(lists[0]):self.fail("{text} is not the lists: {list}".format(text=text, list=lists[i]))self.assertTrue(True)def assert_value_contained_in_list(self, text, resultlist):""":param text: text = a:param resultlist:   resultlist = [test1,(c,d),testa]:return:"""for i in range(len(resultlist)):if text in resultlist[i]:self.assertTrue(True)breakelif i+1 == len(resultlist):self.fail("{text} is not the list: {list}".format(text=text, list=resultlist))def assert_value_not_contained_in_list(self, text, resultlist):if text not in resultlist:self.assertTrue(True)else:self.fail("{text} is the list: {list}".format(text=text, list=resultlist))if __name__ == "__main__":'''self test'''ccc = BSTestCase('test')a = 'test aa'b=['test aaa', 'test aa']ccc.assertIn(a, b,'pass')

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/38528.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2024.06.30 刷题日记

121. 买卖股票的最佳时机 实例 1: 输入:[7,1,5,3,6,4] 输出:5 解释:在第 2 天(股票价格 1)的时候买入,在第 5 天(股票价格 6)的时候卖出,最大利润 6-1 5…

高考假期预习指南:开启你的IT学习之旅

七月来临,各省高考分数已揭榜完成。而高考的完结并不意味着学习的结束,而是新旅程的开始。对于有志于踏入IT领域的高考少年们,这个假期是开启探索IT世界的绝佳时机。作为一名在计算机行业深耕多年的专家,我愿意为准新生们提供一份…

等级保护测评在测评中Linux系统怎么改

在等级保护测评中,针对Linux系统的整改主要是为了提高其安全性,使之符合等级保护的基本要求。 以下是一些常见的整改步骤和建议: 1. 身份鉴别: • 强化密码策略,例如设置复杂的密码规则、密码长度、密码复杂度、密码…

Web基础与HTTP协议:

Web基础与HTTP协议 Web:就是我们所说的页面,打开网站所展示的页面。(全球广域网,万维网) 分布式图形信息系统。 http https (加密的)超文本传输协议 分布式:计算机系统或者应用程序…

大型游乐设施操作试题

选择题 1、《游乐设施安全技术监察规程(试行)》规定:制造单位应在游乐设施明显部位装设铭牌,铭牌内容至少应包括制造单位名称与地址、设备名称、编号等等技术参数,但下列除哪项外均正确。[单选题]* A、速度和高度 B、生产许可证号 C、制造单位的联系电话…

WLAN Hostapd配置参数详解 - EN

中文的配置参数详解:WLAN Hostapd配置参数详解-CSDN博客 ##### hostapd configuration file ############################################## # Empty lines and lines starting with # are ignored # AP netdevice name (without ap postfix, i.e., wlan0 uses wl…

vue3引入本地静态资源图片

一、单张图片引入 import imgXX from /assets/images/xx.png二、多张图片引入 说明:import.meta.url 是一个 ESM 的原生功能,会暴露当前模块的 URL。将它与原生的 URL 构造器 组合使用 注意:填写自己项目图片存放的路径 /** vite的特殊性…

SQL注入【1】——通用漏洞/SQL注入/mysql跨库/ACCESS偏移

一、知识点: 1、脚本代码与数据库前置知识 2、Access数据库注入-简易&偏移 3、MYSQL数据库注入-简易:权限跨库 二、前置知识: (一)SQL注入漏洞产生原理分析 SQL注入产生条件:根本条件:可控变量、特定函数。 脚本代码在实现…

教育行业的网络安全:保护学生数据与防范网络欺凌

在数字化的春风中,教育行业迎来了知识的繁花似锦,然而,随之而来的网络安全风暴也悄然逼近。学生数据的脆弱性与网络欺凌的阴影交织成一幅复杂的画卷,呼唤着教育工作者与技术专家共同编织一张密不透风的网络安全之网。本文深入探讨…

C++修饰符类型

一、存储类运算符 auto(自动存储类,但在现代C中,它通常用于自动类型推导) register(建议编译器将变量存储在寄存器中,但现代编译器通常忽略此关键字) static(静态存储类&#xff…

【Spring】Spring Security 核心类介绍及Spring Security 的验证机制

Spring Security 核心类介绍及Spring Security 的验证机制 一、Spring Security 核心类1.1 Authentication1.2 SecurityContextHolder1.3 UserDetails1.4 UserDetailsService1.5 GrantedAuthority1.6 DaoAuthenticationProvider1.7 PasswordEncoder 二、 Spring Security 的验证…

国产压缩包工具——JlmPackCore SDK说明(二)——JlmPack_Create函数说明

一、JlmPack_Create函数说明 JlmPack_Create函数是创建jlm压缩文件的核心函数,最大允许CATALOG_MAX_LIMIT(请参考Config.h)个目录,意思是包括文件夹和文件在内,遍历整个列表最大允许CATALOG_MAX_LIMIT个目录对象&#…

《昇思25天学习打卡营第4天|数据集 Dataset》

文章目录 前言:今日所学:1. 数据集加载2. 数据集迭代3. 数据集常用操作与自定义数据集 前言: 今天学习的是数据集的内容。首先,数据是深度学习的基石,高质量的数据输入能够在整个深度神经网络中发挥积极作用。MindSpo…

【UE5.1】Chaos物理系统基础——01 创建可被破坏的物体

目录 步骤 一、通过笔刷创建静态网格体 二、破裂静态网格体 三、“统一” 多层级破裂 四、“簇” 群集化的破裂 五、几何体集的材质 六、防止几何体集自动破碎 步骤 一、通过笔刷创建静态网格体 1. 可以在Quixel Bridge中下载两个纹理,用于表示石块的内外纹…

C++中的类型转换操作符:static_cast reinterpret_cast const_cast dynamic_cast

目录​​​​​​​ C语言中的类型转换 C中的类型转换 C中的类型转换操作符 static_cast reinterpret_cast const_cast volatile关键字 赋值兼容 dynamic_cast C语言中的类型转换 基本概念:赋值运算符左右两侧类型不同,或形参与实参类型不匹配…

51单片机通过控制寄存器控制设备,那么程序中变量的运算职责由谁完成的呢

在51单片机(或更广泛地说,在任何微控制器或微处理器系统中)的程序执行过程中,变量的运算职责主要由中央处理器(CPU)完成。CPU 负责执行程序中的指令,包括对各种变量进行算术和逻辑运算。 当你编…

【Android build异常】androidx导致

现象: 增加 implementation androidx.activity:activity:1.8.0后,导致build失败: 提示SDK Java_Home 1.8 而当前需要11。修改settings-Gradle为11仍不起作用。解决: 删除该行引用。 拓展: 1、compileSdkVersion …

如何在宝塔面板中配置SSL证书?

目录 一、申请证书二、登录宝塔面板配置SSL证书一、申请证书 登录华为云,进入“云证书管理服务 CCM”: 点击“购买证书”: 选择“DV(Basic)”->“DigiCert”,点击【立即购买】购买有效期为3个月的免费证书。 申请证书:

JAVA里的object类

public static String toString(Object o) // 获取对象的字符串表现形式 public static boolean equals(Object a, Object b) // 比较两个对象是否相等 public static boolean isNull(Object obj) // 判断对象是否为null pu…