liumiaomiao před 1 rokem
rodič
revize
11d88fe734

+ 10 - 0
cases/未登录功能/链接验证/__st__.py

@@ -0,0 +1,10 @@
+from lib.linkapi import apilink
+
+def suite_setup():
+    # 初始化 Playwright
+    apilink.setup()
+
+
+def suite_teardown():
+    # 关闭浏览器
+    apilink.teardown()

+ 23 - 0
cases/未登录功能/链接验证/app_search_link.py

@@ -0,0 +1,23 @@
+from hytest import *
+from lib.linkapi import apilink
+class T0001:
+    #测试用例名称
+    name = "app标讯搜索页验证"
+    #测试步骤
+    def teststeps(self):
+        STEP(1, "访问页面并截取特定区域快照")
+        url = 'https://app-a1.jianyu360.cn/jy_mobile/search/middle/bidding'
+        #样本快照
+        expected_screenshot = 'images/expected_app_search_screenshot.png'
+        current_screenshot = 'images/current_app_search_screenshot.png'
+
+        # 定义要截取的区域
+        clip = {
+            'x': 0,
+            'y': 0,
+            'width': 1280,  # 根据你的截图实际宽度设置
+            'height': 600  # 根据你的截图实际高度设置
+        }
+        result=apilink.obtain_original_snapshot(url,expected_screenshot,current_screenshot,clip)
+
+        CHECK_POINT("检查图片是否相同", result)

+ 45 - 0
lib/linkapi.py

@@ -0,0 +1,45 @@
+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()