簡體   English   中英

如何在Appium中比較一張圖片,使用Python代碼?

[英]How to compare a picture in Appium, using Python code?

我需要在iOS app中對比模板圖片。 我需要使用什么語法? 我找到了很多關於它的信息,但只有 Java 語法。

我在 Mac 上使用 Appium + Python。

我無法使用 openCV 庫。 因為我無法安裝opencv4nodejs
您可以嘗試像這樣安裝它,使用 npm 模塊: npm i -g opencv4nodejs所以,我找到了比較圖像的解決方案。 您可以使用此代碼:

/**
 * Finds the a region in one image that best matches another, smaller, image.
 */
public static int[] findSubImage(BufferedImage im1, BufferedImage im2){
    int w1 = im1.getWidth(); int h1 = im1.getHeight();
    int w2 = im2.getWidth(); int h2 = im2.getHeight();
    assert(w2 <= w1 && h2 <= h1);
    // will keep track of best position found
    int bestX = 0; int bestY = 0; double lowestDiff = Double.POSITIVE_INFINITY;
    // brute-force search through whole image (slow...)
    for(int x = 0;x < w1-w2;x++){
        for(int y = 0;y < h1-h2;y++){
            double comp = compareImages(im1.getSubimage(x,y,w2,h2),im2);
            if(comp < lowestDiff){
                bestX = x; bestY = y; lowestDiff = comp;
            }
        }
    }
    // output similarity measure from 0 to 1, with 0 being identical
    System.out.println(lowestDiff + "output similarity measure from 0 to 1, with 0 being identical");
    Assertions.assertTrue(lowestDiff<0.5);
    // return best location
    return new int[]{bestX,bestY};
}

/**
 * Determines how different two identically sized regions are.
 */
public static double compareImages(BufferedImage im1, BufferedImage im2){
    assert(im1.getHeight() == im2.getHeight() && im1.getWidth() == im2.getWidth());
    double variation = 0.0;
    for(int x = 0;x < im1.getWidth();x++){
        for(int y = 0;y < im1.getHeight();y++){
            variation += compareARGB(im1.getRGB(x,y),im2.getRGB(x,y))/Math.sqrt(3);
        }
    }
    return variation/(im1.getWidth()*im1.getHeight());
}

/**
 * Calculates the difference between two ARGB colours (BufferedImage.TYPE_INT_ARGB).
 */
public static double compareARGB(int rgb1, int rgb2){
    double r1 = ((rgb1 >> 16) & 0xFF)/255.0; double r2 = ((rgb2 >> 16) & 0xFF)/255.0;
    double g1 = ((rgb1 >> 8) & 0xFF)/255.0;  double g2 = ((rgb2 >> 8) & 0xFF)/255.0;
    double b1 = (rgb1 & 0xFF)/255.0;         double b2 = (rgb2 & 0xFF)/255.0;
    double a1 = ((rgb1 >> 24) & 0xFF)/255.0; double a2 = ((rgb2 >> 24) & 0xFF)/255.0;
    // if there is transparency, the alpha values will make difference smaller
    return a1*a2*Math.sqrt((r1-r2)*(r1-r2) + (g1-g2)*(g1-g2) + (b1-b2)*(b1-b2));
}

我在這里找到了它。 它工作得很好,但太慢了。 如果你也不能使用openCV,你可以使用這種方式。 祝你好運。

我猜您已經通過使用get_screenshot_as_base64()此處為文檔)檢索了模板圖像和真實圖像(以檢查模板)。

然后你可以嘗試使用 Appium 內嵌的 OpenCV 庫,通過調用函數get_images_similarity()這里是實現)來檢查兩幅圖像的相似程度,得到兩幅圖像的相似度分數並在此基礎上進行推理。

有關更多詳細信息,請查看這篇有用的文章!

我試圖通過 OpenCV 庫將圖片與模板進行比較。 我就是做這個的:

  1. 將方法添加到base_page.py

     def compare_image_with_screenshot(self, image_name: str): os.chdir('../src/screenshots/') with open(f'{image_name}.png', 'rb') as img: first_image = base64.b64encode(img.read()).decode('ascii') second_image = base64.b64encode(self._driver.get_screenshot_as_png()).decode('ascii') return self._driver.get_images_similarity(first_image, second_image)
  2. 在頁面對象文件中使用此方法。

     @allure.step('Compare screenshot with template') def get_image_comparison_percents(self): """ This method gets screenshot on device with template in repo. Comparison result is percentage of similarity. Test is OK if comparison more than 90% """ result = self.compare_image_with_screenshot(OfflineLocators.offline_stub) return result.get('score')
  3. 在必要的測試中使用步驟。

     @allure.link(url='https://jira.myproject.tech/browse/TEST-1', name='TEST-1 - Offline stub') @allure.title('Offline stub') def test_offline_stub(appdriver): TourActions(appdriver).skip_tour() Navigation(appdriver).open_my_offline_page() assert Offline(appdriver).get_page_title_text() == 'Offline' assert Offline(appdriver).get_image_comparison_percents() > 0.9

由於所有這些,我得到了一定比例的圖片相似性。 它可以是你需要的那個百分比。 對於我的測試,它是可以的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM