簡體   English   中英

在 Sikuli X Java 中查找圖像

[英]finding image in Sikuli X Java

我在嘗試在屏幕上查找圖像時遇到問題,我嘗試以兩種不同的方式進行操作,但似乎對我不起作用。 我正在嘗試使用在屏幕上顯示的 IOS 模擬器上運行的 Appium 來執行此操作,因此我不認為這是截取屏幕截圖的問題。

我正在運行 MAC OSX El Capitan 我在我的項目中導入了 Sikuli X java API

我還需要導入 MAC Sikuli 庫 jar 嗎?

這是我迄今為止嘗試過的:

1.

Screen s = new Screen();
Pattern test = new Pattern("/Users/ealiaj/Desktop/Automation/workspace/WheelsUp - IOS/screenshot.jpg");
try {
    s.find(test);
} catch (FindFailed e) {

}

2.

Screen s = new Screen();
try {
    s.find("screenshot.jpg");
} catch (FindFailed e) {

}

我不斷發現找不到錯誤。

錯誤信息:

FindFailed:在文件 Region.java 中的 S(0)[0,0 1440x900] 行 2189 中找不到 /Users/ealiaj/Desktop/Automation/workspace/WheelsUp - IOS/screenshot1.jpg

試圖找到的圖像 這是屏幕上的圖像,大紅色矩形是我為其創建屏幕截圖並嘗試查找的圖像,但出現該錯誤。

我唯一能夠成功找到的是那個灰色矩形,或者至少它不會引發錯誤。

您可以使用此方法來驗證圖像:

@Test
public void verifyImages() {    

    //WebElement img = driver.findElementByClassName("android.widget.ImageView");

   //take screen shot
    File screen = ((TakesScreenshot) driver)
                        .getScreenshotAs(OutputType.FILE);


    //capture image of searched contact icon
    List<WebElement > imageList = driver.findElementsByXPath("//*[@class='android.widget.ImageView' and @index='0']");
    System.out.println(imageList.size());

    System.out.println(i);
    WebElement image = imageList.get(1);
    Point point = image.getLocation();

    //get element dimension
    int width = image.getSize().getWidth();
    int height = image.getSize().getHeight();

    BufferedImage img = ImageIO.read(screen);
    BufferedImage dest = img.getSubimage(point.getX(), point.getY(), width,
                                                                 height);
    ImageIO.write(dest, "png", screen);
    File file = new File("Menu.png");
    FileUtils.copyFile(screen, file);

    //verify images
    verifyImage("Menu.png", "Menu.png" );
}



public void verifyImage(String image1, String image2) throws IOException{
    File fileInput = new File(image1);
    File fileOutPut = new File(image2);

    BufferedImage bufileInput = ImageIO.read(fileInput);
    DataBuffer dafileInput = bufileInput.getData().getDataBuffer();
    int sizefileInput = dafileInput.getSize();                     
    BufferedImage bufileOutPut = ImageIO.read(fileOutPut);
    DataBuffer dafileOutPut = bufileOutPut.getData().getDataBuffer();
    int sizefileOutPut = dafileOutPut.getSize();
    Boolean matchFlag = true;
    if(sizefileInput == sizefileOutPut) {                         
       for(int j=0; j<sizefileInput; j++) {
             if(dafileInput.getElem(j) != dafileOutPut.getElem(j)) {
                   matchFlag = false;
                   break;
             }
        }
    }
    else                            
       matchFlag = false;
    Assert.assertTrue(matchFlag, "Images are not same");    
 }

錯誤消息表明該程序正在查找 .PNG 文件,並且在您的代碼中放置了一個 .JPG 文件。

暫無
暫無

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

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