您的位置:首页 > 产品设计 > UI/UE

使用Android UiAutomator解锁手势密码

2016-09-13 18:59 375 查看
package com.jane.demofortest;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Point;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until;
import static org.hamcrest.CoreMatchers.notNullValue;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertThat;

/**
* Created by jane on 16/9/13.
*/

@RunWith(AndroidJUnit4.class)
public class UnitTestDemo {

private static final String BASIC_SAMPLE_PACKAGE = "com.jane.example";
private static final int LAUNCH_TIMEOUT = 10000;

UiDevice mDevice;

@Before
public void setUp() throws Exception{
// 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(BASIC_SAMPLE_PACKAGE);
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(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT);

Thread.sleep(LAUNCH_TIMEOUT);
}

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;
}

@Test
public void test_login(){
System.out.println("Test Begin");
mDevice.swipe(new Point[]{new Point(190,659),new Point(547,655),new Point(547,1018),new Point(540,1370),new Point(900,1359)},6);//这段是解锁手势密码
}
}


不知道为什么:mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT);

这里会报空指针的错误,有大神给指点下吗?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 测试