来源:自学PHP网 时间:2020-09-27 14:57 作者:小飞侠 阅读:次
[导读] Python selenium实现断言3种方法解析...
今天带来Python selenium实现断言3种方法解析教程详解
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)
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() 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学php网。 以上就是关于Python selenium实现断言3种方法解析全部内容,感谢大家支持自学php网。 |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com