linkapi.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #打开链接,返回页面底部某个值
  23. def obtain_url_bottom(self,url,loc):
  24. response = requests.get(url)
  25. response.encoding = 'utf-8' # 设置编码为gbk
  26. soup = BeautifulSoup(response.text, 'html.parser')
  27. # 查找元素并获取值
  28. element = soup.find('div', attrs={'class': loc})
  29. # element = soup.find(loc)
  30. element_value = element.text
  31. return element_value
  32. def setup(self):
  33. # 初始化 Playwright
  34. self.playwright = sync_playwright().start()
  35. self.browser = self.playwright.chromium.launch(headless=True)
  36. self.page = self.browser.new_page()
  37. def teardown(self):
  38. # 关闭浏览器
  39. self.browser.close()
  40. self.playwright.stop()
  41. def obtain_element_text(self,url,element):
  42. self.page.goto(url)
  43. # 等待元素出现
  44. self.page.wait_for_selector(element)
  45. # 查找元素
  46. element_handle = self.page.locator(element)
  47. # 获取元素的文本或属性值
  48. element_text = element_handle.text_content()
  49. # element_attribute = element_handle.get_attribute('属性名')
  50. # 设置超时时间
  51. self.page.wait_for_timeout(3000)
  52. return element_text
  53. # print(f'元素的属性值: {element_attribute}')
  54. #网页截图模糊遮罩方法,适用于网页有动态元素,进行遮罩比较
  55. def save_screenshot_mask(self,url, output_path, elements, clip=None):
  56. locs = []
  57. self.page.goto(url)
  58. for element in elements:
  59. loc = self.page.locator(element)
  60. locs.append(loc)
  61. self.page.screenshot(path=output_path,mask=locs, clip=clip)
  62. #网页直接截图方法,适用于网页元素不变,可直接截图比较
  63. def save_screenshot(self,url, output_path, clip=None):
  64. self.page.goto(url)
  65. self.page.screenshot(path=output_path, clip=clip)
  66. def compare_images(self, image1_path, image2_path):
  67. image1 = Image.open(image1_path)
  68. image2 = Image.open(image2_path)
  69. diff = ImageChops.difference(image1, image2)
  70. if diff.getbbox() is None:
  71. INFO("两张图片完全相同")
  72. return True
  73. else:
  74. INFO("两张图片存在差异")
  75. return False
  76. #对比样本快照与当前快照
  77. def contrast_snapshot(self,url,expected_screenshot,current_screenshot,clip):
  78. # 如果不存在样本照片,生成样本照片
  79. if not os.path.exists(expected_screenshot):
  80. self.save_screenshot(url, expected_screenshot,clip)
  81. INFO(f"样本快照已保存:{expected_screenshot}")
  82. #生成对比照片
  83. apilink.save_screenshot(url, current_screenshot,clip)
  84. INFO(f"对比快照已保存:{expected_screenshot}")
  85. #返回对比结果
  86. result = apilink.compare_images(current_screenshot, expected_screenshot)
  87. return result
  88. #对比样本快照与当前快照2
  89. def contrast_snapshot_mask(self,url,expected_screenshot,current_screenshot,element,clip):
  90. # 如果不存在样本照片,生成样本照片
  91. if not os.path.exists(expected_screenshot):
  92. self.save_screenshot_mask(url, expected_screenshot,element,clip)
  93. INFO(f"样本快照已保存:{expected_screenshot}")
  94. #生成对比照片
  95. apilink.save_screenshot_mask(url, current_screenshot,element,clip)
  96. INFO(f"对比快照已保存:{expected_screenshot}")
  97. #返回对比结果
  98. result = apilink.compare_images(current_screenshot, expected_screenshot)
  99. return result
  100. apilink = APILink()