2016-05-10 6 views
5

以下のコードを使用して、アプリケーションJainLibraryのページのスクリーンショットを正常に取得できました。私はjunitとappiumを使用しています。appiumを使用してスクリーンショットを参照画像と比較する方法

public String Screenshotpath = "Mention the folder Location"; 
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
FileUtils.copyFile(scrFile, new File(Screenshotpath+"Any name".jpg")); 

ここで、スクリーンショットを参照画像と比較して、テストケースを進めることができます。あなたはPNGとして参照スクリーンショットを保存する必要が

// save the baseline screenshot 

driver.get("https://www.google.co.uk/intl/en/about/"); 
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
FileUtils.copyFile(scrFile, new File("c:\\temp\\screenshot.png")); 

// take another screenshot and compare it to the baseline 

driver.get("https://www.google.co.uk/intl/en/about/"); 
byte[] pngBytes = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES); 

if (IsPngEquals(new File("c:\\temp\\screenshot.png"), pngBytes)) { 
    System.out.println("equals"); 
} else { 
    System.out.println("not equals"); 
} 
public static boolean IsPngEquals(File pngFile, byte[] pngBytes) throws IOException { 
    BufferedImage imageA = ImageIO.read(pngFile); 

    ByteArrayInputStream inStreamB = new ByteArrayInputStream(pngBytes); 
    BufferedImage imageB = ImageIO.read(inStreamB); 
    inStreamB.close(); 

    DataBufferByte dataBufferA = (DataBufferByte)imageA.getRaster().getDataBuffer(); 
    DataBufferByte dataBufferB = (DataBufferByte)imageB.getRaster().getDataBuffer(); 

    if (dataBufferA.getNumBanks() != dataBufferB.getNumBanks()) { 
     return false; 
    } 

    for (int bank = 0; bank < dataBufferA.getNumBanks(); bank++) { 
     if (!Arrays.equals(dataBufferA.getData(bank), dataBufferB.getData(bank))) { 
      return false; 
     } 
    } 

    return true; 
} 

注:

+1

あなたはどのような結果を期待していますか? –

+0

アプリケーションにロケーション条件があります。私がその条件に合致することができれば、次のテストケースにさらに進むことができます。比較が必要な他の多くのテストケースもあります。 – Alex

答えて

3

簡単な解決策は、参照screenshootして、各ピクセルを比較することであろう。 JPEGの形式でピクセルが変更されます。

+0

あなたの答えをありがとう。私は今日それを試し、その後適切な結果であなたに戻ってきます。 – Alex

関連する問題