linkapi.py 4.6 KB

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