您好,登錄后才能下訂單哦!
在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。