文件导出按导出步骤可以分为集中测试场景:
1、测试导出按钮功能:
1.1、校验元素可点击:
def click_export_button(self): self.wait_ele_clickable(self.down_load)
1.2、点击导出之后校验提示语句:
def click_export_button(self): self.normal_click(self.down_load, model='点击导出按钮')
def verify_text_display(self, text, index="1", model=None): time.sleep(1) try: WebDriverWait(self.driver, 1, 0.5).until(ec.visibility_of_element_located((By.XPATH, "(//*[contains(text(),'" + text + "')])" + "[" + index + "]"))) self.logger.info(f'Verify:"{text}" is displayed!') return True except Exception as e: self.logger.exception(f'Verify:"{text}" is not displayed!Message:"{e}"') allure.attach(self.driver.get_screenshot_as_png(), "截图", allure.attachment_type.PNG) return False
assert CommonStep(web_driver).verify_text_display('下载成功') == True
1.3、没有提示语,校验导出文件+1
def get_export_file(self): time.sleep(1) path = str(Path("Download/").absolute()) export_files = os.listdir(path) name = 'fujian1' name_list = [] for x in range(0, len(export_files)): if name in export_files[x]: name_list.append(export_files[x]) else: pass return name_list
assert len(export_file1)+1==len(export_file2)
2、测试导出的文件名称是否符合目标格式
2.1、先设置启动chrome时定制选项,用add_experimental_option来修改chrome设置
@fixture(scope="function", autouse=True) def web_driver(): print("====Open WebDriver====") global web_driver download_location = os.getcwd() + "\\Download" chrome_options = Options() chrome_options.add_experimental_option("prefs", { 'download.default_directory': download_location, 'profile.default_content_settings.images': 0, }) # chrome_options.add_argument('window-size=1920x1080') # chrome_options.add_argument('lang=zh_CN.UTF-8')
2.2、再获取文件名称
def get_export_file(self, date ): time.sleep(1) path = str(Path("Download/").absolute()) export_files = os.listdir(path) name = '{}工资明细报表'.format(date) name_list = [] for x in range(0, len(export_files)): if name in export_files[x]: name_list.append(export_files[x]) else: pass
3、对导出文件字段内容进行校验
def read_excel_rows(file): workbook = xlrd.open_workbook(file) sheet = workbook.sheet_by_index(0) datas = [] for row_idx in range(sheet.nrows): row = sheet.row_values(row_idx) # print(row) datas.append(row) return datas
4、校验页面数据和导出文件中的数据一致
4.1、获取页面列表中的数据data1(前面已介绍)
4.2、获取导出文件中的数据data2(前面已介绍)
4.3、比对数据是否一致
def check_export_datas_is_right(self, data1, data2): data_table = dict(zip(data1['title'], data1['data'][0])) for title_name in data1['title']: if data_table[title_name] == data2[title_name]: return True else: return False
5、文件排序,取最后/最新下载的文件
def get_download_file_name(self): download_path = os.path.join(os.getcwd(), "Download") file_name_list = os.listdir(download_path) # 按创建时间降序排列 sorted_file_list = sorted(file_name_list, key=lambda x: os.path.getctime(os.path.join(download_path, x)), reverse=True) return sorted_file_list[0]
6、获取下载的所有文件/文件数量
def get_download_folder_existing_file_number(self): download_path = os.path.join(os.getcwd(), "Download") return len(os.listdir(download_path))