linkapi.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from hytest import *
  2. from playwright.sync_api import sync_playwright
  3. from PIL import Image, ImageChops
  4. from bs4 import BeautifulSoup
  5. import requests
  6. class APILink():
  7. #打开链接,返回title
  8. def obtain_url_title(self,url):
  9. response = requests.get(url)
  10. response.encoding = 'utf-8' # 设置编码为gbk
  11. soup = BeautifulSoup(response.text, 'html.parser')
  12. title = soup.title.string
  13. return title
  14. #打开链接,status=200,返回true
  15. def open_url_status(self,url):
  16. response = requests.get(url)
  17. status_code = response.status_code
  18. if status_code==200:
  19. return True
  20. else:
  21. return False
  22. def fetch_page_source(self,url):
  23. try:
  24. response = requests.get(url)
  25. response.raise_for_status() # 检查请求是否成功
  26. return response.text # 返回网页源代码
  27. except requests.RequestException as e:
  28. print(f"Error fetching the URL: {e}")
  29. return None
  30. def check_value_in_source(self,url,value):
  31. page_source = self.fetch_page_source(url)
  32. if page_source:
  33. if value in page_source:
  34. # print(f"'{value_to_check}' exists in the page source.")
  35. return True
  36. else:
  37. # print(f"'{value_to_check}' does not exist in the page source.")
  38. return False
  39. else:
  40. # print("Failed to retrieve the page source.")
  41. return False
  42. def setup(self):
  43. # 初始化 Playwright
  44. self.playwright = sync_playwright().start()
  45. self.browser = self.playwright.chromium.launch(headless=True)
  46. self.page = self.browser.new_page()
  47. def teardown(self):
  48. # 关闭浏览器
  49. self.browser.close()
  50. self.playwright.stop()
  51. def obtain_element_text(self,url,element):
  52. self.page.goto(url)
  53. # 等待元素出现
  54. self.page.wait_for_selector(element)
  55. # 查找元素
  56. element_handle = self.page.locator(element)
  57. # 获取元素的文本或属性值
  58. element_text = element_handle.text_content()
  59. # element_attribute = element_handle.get_attribute('属性名')
  60. # 设置超时时间
  61. self.page.wait_for_timeout(3000)
  62. return element_text
  63. # print(f'元素的属性值: {element_attribute}')
  64. #网页截图模糊遮罩方法,适用于网页有动态元素,进行遮罩比较
  65. def save_screenshot_mask(self,url, output_path, elements, clip=None):
  66. locs = []
  67. self.page.goto(url)
  68. for element in elements:
  69. loc = self.page.locator(element)
  70. locs.append(loc)
  71. self.page.screenshot(path=output_path,mask=locs, clip=clip)
  72. #网页直接截图方法,适用于网页元素不变,可直接截图比较
  73. def save_screenshot(self,url, output_path, clip=None):
  74. self.page.goto(url)
  75. self.page.screenshot(path=output_path, clip=clip)
  76. def compare_images(self, image1_path, image2_path):
  77. image1 = Image.open(image1_path)
  78. image2 = Image.open(image2_path)
  79. diff = ImageChops.difference(image1, image2)
  80. if diff.getbbox() is None:
  81. INFO("两张图片完全相同")
  82. return True
  83. else:
  84. INFO("两张图片存在差异")
  85. return False
  86. #对比样本快照与当前快照
  87. def contrast_snapshot(self,url,expected_screenshot,current_screenshot,clip):
  88. # 如果不存在样本照片,生成样本照片
  89. if not os.path.exists(expected_screenshot):
  90. self.save_screenshot(url, expected_screenshot,clip)
  91. INFO(f"样本快照已保存:{expected_screenshot}")
  92. #生成对比照片
  93. apilink.save_screenshot(url, current_screenshot,clip)
  94. INFO(f"对比快照已保存:{expected_screenshot}")
  95. #返回对比结果
  96. result = apilink.compare_images(current_screenshot, expected_screenshot)
  97. return result
  98. #对比样本快照与当前快照2
  99. def contrast_snapshot_mask(self,url,expected_screenshot,current_screenshot,element,clip):
  100. # 如果不存在样本照片,生成样本照片
  101. if not os.path.exists(expected_screenshot):
  102. self.save_screenshot_mask(url, expected_screenshot,element,clip)
  103. INFO(f"样本快照已保存:{expected_screenshot}")
  104. #生成对比照片
  105. apilink.save_screenshot_mask(url, current_screenshot,element,clip)
  106. INFO(f"对比快照已保存:{expected_screenshot}")
  107. #返回对比结果
  108. result = apilink.compare_images(current_screenshot, expected_screenshot)
  109. return result
  110. apilink = APILink()