gpt4 book ai didi

android - 使用 Espresso 进行相机操作 UI 测试

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:22 24 4
gpt4 key购买 nike

我需要自动化我的 UI 测试,以便使用 espresso 测试项目进行后续操作。

操作:

点击一个按钮打开我的手机摄像头。捕获图像,并将图像保存在 sdcard 存储中。完成后还会在屏幕上更新一个小 ImageView 。

应用程序运行良好,但对于所有其他操作和类似类型的上述操作,一次又一次地手动测试它成为一个耗时的过程。

最佳答案

我正在处理类似的问题并在下面的链接中找到了最佳可用解决方案 Camera UI test

// CameraActivityInstrumentationTest.java
public class CameraActivityInstrumentationTest {

// IntentsTestRule is an extension of ActivityTestRule. IntentsTestRule sets up Espresso-Intents
// before each Test is executed to allow stubbing and validation of intents.
@Rule
public IntentsTestRule<CameraActivity> intentsRule = new IntentsTestRule<>(CameraActivity.class);

@Test
public void validateCameraScenario() {
// Create a bitmap we can use for our simulated camera image
Bitmap icon = BitmapFactory.decodeResource(
InstrumentationRegistry.getTargetContext().getResources(),
R.mipmap.ic_launcher);

// Build a result to return from the Camera app
Intent resultData = new Intent();
resultData.putExtra("data", icon);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);

// Stub out the Camera. When an intent is sent to the Camera, this tells Espresso to respond
// with the ActivityResult we just created
intending(toPackage("com.android.camera2")).respondWith(result);

// Now that we have the stub in place, click on the button in our app that launches into the Camera
onView(withId(R.id.btnTakePicture)).perform(click());

// We can also validate that an intent resolving to the "camera" activity has been sent out by our app
intended(toPackage("com.android.camera2"));

// ... additional test steps and validation ...
}
}

关于android - 使用 Espresso 进行相机操作 UI 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28019657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com