<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問(wèn) http://qaseven.github.io/

    移植MonkeyRunner的圖片對(duì)比功能實(shí)現(xiàn)-Appium篇

     如果你的目標(biāo)測(cè)試app有很多imageview組成的話,這個(gè)時(shí)候monkeyrunner的截圖比較功能就體現(xiàn)出來(lái)了。而其他幾個(gè)流行的框架如Robotium,UIAutomator以及Appium都提供了截圖,但少了兩個(gè)功能:
      獲取子圖
      圖片比較
      既然Google開(kāi)發(fā)的MonkeyRunner能盛行這么久,且它體功能的結(jié)果驗(yàn)證功能只有截屏比較,那么必然有它的道理,有它存在的價(jià)值,所以我們很有必要在需要的情況下把它相應(yīng)的功能給移植到其他框架上面上來(lái)。
      經(jīng)過(guò)本人前面文章描述的幾個(gè)框架的源碼的研究(robotium還沒(méi)有做),大家可以知道MonkeyRunner是跑在PC端的,只有在需要發(fā)送相應(yīng)的命令事件時(shí)才會(huì)驅(qū)動(dòng)目標(biāo)機(jī)器的monkey或者shell等。比如獲取圖片是從目標(biāo)機(jī)器的buffer設(shè)備得到,但是比較圖片和獲取子圖是從客戶PC端做的。
      這里Appium工作的方式非常的類似,因?yàn)樗彩窃诳蛻舳伺?,但需要注入事件發(fā)送命令時(shí)還是通過(guò)目標(biāo)機(jī)器段的bootstrap來(lái)驅(qū)動(dòng)uiatuomator來(lái)完成的,所以要把MonkeyRunner的獲取子圖已經(jīng)圖片比較的功能移植過(guò)來(lái)是非常容易的事情。
      但UiAutomator就是另外一回事了,因?yàn)樗耆窃谀繕?biāo)機(jī)器那邊跑的,所以你的代碼必須要android那邊支持,所以本人在移植到UiAutomator上面就碰到了問(wèn)題,這里先給出Appium 上面的移植,以方便大家的使用,至于UiAutomator和Robotium的,今后本人會(huì)酌情考慮是否提供給大家。
      還有就是這個(gè)移植過(guò)來(lái)的代碼沒(méi)有經(jīng)過(guò)優(yōu)化的,比如失敗是否保存圖片以待今后查看等。大家可以基于這個(gè)基礎(chǔ)實(shí)現(xiàn)滿足自己要求的功能
      1. 移植代碼
      移植代碼放在一個(gè)Util.java了工具類中:
    public static boolean sameAs(BufferedImage myImage,BufferedImage otherImage, double percent)
    {
    //BufferedImage otherImage = other.getBufferedImage();
    //BufferedImage myImage = getBufferedImage();
    if (otherImage.getWidth() != myImage.getWidth()) {
    return false;
    }
    if (otherImage.getHeight() != myImage.getHeight()) {
    return false;
    }
    int[] otherPixel = new int[1];
    int[] myPixel = new int[1];
    int width = myImage.getWidth();
    int height = myImage.getHeight();
    int numDiffPixels = 0;
    for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
    if (myImage.getRGB(x, y) != otherImage.getRGB(x, y)) {
    numDiffPixels++;
    }
    }
    }
    double numberPixels = height * width;
    double diffPercent = numDiffPixels / numberPixels;
    return percent <= 1.0D - diffPercent;
    }
    public static BufferedImage getSubImage(BufferedImage image,int x, int y, int w, int h)
    {
    return image.getSubimage(x, y, w, h);
    }
    public static BufferedImage getImageFromFile(File f) {
    BufferedImage img = null;
    try {
    img = ImageIO.read(f);
    } catch (IOException e) {
    //if failed, then copy it to local path for later check:TBD
    //FileUtils.copyFile(f, new File(p1));
    e.printStackTrace();
    System.exit(1);
    }
    return img;
    }
    這里就不多描述了,基本上就是基于MonkeyRunner做輕微的修改,所以叫做移植。而UiAutomator就可能需要大改動(dòng),要重現(xiàn)實(shí)現(xiàn)了。
     2. 客戶端調(diào)用代碼舉例
    packagesample.demo.AppiumDemo;
    importstaticorg.junit.Assert.*;
    importjava.awt.image.BufferedImage;
    importjava.io.File;
    importjava.io.IOException;
    importjava.net.URL;
    importjavax.imageio.ImageIO;
    importlibs.Util;
    importio.appium.java_client.android.AndroidDriver;
    importorg.apache.commons.io.FileUtils;
    importorg.junit.After;
    importorg.junit.Before;
    importorg.junit.Test;
    importorg.openqa.selenium.By;
    importorg.openqa.selenium.OutputType;
    importorg.openqa.selenium.WebElement;
    importorg.openqa.selenium.remote.DesiredCapabilities;
    publicclassCompareScreenShots{
    privateAndroidDriverdriver;
    @Before
    publicvoidsetUp()throwsException{
    DesiredCapabilitiescap=newDesiredCapabilities();
    cap.setCapability("deviceName","Android");
    cap.setCapability("appPackage","com.example.android.notepad");
    cap.setCapability("appActivity",".NotesList");
    driver=newAndroidDriver(newURL("http://127.0.0.1:4723/wd/hub"),cap);
    }
    @After
    publicvoidtearDown()throwsException{
    driver.quit();
    }
    @Test
    publicvoidcompareScreenAndSubScreen()throwsInterruptedException,IOException{
    Thread.sleep(2000);
    WebElementel=driver.findElement(By.className("android.widget.ListView")).findElement(By.name("Note1"));
    el.click();
    Thread.sleep(1000);
    Stringp1="C:/1";
    Stringp2="C:/2";
    Filef2=newFile(p2);
    Filef1=driver.getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(f1,newFile(p1));
    BufferedImageimg1=Util.getImageFromFile(f1);
    f2=driver.getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(f2,newFile(p2));
    BufferedImageimg2=Util.getImageFromFile(f2);
    Booleansame=Util.sameAs(img1,img2,0.9);
    assertTrue(same);
    BufferedImagesubImg1=Util.getSubImage(img1,6,39,474,38);
    BufferedImagesubImg2=Util.getSubImage(img1,6,39,474,38);
    same=Util.sameAs(subImg1,subImg2,1);
    Filef3=newFile("c:/sub-1.png");
    ImageIO.write(subImg1,"PNG",f3);
    Filef4=newFile("c:/sub-2.png");
    ImageIO.write(subImg1,"PNG",f4);
    }
    }
      也不多解析了,沒(méi)有什么特別的東西。
      大家用得上的就支持下就好了...

    posted on 2014-12-23 00:10 順其自然EVO 閱讀(1028) 評(píng)論(0)  編輯  收藏 所屬分類: 測(cè)試學(xué)習(xí)專欄

    <2014年12月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲人成无码网站在线观看 | 国产成人在线免费观看| 免费无码又黄又爽又刺激| 成人免费视频软件网站| 四虎免费影院4hu永久免费| 一本色道久久综合亚洲精品| 亚洲成AV人片一区二区密柚| 国产v亚洲v天堂a无| 猫咪免费人成网站在线观看入口| 中文字幕免费不卡二区| 黄页网站在线观看免费高清| 亚洲AV伊人久久青青草原| 亚洲国产精品高清久久久| 亚洲午夜成人精品无码色欲| 国产男女爽爽爽免费视频| 成人黄软件网18免费下载成人黄18免费视频 | 午夜亚洲乱码伦小说区69堂| 中国黄色免费网站| 精品亚洲成a人在线观看| 国产精品视频白浆免费视频| 日韩免费高清视频| 亚洲噜噜噜噜噜影院在线播放| 亚洲伊人久久成综合人影院| 亚洲另类春色国产精品| 中国在线观看免费的www| 免费在线精品视频| 中文字幕乱码亚洲精品一区| 久久久久久国产精品免费无码| 亚洲精品国产日韩无码AV永久免费网| 亚洲国产片在线观看| 99蜜桃在线观看免费视频网站| 亚洲免费在线观看| 色屁屁www影院免费观看视频| 免费成人激情视频| 亚洲成人动漫在线观看| 24小时日本韩国高清免费| 久久亚洲AV成人无码| 99免费在线观看视频| 亚洲人成免费电影| 91香蕉视频免费| 春暖花开亚洲性无区一区二区|