问题遇到的现象和发生背景 selenium ddt数据驱动+断言失败则截图,无法截图 - # 1.selenium 引入 webdriver
- import os
- from selenium import webdriver
- # 2.引入ddt代码库
- import ddt
- # 3.引入unittest
- import unittest
- import time
- from selenium.webdriver.common.by import By
- # 5.引入步骤1中的数据读取方法
- from selenium.webdriver.support.select import Select
-
- from csv_4 import read
-
-
- @ddt.ddt
- # ddt装饰器
- class Pxnr(unittest.TestCase):
- def setUp(self):
- # 4.selenium-webdriver 打开谷歌
- self.driver = webdriver.Chrome()
- # 6.get方法获取页面
- self.driver.get("http://172.32.254.111/ams/front/login.do")
- # 7.智能等待30秒
- self.driver.implicitly_wait(30)
-
- def tearDown(self):
- self.driver.quit()
- stream_info = read()
- # 创建一个变量来接受数据
- @ddt.data(*stream_info)
-
-
- def test_pxnr(self, list):
- try:
- driver = self.driver
- driver.find_element(By.NAME, "taskId").send_keys("24")
- # 登录页面
- driver.find_element(By.ID, "loginName").send_keys("2022061304")
- driver.find_element(By.NAME, "password").send_keys("RM0901")
- driver.find_element(By.TAG_NAME, "button").click()
- # 资产报废页面
- driver.find_element(By.LINK_TEXT, "资产报废").click() # 资产报废-链接
- # 资产报废-新增
- driver.find_element(By.XPATH, '//*[@id="fmsearch"]/div/button').click() # 报废登记-按钮
- #
- aa = driver.find_element(By.NAME, "assetId") # 资产名称
- Select(aa).select_by_visible_text(list[0])
- #
- time.sleep(1)
- driver.find_element(By.XPATH, '//*[@id="软件部"]').click() # 申请人
- time.sleep(1)
- #
- dd = driver.find_element(By.NAME, 'dictScrapWay') # 报废方式
- Select(dd).select_by_visible_text(list[1]) # 选择
- # list[1] 报废方式-下拉列表 输入读取的数据
- #
- time.sleep(1)
- driver.find_element(By.XPATH, '//*[@id="scrapDate"]').click() # 报废日期-输入框-点击
- time.sleep(1)
- driver.find_element(By.XPATH, '//*[@id="scrapDate"]').send_keys("20220616")
- driver.find_element(By.XPATH, '//*[@id="remark"]').send_keys("尝试")
- #
- driver.find_element(By.XPATH, '//*[@id="submitButton"]').click() # 点击提交按钮
- element = driver.switch_to.alert.text # 弹出保存 必须强制等待,缓冲
- print("1.实际结果:", element)
- # 实际结果和预期结果进行断言 assertEqual断言方法进行对比
- # 如果不正确要向 (下面)except获取时间
- self.assertEqual(list[2], element) # 预期 实际
- time.sleep(2)
- driver.switch_to.alert.accept() # 确定
- time.sleep(2)
- except:
- #获取时间
- current_time = time.strftime("%Y%m%d%H%M%S") #20221019163221
- # #获取当前绝对路径
- func_path = os.path.dirname(__file__) #D:\charm\race
- # 将路径转为字符串
- base_dir = os.path.dirname(func_path) #D:\charm
- # 对路径的字符串进行替换
- base_dir = base_dir.replace('\\', '/') #D:/charm
- filepath = base_dir + '/race/screenshot/img' + current_time + '.png'
- print("5.",filepath)#D:/charm/race/screenshot/img20221019163221.png
- jt = self.driver.get_screenshot_as_file(filepath)
- print(jt)
- finally:
- self.driver.quit()
-
- if __name__ == '__main__':
- unittest.main()
- # 调用unittest.main()启动测试
-
-
复制代码运行结果及报错内容
我想要达到的结果请教,怎么才能成功截图啊
|