import unittest from selenium import webdriver from time import sleep import csv from selenium.webdriver.support.select import Select from tools.HTMLTestRunner import HTMLTestRunner class TestLogin(unittest.TestCase): def setUp(self): self.url = "http://127.0.0.1:8888/iwebshop/index.php?controller=systemadmin&action=index" self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(20) self.driver.get(self.url) # 利用cookie 跳过登录 cookie = "iweb_Captcha=2cebbe87a6NjA4MTAwMDYwNjYwYzRjPmU%2BOTVlZzBiYzI2Z2EzOzFpYW5hZA; iweb_admin_id=33a21edc42MjgwMDA0MDQ1NGRlZWY1NzAzYjo%2BM2I1OGNrZGExNjIy; iweb_admin_name=6587ec169fNjAwMDAwMTM4M2RibGM9ZDM8OD82MGU%2FNjY3Mzo0PWVhZGhhbg; iweb_admin_pwd=182e271b86MTA3MDA3NzAwN2M5NjYzMzhtNWY3MjAzY2U0ZDRkNjFlNTNhZGUzMTI5YWY1OWNrYmc1N2U5NDdmMDhjODg0ZA; iweb_admin_role_name=053ddee2c0NzAwMzMzMDc1ODgxPTsxYzgyZWFjbDM%2FZWY3MWQ2M2LotoXnuqfnpqjnl4%2Fil5g; iweb_admin_right=d5ce7aa7d1MjgwNzA2NjQ1MGZmMG1oNzBlZmRtYTszOWA2PTRhZmdhZW9gaWlzfXFhdG9z; iweb_user_id=cb35bfd808MDE4MDAwOTA2OT09OzRhMDM%2FNTo3MDUxZTE%2BMTU1MDA; iweb_user_pwd=f65f9657e8NDAwMDgwNzAzMDk5OTRkMTM3MTgyMDU%2FZDU1NzQ2MDI; iweb_username=cf0c179f04MDc2MDAyOTAwOTA5PTJmNTM3Njg0Njc5bDU%2FMTYxMDY; iweb_safecode=b2e94c323aMDIxNzAzMDAwMDk2YzY1MGI5PDIwOjJnamE5YjJmMjZkM2IzajA0MGpjY2UzPmRmMzc7PmJuN2EyYmFhMjJhMw; iweb_head_ico=f04b7ffcf4NjA2OTA4MDYxNjA8OTRkMzY2NzgyNjc6YTUxOjM3OTo" for i in cookie.split("; "): dict1 = {} dict1["name"] = i.split("=")[0] dict1["value"] = i.split("=")[1] self.driver.add_cookie(dict1) def register(self,i): self.driver.get("http://127.0.0.1:8888/iwebshop/index.php?controller=system&action=default") self.driver.find_element_by_link_text("会员").click() self.driver.find_element_by_class_name("addition").click() self.driver.find_element_by_name("user_name").send_keys(i[0]) self.driver.find_element_by_name("email").send_keys(i[1]) self.driver.find_element_by_name("password").send_keys(i[2]) self.driver.find_element_by_name("repassword").send_keys(i[3]) sleep(5) Select(self.driver.find_element_by_id("province")).select_by_index(5) sleep(5) Select(self.driver.find_element_by_id("city")).select_by_index(5) sleep(5) Select(self.driver.find_element_by_id("area")).select_by_index(1) self.driver.find_element_by_xpath("//*[@id='admin_right']/div[2]/div/form/table/tbody/tr[17]/td[2]/button/span").click() self. element_is_present(i) def element_is_present(self,name): try: real_name = self.driver.find_element_by_xpath("//*[@id='list_table']/tbody/tr[1]/td[2]").text self.assertEqual(real_name,name[4]) except: raise AssertionError def test01(self): # 跳转到首页 with open("./bb.csv", "rt", encoding="utf-8-sig") as f: reader = csv.reader(f) for i in reader: # 子测试 with self.subTest(i=i): print(i[4]) if i[4] == 1: self.register(i) else: # 对于反向用例,注册不成功,捕获AssertionError,注册成功,就是bug with self.assertRaises(AssertionError): self.register(i) def tearDown(self): sleep(10) self.driver.close()
unittest 登录加强版
最新推荐文章于 2024-05-09 22:22:11 发布