顯示具有 java 標籤的文章。 顯示所有文章
顯示具有 java 標籤的文章。 顯示所有文章

2014年11月19日 星期三

[Appium] 使用一個總設定檔管理全部script的設定

script跟appium server溝通主要有兩個重點:

1.Capabilities
2.ip

如果把這兩個變數都寫死在全部的腳本裡面,會造成想要修改參數時非常麻煩
例如:你有50個script,當想要修改ip的時候得把50個script檔案都打開來修改...

可以寫一個總設定檔,把設定寫成function在裡面,當腳本需要連線時呼叫函數回傳設定

範例:


總設定檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class AllSettings {
 

 public static DesiredCapabilities Set(){
  //set all Capability
  DesiredCapabilities set = new DesiredCapabilities();
  set.setCapability("appium-version", "1.3.1");
  set.setCapability("device", "iOS Simulator");
  set.setCapability("deviceName", "iPad Air");
  set.setCapability("platformName", "iOS");
  set.setCapability("platformVersion", "8.1");
  set.setCapability("showSimulatorLog", "true");
  set.setCapability("app","/Users/blabla/blabla");
  return set;
 }

 public static String URL(){
  return "http://127.0.0.1:4725/wd/hub";
 }
}

腳本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import AllSettings;

public class sample {
 AppiumDriver wd = null;

 @Test
 public void test() throws MalformedURLException, InterruptedException {
  wd = new AppiumDriver(new URL(AllSettings.URL()),AllSettings.Set());
  wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  
 }
 
}


這樣對於管理全部的腳本非常方便,也不用每個腳本都去確認設定是否正確




2014年11月16日 星期日

[Java] 圖片像素比對

public static void ImageCompare(String sample, String file)
            throws IOException {
        double total, result = 0;
        int record = 0;

        // ----open file----
        //開啟檔案
        BufferedImage image1 = ImageIO.read(new File(
                "/Users/zhangnaiyuan/Documents/screenshot/" + sample));
        BufferedImage image2 = ImageIO.read(new File(
                "/Users/zhangnaiyuan/Documents/screenshot/" + file));
        // -----------------

        // ----pixel array-----
        //取得圖片的長寬,C是暫存
        // A=sample, B=screenshot, C=result
        int[][] imageA = new int[image1.getWidth()][image1.getHeight()];
        int[][] imageB = new int[image1.getWidth()][image1.getHeight()];
        int[][] imageC = new int[image1.getWidth()][image1.getHeight()];
        // --------------------

        // -----save all sample's pixel to array----
        //把每個像素存進陣列
        for (int x = 0; x < image1.getWidth(); x++) {
            for (int y = 0; y < image1.getHeight(); y++) {
                imageA[x][y] = image1.getRGB(x, y);
            }
        }
        // -----------------------------------------

        // -----save all screenshot's pixel to array----
        //把每個像素存進陣列
        for (int x = 0; x < image2.getWidth(); x++) {
            for (int y = 0; y < image2.getHeight(); y++) {
                imageB[x][y] = image2.getRGB(x, y);
            }
        }
        // -----------------------------------------------

        // ----find the different of 2 images-----
        //比較AB兩陣列,將結果存進C陣列,如果一樣相減會是0
        for (int x = 0; x < image1.getWidth(); x++) {
            for (int y = 0; y < image1.getHeight(); y++) {
                imageC[x][y] = imageA[x][y] - imageB[x][y];
            }
        }
        // ---------------------------------------

        // ----record the different's number----
        //算出結果C陣列有幾個不同的像素(結果不是0)
        for (int x = 0; x < image1.getWidth(); x++) {
            for (int y = 0; y < image1.getHeight(); y++) {
                if (imageC[x][y] != 0)
                    record += 1;
            }
        }
        // --------------------------------------

        // count result
        total = (image1.getWidth()) * (image1.getHeight());
        result = (record / total) * 100;
        // output the result
        System.out.println("The different of 2 images is: " + result + "%");
    }