from hytest import * from playwright.sync_api import sync_playwright from PIL import Image, ImageChops class APILink(): def setup(self): # 初始化 Playwright self.playwright = sync_playwright().start() self.browser = self.playwright.chromium.launch(headless=True) self.page = self.browser.new_page() def teardown(self): # 关闭浏览器 self.browser.close() self.playwright.stop() def save_screenshot(self,url, output_path, clip): self.page.goto(url) self.page.screenshot(path=output_path, clip=clip) def compare_images(self, image1_path, image2_path): image1 = Image.open(image1_path) image2 = Image.open(image2_path) diff = ImageChops.difference(image1, image2) if diff.getbbox() is None: INFO("两张图片完全相同") return True else: INFO("两张图片存在差异") return False #对比样本快照与当前快照 def obtain_original_snapshot(self,url,expected_screenshot,current_screenshot,clip): # 如果不存在样本照片,生成样本照片 if not os.path.exists(expected_screenshot): self.save_screenshot(url, expected_screenshot, clip) INFO(f"样本快照已保存:{expected_screenshot}") #生成对比照片 apilink.save_screenshot(url, current_screenshot, clip) #返回对比结果 result = apilink.compare_images(current_screenshot, expected_screenshot) return result apilink = APILink()