【Uiautomator】手机系统测试----图库

来源:互联网 发布:python numpy split 编辑:程序博客网 时间:2024/05/01 23:17

build.gradle 设置如下


添加库

defaultConfig {    ...    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    testCompile 'junit:junit:4.12'    compile 'com.android.support:appcompat-v7:23.2.1'    androidTestCompile 'com.android.support:support-annotations:23.2.1'    androidTestCompile 'com.android.support.test:runner:0.4.1'    // UiAutomator Testing    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'...}



GalleryViewTestCase.java
import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.content.pm.ResolveInfo;import android.os.Build;import android.support.test.InstrumentationRegistry;import android.support.test.uiautomator.By;import android.support.test.uiautomator.UiDevice;import android.support.test.uiautomator.UiObject;import android.support.test.uiautomator.UiObjectNotFoundException;import android.support.test.uiautomator.UiSelector;import android.support.test.uiautomator.Until;import com.fxly.uiautomatordemo.Strings.CommonStrings;import org.junit.Before;import org.junit.Test;import static org.hamcrest.CoreMatchers.notNullValue;import static org.junit.Assert.assertThat;/** * Created by Lambert Liu on 2016-04-05. */public class GalleryViewTestCase {    private static final String BASIC_SAMPLE_PACKAGE            = "com.android.gallery3d";    private static final int LAUNCH_TIMEOUT = 5000;    private static final String STRING_TO_BE_TYPED = "UiAutomator";    private UiDevice mDevice;    @Before    public void startMainActivityFromHomeScreen() {        // Initialize UiDevice instance        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());        // Start from the home screen        mDevice.pressHome();        // Wait for launcher        final String launcherPackage = getLauncherPackageName();        assertThat(launcherPackage, notNullValue());        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);        // Launch the blueprint app        Context context = InstrumentationRegistry.getContext();        final Intent intent = context.getPackageManager()                .getLaunchIntentForPackage(CommonStrings.GALLERY);        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances        context.startActivity(intent);        // Wait for the app to appear        mDevice.wait(Until.hasObject(By.pkg(CommonStrings.GALLERY).depth(0)), LAUNCH_TIMEOUT);    }    @Test    public void Is_No_picture() throws UiObjectNotFoundException {        UiObject nopicture=new UiObject(new UiSelector().className("android.widget.Button").text("Camera"));        if(nopicture.exists()){            nopicture.clickAndWaitForNewWindow();            Sleep(4);            UiObject shutter_picture=new UiObject(new UiSelector().descriptionStartsWith("Shutter"));            shutter_picture.click();            Sleep(5);        }else {        }        mDevice.pressBack();    }    @Test    public void Albums_types_test() throws UiObjectNotFoundException {//        android:id/action_bar_spinner        String trooper650_4G_albums_stype[]={"Albums","Locations","Times","People","Tags"};        if(Build.MODEL.equals("KAZAM Trooper 650 4G")){            UiObject albums_type=new UiObject(new UiSelector().resourceId("android:id/action_bar_spinner"));            for (int aa=0;aa<trooper650_4G_albums_stype.length;aa++){                albums_type.click();                Sleep(2);                UiObject type=new UiObject(new UiSelector().resourceId("android:id/text1").text(trooper650_4G_albums_stype[aa]));                type.click();                Sleep(5);            }        }        else {        }    }    @Test    public  void albums_del_test() throws UiObjectNotFoundException {        if(Build.MODEL.equals("KAZAM Trooper 650 4G")){            UiObject more_menu=new UiObject(new UiSelector().descriptionContains("More options"));            if (more_menu.exists()){ more_menu.click();Sleep(4);}            else {mDevice.pressMenu();Sleep(2);}            UiObject selectalbum=new UiObject(new UiSelector().text("Select album"));            if(selectalbum.exists()){                selectalbum.click();                Sleep(3);//                UiObject selected=new UiObject(new UiSelector().className("android.widget.FrameLayout").childSelector(new UiSelector().className("android.widget.ImageView")));                UiObject selected=new UiObject(new UiSelector().className("android.widget.Button"));                selected.click();                Sleep(3);                UiObject selected_all =new UiObject(new UiSelector().text("Select all"));                selected_all.click();                UiObject button_del=new UiObject(new UiSelector().resourceId("com.android.gallery3d:id/action_delete"));                button_del.click();                Sleep(3);                UiObject is_del=new UiObject(new UiSelector().text("Delete selected item?"));                if(is_del.exists()){                    UiObject ok_button=new UiObject(new UiSelector().className("android.widget.Button").text("OK"));                    ok_button.click();                    Sleep(5);                }else {                    System.out.println("");                }            }else {                System.out.println("");            }        }    }    @Test    public void takepicture() throws UiObjectNotFoundException {        if(Build.MODEL.equals("KAZAM Trooper 650 4G")){            UiObject enter_camera=new UiObject(new UiSelector().descriptionStartsWith("Switch to camera"));            enter_camera.click();            Sleep(2);            UiObject shutter_picture=new UiObject(new UiSelector().descriptionStartsWith("Shutter"));            shutter_picture.click();            Sleep(2);            UiObject switch_camera=new UiObject(new UiSelector().resourceId("com.android.camera2:id/front_back_switcher"));            switch_camera.click();            Sleep(2);            shutter_picture.click();            Sleep(2);            switch_camera.click();            Sleep(2);            shutter_picture.click();            Sleep(2);        }        else {        }        mDevice.pressBack();        Sleep(2);        mDevice.pressBack();        Sleep(2);        mDevice.pressHome();    }    @Test    public void albums_refresh() throws UiObjectNotFoundException {        if(Build.MODEL.equals("KAZAM Trooper 650 4G")){            UiObject more_menu=new UiObject(new UiSelector().descriptionContains("More options"));            if (more_menu.exists()){ more_menu.click();Sleep(4);}            else {mDevice.pressMenu();Sleep(2);}            UiObject refresh_albums=new UiObject(new UiSelector().text("Refresh"));            refresh_albums.click();            Sleep(2);        }    }    @Test    public void albums_picture() throws UiObjectNotFoundException {//        String albums_rotate[]={"Delete","Slideshow","Rotate left","Rotate right","Details","Set picture as"};        String albums_rotate[]={"Rotate left","Rotate right"};        String picture_edit[]={"Edit","Crop"};        if(Build.MODEL.equals("KAZAM Trooper 650 4G")){            UiObject enteralbum=new UiObject(new UiSelector().resourceId("com.android.gallery3d:id/gl_root_view"));            int x= enteralbum.getVisibleBounds().centerX();            int y= enteralbum.getVisibleBounds().centerY();            mDevice.click(x,y);            Sleep(2);            mDevice.click(x,y-20);            Sleep(2);            for (int e=0;e<albums_rotate.length;e++){                UiObject more_menu=new UiObject(new UiSelector().descriptionContains("More options"));                if (more_menu.exists()){ more_menu.click();Sleep(4);}                else {mDevice.pressMenu();Sleep(2);}                UiObject menu_list=new UiObject(new UiSelector().text(albums_rotate[e]));                menu_list.clickAndWaitForNewWindow();                Sleep(2);            }            //Edit            for (int e=0;e<picture_edit.length;e++){                UiObject more_menu=new UiObject(new UiSelector().descriptionContains("More options"));                if (more_menu.exists()){ more_menu.click();Sleep(4);}                else {mDevice.pressMenu();Sleep(2);}                UiObject menu_list=new UiObject(new UiSelector().text(picture_edit[e]));                menu_list.clickAndWaitForNewWindow();                Sleep(2);                mDevice.pressBack();                Sleep(2);            }            //Del            UiObject more_menu=new UiObject(new UiSelector().descriptionContains("More options"));            if (more_menu.exists()){ more_menu.click();Sleep(4);}            else {mDevice.pressMenu();Sleep(2);}            UiObject menu_del=new UiObject(new UiSelector().text("Delete"));            menu_del.click();            Sleep(2);            UiObject is_del=new UiObject(new UiSelector().text("Delete selected item?"));            if(is_del.exists()){                UiObject ok_bnt=new UiObject(new UiSelector().text("OK"));                ok_bnt.clickAndWaitForNewWindow();                Sleep(2);            }            //Slideshow            if (more_menu.exists()){ more_menu.click();Sleep(4);}            else {mDevice.pressMenu();Sleep(2);}            UiObject menu_slideshow=new UiObject(new UiSelector().text("Slideshow"));            menu_slideshow.click();            Sleep(10);            mDevice.pressBack();            Sleep(2);            //Details            if (more_menu.exists()){ more_menu.click();Sleep(4);}            else {mDevice.pressMenu();Sleep(2);}            UiObject menu_details=new UiObject(new UiSelector().text("Details"));            menu_details.click();            Sleep(2);            mDevice.pressBack();            Sleep(2);            //Set picture as            if (more_menu.exists()){ more_menu.click();Sleep(4);}            else {mDevice.pressMenu();Sleep(2);}            UiObject menu_set_as=new UiObject(new UiSelector().text("Set picture as"));            menu_set_as.click();            Sleep(2);            //Wallpaper            UiObject set_as_wallpaper=new UiObject(new UiSelector().text("Wallpaper"));            set_as_wallpaper.clickAndWaitForNewWindow();            Sleep(2);            //Set wallpaper            UiObject set_as_wallpaper_ok=new UiObject(new UiSelector().text("Set wallpaper"));            set_as_wallpaper_ok.click();            Sleep(2);//         Set picture as   Contact photo        }    }    /**     * Uses package manager to find the package name of the device launcher. Usually this package     * is "com.android.launcher" but can be different at times. This is a generic solution which     * works on all platforms.`     */    private String getLauncherPackageName() {        // Create launcher Intent        final Intent intent = new Intent(Intent.ACTION_MAIN);        intent.addCategory(Intent.CATEGORY_HOME);        // Use PackageManager to get the launcher package name        PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();        ResolveInfo resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);        return resolveInfo.activityInfo.packageName;    }    private void Sleep(int i) {        try {            Thread.sleep(i*1000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }    private int gettestcount() {        int loop=5;        return loop;    }}



0 1