allure中可以保存日志信息和截圖
日志allure能夠自動識別。截圖需要自己在添加allure方法。
具體實現如下:
import allure
def save_scree_image(self): """ 對當前頁面進行截圖 :return: """ start_time = time.time() filename = '{}.png'.format(start_time) file_path = os.path.join(error_img, filename) self.driver.save_screenshot(file_path) log.info("錯誤頁面截圖成功,圖表保存的路徑:{}".format(file_path)) return file_path def save_image_to_allure(self): """ 保存失敗的截圖到allure報告中 :return: """ file_path = self.save_scree_image() with open(file_path, "rb") as f: file = f.read() allure.attach(file, "失敗截圖", allure.attachment_type.PNG)
附:open 函數中 r 和 rb 的區別 【參考:https://www.jianshu.com/p/d9ab29cbb103】
如果我們讀取人工書寫的數據那么就使用r
,如果我們讀取非人工書寫的數據那么我們就是使用rb
,圖片就是一種非常典型的非人工書寫數據。