linkapi.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from hytest import *
  2. from PIL import Image, ImageChops
  3. from bs4 import BeautifulSoup
  4. import requests
  5. class APILink():
  6. #增加初始化方法
  7. def setup(self):
  8. pass
  9. #增加结束方法
  10. def teardown(self):
  11. pass
  12. headers = {
  13. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67;jy-test"
  14. }
  15. #headers设置为全局变量
  16. GSTORE['headers'] = headers
  17. # session对象设置为全局变量
  18. s = requests.Session()
  19. GSTORE['s'] = s
  20. #打开链接,返回title
  21. def obtain_url_title(self,url):
  22. headers=GSTORE['headers']
  23. s = GSTORE['s']
  24. response = s.get(url,headers=headers)
  25. response.encoding = 'utf-8' # 设置编码为utf-8
  26. if response.status_code == 200:
  27. soup = BeautifulSoup(response.text, 'html.parser')
  28. title_tag = soup.title
  29. if title_tag is not None:
  30. return title_tag.string
  31. else:
  32. return 'No title found'
  33. else:
  34. return 'Failed to retrieve content'
  35. #打开链接,status=200,返回true
  36. def open_url_status(self,url):
  37. headers=GSTORE['headers']
  38. s = GSTORE['s']
  39. response = s.get(url,headers=headers)
  40. status_code = response.status_code
  41. if status_code==200:
  42. return True
  43. else:
  44. return False
  45. #打开链接,返回页面底部某个值
  46. def obtain_url_bottom(self,url,loc):
  47. response = requests.get(url)
  48. response.encoding = 'utf-8' # 设置编码为gbk
  49. soup = BeautifulSoup(response.text, 'html.parser')
  50. # 查找元素并获取值
  51. element = soup.find('div', attrs={'class': loc})
  52. # element = soup.find(loc)
  53. element_value = element.text
  54. return element_value
  55. def obtain_element_text(self,url,element):
  56. self.page.goto(url)
  57. # 等待元素出现
  58. self.page.wait_for_selector(element)
  59. # 查找元素
  60. element_handle = self.page.locator(element)
  61. # 获取元素的文本或属性值
  62. element_text = element_handle.text_content()
  63. # element_attribute = element_handle.get_attribute('属性名')
  64. # 设置超时时间
  65. self.page.wait_for_timeout(3000)
  66. return element_text
  67. # print(f'元素的属性值: {element_attribute}')
  68. #网页截图模糊遮罩方法,适用于网页有动态元素,进行遮罩比较
  69. def save_screenshot_mask(self,url, output_path, elements, clip=None):
  70. locs = []
  71. self.page.goto(url)
  72. for element in elements:
  73. loc = self.page.locator(element)
  74. locs.append(loc)
  75. self.page.screenshot(path=output_path,mask=locs, clip=clip)
  76. #网页直接截图方法,适用于网页元素不变,可直接截图比较
  77. def save_screenshot(self,url, output_path, clip=None):
  78. self.page.goto(url)
  79. self.page.screenshot(path=output_path, clip=clip)
  80. def compare_images(self, image1_path, image2_path):
  81. image1 = Image.open(image1_path)
  82. image2 = Image.open(image2_path)
  83. diff = ImageChops.difference(image1, image2)
  84. if diff.getbbox() is None:
  85. INFO("两张图片完全相同")
  86. return True
  87. else:
  88. INFO("两张图片存在差异")
  89. return False
  90. #对比样本快照与当前快照
  91. def contrast_snapshot(self,url,expected_screenshot,current_screenshot,clip):
  92. # 如果不存在样本照片,生成样本照片
  93. if not os.path.exists(expected_screenshot):
  94. self.save_screenshot(url, expected_screenshot,clip)
  95. INFO(f"样本快照已保存:{expected_screenshot}")
  96. #生成对比照片
  97. apilink.save_screenshot(url, current_screenshot,clip)
  98. INFO(f"对比快照已保存:{expected_screenshot}")
  99. #返回对比结果
  100. result = apilink.compare_images(current_screenshot, expected_screenshot)
  101. return result
  102. #对比样本快照与当前快照2
  103. def contrast_snapshot_mask(self,url,expected_screenshot,current_screenshot,element,clip):
  104. # 如果不存在样本照片,生成样本照片
  105. if not os.path.exists(expected_screenshot):
  106. self.save_screenshot_mask(url, expected_screenshot,element,clip)
  107. INFO(f"样本快照已保存:{expected_screenshot}")
  108. #生成对比照片
  109. apilink.save_screenshot_mask(url, current_screenshot,element,clip)
  110. INFO(f"对比快照已保存:{expected_screenshot}")
  111. #返回对比结果
  112. result = apilink.compare_images(current_screenshot, expected_screenshot)
  113. return result
  114. apilink = APILink()