Python selenium实现断言3种方法解析
作者:多测师_郑sir 时间:2023-06-06 06:08:53
1.if ...else ...判断进行断言
from time import *
from selenium import webdriver
def login(user="admin",pwd="123456"):
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://192.168.1.110:8080/cms")
driver.maximize_window()
sleep(1)
driver.find_element_by_id("userAccount").send_keys(user)
sleep(1)
driver.find_element_by_id("loginPwd").send_keys(pwd)
sleep(1)
driver.find_element_by_id("loginBtn").click() #登录
sleep(1)
users = driver.find_element_by_class_name("c-white").text #获取用户名
sleep(1)
driver.quit()
if users[6:] == user:
pass
else:
raise AssertionError
if __name__ == '__main__':
login()
from time import *
from selenium import webdriver
def login(user="admin",pwd="123456"):
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://192.168.1.110:8080/cms")
driver.maximize_window()
sleep(1)
driver.find_element_by_id("userAccount").send_keys(user)
sleep(1)
driver.find_element_by_id("loginPwd").send_keys(pwd)
sleep(1)
driver.find_element_by_id("loginBtn").click() #登录
sleep(1)
users = driver.find_element_by_class_name("c-white").text #获取用户名
sleep(1)
driver.quit()
if users[6:7] == user:
pass
else:
assert False
if __name__ == '__main__':
login()
2.try ... except ...断言
from time import *
from selenium import webdriver
def fatie(title,body):
dr = login()
sleep(2)
dr.find_element_by_partial_link_text("版块").click()
sleep(1)
dr.find_element_by_id("subject").send_keys(title)
sleep(1)
dr.find_element_by_id("fastpostmessage").send_keys(body)
sleep(2)
dr.find_element_by_name("topicsubmit").click()
sleep(2)
try:
dr.find_element_by_id("thread_subject")
except:
raise AssertionError
else:
pass
sleep(3)
dr.quit()
3.assert断言(基于unittest)
assertIn(arg1, arg2, msg=None) 验证 arg1 是 arg2 的子串,不是则 fail
assertNotIn(arg1, arg2, msg=None) 验证 arg1 不是 arg2 的子串,是则 fail
assertEqual(arg1, arg2, msg=None) 验证 arg1=arg2,不等则 fail
assertNotEqual(arg1, arg2, msg=None) 验证 arg1 != arg2, 相等则 fail
import time
import unittest
class Test(unittest.TestCase):
def test01(self):
self.assertEqual(2,2,msg="test01 error!")
def test02(self):
self.assertNotEqual(1,2,msg="test02 error!")
def test03(self):
self.assertIn("fang","xinfangshuo",msg="test03 error!")
def test04(self):
self.assertNotIn("xing","xinfangshuo",msg="test04 error!")
def tearDown(self):
time.sleep(1)
if __name__ == '__main__':
unittest.main()
来源:https://www.cnblogs.com/ZhengYing0813/p/11779590.html
标签:Python,selenium,断言
0
投稿
猜你喜欢
基于Python的OCR实现示例
2021-03-05 20:58:43
通过实例了解Python str()和repr()的区别
2022-06-01 21:37:36
详解Mysql主从同步配置实战
2024-01-14 16:36:13
pytorch 一行代码查看网络参数总量的实现
2023-04-23 17:42:36
vue之计算属性的缓存computed的用法解读
2024-05-11 09:14:05
python 输出列表元素实例(以空格/逗号为分隔符)
2023-10-03 16:40:52
Python中使用Flask、MongoDB搭建简易图片服务器
2021-12-09 19:57:16
python中对信号的处理详解
2021-05-29 08:37:41
MSSQL数据加密解密代码
2023-07-08 18:45:30
pyecharts结合flask框架的使用
2022-12-01 18:37:25
javascript设计模式交流(一)Singleton Pattern
2007-11-29 13:20:00
python 经典数字滤波实例
2022-08-26 10:23:22
用户如何有效地利用ORACLE数据字典
2008-03-04 18:19:00
golang gorm模型结构体的定义示例
2024-04-23 09:35:24
python切割图片的示例
2021-01-10 22:09:12
python实现三次密码验证的示例
2022-01-24 16:34:25
ASP FCKeditor在线编辑器使用方法
2023-01-12 23:15:04
python3.5 email实现发送邮件功能
2023-06-14 15:58:59
ECMAScript6函数默认参数
2024-05-13 09:18:24
ASP.NET(C#)读取Excel的文件内容
2023-07-10 22:38:35