在GUI自动化测试中,截图和对比是非常重要的一部分
from PIL import ImageGrab
def take_screenshot():
screenshot = ImageGrab.grab()
screenshot.save("screenshot.png")
import cv2
import numpy as np
from PIL import ImageGrab
def take_screenshot():
screenshot = ImageGrab.grab()
img_np = np.array(screenshot)
cv2.imwrite("screenshot.png", img_np)
from selenium import webdriver
def take_screenshot():
driver = webdriver.Chrome()
driver.get("https://www.example.com")
driver.save_screenshot("screenshot.png")
driver.quit()
import pyautogui
def take_screenshot():
screenshot = pyautogui.screenshot()
screenshot.save("screenshot.png")
import subprocess
def take_screenshot():
subprocess.call(["import", "-window", "root", "screenshot.png"])
import subprocess
def compare_images(image1, image2):
result = subprocess.run(["compare", "-metric", "AE", image1, image2, "diff.png"], capture_output=True, text=True)
return int(result.stdout.strip())
difference = compare_images("image1.png", "image2.png")
print(f"Difference: {difference}")
import cv2
def compare_images(image1, image2):
img1 = cv2.imread(image1)
img2 = cv2.imread(image2)
difference = cv2.absdiff(img1, img2)
return cv2.countNonZero(difference)
difference = compare_images("image1.png", "image2.png")
print(f"Difference: {difference}")
from PIL import Image, ImageChops
def compare_images(image1, image2):
img1 = Image.open(image1)
img2 = Image.open(image2)
diff = ImageChops.difference(img1, img2)
return sum(diff.getdata())
difference = compare_images("image1.png", "image2.png")
print(f"Difference: {difference}")
这些示例展示了如何使用不同的Python库进行截图和对比。你可以根据你的需求选择合适的库。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。