【轉】Appium 中截取 element 圖片作為對比,判斷對比結果


其實在https://github.com/gb112211/Adb-For-Test 里面有一個截取element進行對比的方法,但是在使用appium時是無法使用的,因為其用到了uiautomator命令。。。

在appium中截取界面某個element,也就是截取屏幕的部分區域進行對比,在以圖片對比結果作為判斷依據的時候還是有用的,直接上代碼:
extend.py

 

#!/usr/bin/env python  
#coding=utf-8  
  
import os  
import platform  
import tempfile  
import shutil  
  
from PIL import Image  
  
PATH = lambda p: os.path.abspath(p)  
TEMP_FILE = PATH(tempfile.gettempdir() + "/temp_screen.png")  
  
class Appium_Extend(object):  
    def __init__(self, driver):  
        self.driver = driver  
  
    def get_screenshot_by_element(self, element):  
        #先截取整個屏幕,存儲至系統臨時目錄下  
        self.driver.get_screenshot_as_file(TEMP_FILE)  
  
        #獲取元素bounds  
        location = element.location  
        size = element.size  
        box = (location["x"], location["y"], location["x"] + size["width"], location["y"] + size["height"])  
  
        #截取圖片  
        image = Image.open(TEMP_FILE)  
        newImage = image.crop(box)  
        newImage.save(TEMP_FILE)  
  
        return self  
  
    def get_screenshot_by_custom_size(self, start_x, start_y, end_x, end_y):  
        #自定義截取范圍  
        self.driver.get_screenshot_as_file(TEMP_FILE)  
        box = (start_x, start_y, end_x, end_y)  
  
        image = Image.open(TEMP_FILE)  
        newImage = image.crop(box)  
        newImage.save(TEMP_FILE)  
  
        return self  
  
    def write_to_file( self, dirPath, imageName, form = "png"):  
        #將截屏文件復制到指定目錄下  
        if not os.path.isdir(dirPath):  
            os.makedirs(dirPath)  
        shutil.copyfile(TEMP_FILE, PATH(dirPath + "/" + imageName + "." + form))  
  
    def load_image(self, image_path):  
        #加載目標圖片供對比用  
        if os.path.isfile(image_path):  
            load = Image.open(image_path)  
            return load  
        else:  
            raise Exception("%s is not exist" %image_path)  
  
    #http://testerhome.com/topics/202  
    def same_as(self, load_image, percent):  
        #對比圖片,percent值設為0,則100%相似時返回True,設置的值越大,相差越大  
        import math  
        import operator  
  
        image1 = Image.open(TEMP_FILE)  
        image2 = load_image  
  
        histogram1 = image1.histogram()  
        histogram2 = image2.histogram()  
  
        differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2, \  
                                                         histogram1, histogram2)))/len(histogram1))  
        if differ <= percent:  
            return True  
        else:  
            return False  

接着跑了個appium腳本簡單測試了下:
extend_test.py

#coding=utf-8  
  
import unittest  
import os  
  
from extend import Appium_Extend  
from appium import webdriver  
  
class Test(unittest.TestCase):  
    #初始化環境  
    def setUp(self):  
        desired_caps = {}  
        desired_caps["platformName"] = "Android"  
        desired_caps["platformVersion"] = "4.3"  
        desired_caps["deviceName"] = "788a6ab5"  
        desired_caps["appPackage"] = "com.android.settings"  
        desired_caps["appActivity"] = ".Settings"  
  
        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)  
  
        self.extend = Appium_Extend(self.driver)  
  
        #回到主屏幕  
        self.driver.press_keycode(3)  
  
    #退出測試  
    def tearDown(self):  
        self.driver.quit()  
  
    def test_get_screen_by_element(self):  
        element = self.driver.find_element_by_id("com.android.deskclock:id/imageview")  
  
        self.extend.get_screenshot_by_element(element).write_to_file("f:\\screen", "image")  
        self.assertTrue(os.path.isfile("f:\\screen\\image.png"))  
  
    def test_same_as(self):  
        element = self.driver.find_element_by_id("com.android.deskclock:id/imageview")  
  
        load = self.extend.load_image("f:\\screen\\image.png")  
        #要求百分百相似  
        result = self.extend.get_screenshot_by_element(element).same_as(load, 0)  
        self.assertTrue(result)  
  
if __name__ == "__main__":  
    suite = unittest.TestSuite()  
    suite.addTest(Test("test_get_screen_by_element"))  
    suite.addTest(Test("test_same_as"))  
    #執行測試  
    unittest.TextTestRunner().run(suite)  

這里截取的圖片是下面這張圖中的時間插件:

截取后的圖片:

另外批量截圖、批量對比就針對需求再做擴展了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM