资讯动态

Day22:web自动化项目实战

发布时间:2026/10/13 16:45:23 来源:尧图企业网站定制
一、unittest的使用方法1. 新建一个类继承 unittest.TestCase2. 导入 unittest3. 写一个以 test 开头的方法#2、 导入 unittest import unittest from selenium import webdriver from selenium.webdriver.common.by import By #1、新建一个类继承 unittest.TestCase class TestCase(unittest.TestCase): #3、写一个以 test 开头的方法 def test_01_login(self): # 打开浏览器 driver webdriver.Chrome() # 加载网页 driver.get(https://www.baidu.com) # 定位元素(利于封装) value driver.find_element(By.XPATH, //span[text()按图片搜索]).get_text() print(value) if __name__ __main__: unittest.main()二、unittest 运行测试用例的一个很大的坑unittest运行用例的方式有2种1. 命令行方式python -m unittest 模块名.pypython -m unittest test_case.TestCase.test_02_login2.main 方式坑点**unittest 默认按 ASCII 码顺序执行 test 开头方法不是代码从上到下顺序执行**三、项目实战1、大概内容商品列表该web 的框架实现商品查询import unittest from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select class TestCase(unittest.TestCase): def test_01_login(self): 登录 :return: global driver # 打开浏览器 driver webdriver.Chrome() # 加载网页 driver.get(http://localhost/ecshop/admin/privilege.php?actlogout) #输入用户名和密码 driver.find_element(By.NAME,username).send_keys(admin) driver.find_element(By.NAME,password).send_keys(admin123) #点击登录 driver.find_element(By.XPATH,//input[value进入管理中心]).click() def test_02_select_product(self): 查询商品 :return: #登录 global driver # 打开浏览器 driver webdriver.Chrome() driver.implicitly_wait(10) # 加载网页 driver.get(http://localhost/ecshop/admin/privilege.php?actlogout) # 输入用户名和密码 driver.find_element(By.NAME,username).send_keys(admin) driver.find_element(By.NAME,password).send_keys(admin123) # 点击登录 driver.find_element(By.XPATH,//input[value进入管理中心]).click() #查询商品 #进入菜单框架 driver.switch_to.frame(menu-frame) #点击商品列表 driver.find_element(By.LINK_TEXT,商品列表).click() #跳出框架 driver.switch_to.default_content() #进入到main的框架 driver.switch_to.frame(main-frame) #选中所有分类下拉框中的GSM手机 sel Select(driver.find_element(By.NAME,cat_id)) sel.select_by_value(4) #sel.select_by_visible_text(手机配件) #sel.select_by_index(7) #搜索 driver.find_element(By.XPATH,//input[value 搜索 ]).click() if __name__ __main__: unittest.main()2、补充一下拉框选中的处理方法1、通过值选中2、通过文本选中3、通过下标来选中二处理弹出窗口弹窗的3种类型alert只有确定confirm有确定有取消prompt有确定有取消还有输入值#处理弹出窗口: alert(只有确定),confirm(有确定有取消),prompt(有确定有取消还可以输入值) time.sleep(3) ale driver.switch_to.alert ale.accept() #access点击确定dismiss点击取消text获得文本send_keys输入值二、三、

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑