搜尋此網誌

2014年2月7日

Java + Android + Sikuli + UIAutomator = Test automation

Project page: https://code.google.com/p/sikuli-api/

Eclipse:
  • sikuli-api-1.0.2-standalone.jar
  • slf4j-nop-1.7.5.jar
  • log4j-over-slf4j-1.7.5.jar

Example: (Java project)

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Target;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import org.sikuli.api.DesktopScreenRegion;
import org.sikuli.api.ImageTarget;
import org.sikuli.api.ScreenRegion;
import org.sikuli.api.visual.Canvas;
import org.sikuli.api.visual.DesktopCanvas;


public class SikuliTest {

    private static BufferedImage bi;
    private static JFrame frame;

    public static void main(String[] args) throws Exception {
        try {           
            Util.execCMD("adb push WiFi_UItest.jar /data/local/tmp");
            Util.execCMD("adb shell uiautomator runtest WiFi_UItest.jar -e CheckId 1 -c s2.wifi.TurnOn -c s2.wifi.CheckResult -s");
            Util.execCMD("adb pull /mnt/sdcard/Pictures/test_TurnOn.png C:\\tmp");
           
            loadImage();

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
           
            ScreenRegion s = new DesktopScreenRegion();
            Thread.sleep(3000);
            ScreenRegion result = s.find(new ImageTarget(new File("c:\\tmp\\383.jpg")));
            // System.out.println("Score: " + result.getScore());
            // System.out.println("State: " + result.getState());
           
            // Construct a Canvas object of the type DesktopCanvas
            Canvas canvas = new DesktopCanvas();
            // Add a box around a screen region 'r'
            canvas.addBox(result);
            // Add a label on the screen region r
            canvas.addLabel(result, "WE FOUND IT!!");
            // Display the canvas for 3 seconds
            canvas.display(3);
           
            // frame.dispose();
           
        } catch (IOException e) {
            // handle exception
        }
    }

    private static void loadImage() throws IOException {       
        bi = ImageIO.read(new File("C:\\tmp\\test_TurnOn.png"));
    }

    private static void createAndShowGUI() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics g2 = g.create();
                g2.drawImage(bi, 0, 0, getWidth(), getHeight(), null);
                g2.dispose();
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(bi.getWidth()/3, bi.getHeight()/3);
            }
        };

        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        // frame.setLocation(500, 20);
        frame.setVisible(true);
    }
}

沒有留言:

張貼留言