0

我正在为我的应用程序写一个Espresso测试,并试图在我的应用程序中打开相机后自动点击快门按钮。点击使用UIAutomator的相机快门

我在Android模拟器中使用Espresso和UIAutomator。我设法在UIAutomatorViewer中转储这个UI。 UIAutomatorViewer

我想不通为什么我无法点击使用UIAutomator使用此代码快门按钮:

public void clickCameraShutterButton() throws UiObjectNotFoundException 
{ 
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 
    UiSelector shutterSelector = new UiSelector().resourceId("com.android.camera:id/shutter_button"); 
    UiObject shutterButton = device.findObject(shutterSelector); 
    shutterButton.click(); 
} 

相机只是坐在那里,和快门键被按下从来没有。这是我在Android Studio显示器中获得的堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference 

任何意见,将不胜感激。

回答

1

这为我工作

@Before 
public void setUp() { 
    // Initialize UiDevice instance 
    final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); 
    mDevice = UiDevice.getInstance(instrumentation); 
} 

... 


/** 
* @@Test comment [email protected]@ 
* 
* @throws Exception 
*/ 
@Test 
public void culebraGeneratedTest_CameraShutter() throws Exception { 
    mDevice.findObject(By.res("com.android.camera2:id/shutter_button").desc("Shutter").clazz("android.widget.ImageView").text(Pattern.compile("")).pkg("com.android.camera2")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT); 
} 

这个测试发现快门并点击它。

如果您有兴趣使用CulebraTester自动生成此测试。

0

你可以试试这个代码:

device.findObject(new UiSelector().resourceId("com.android.camera:id/shutter_button")).click(); 

device.findObject(new UiSelector().description("Shutter button")).click(); 

device.executeShellCommand("input keyevent 27"); 

这意味着KEYCODE_CAMERA值是27

device.click(x,y);