关于使用Firebase Test Lab对基于Flutter InAppWebView开发的Android应用进行Instrumentation测试的技术问询
关于使用Firebase Test Lab对基于Flutter InAppWebView开发的Android应用进行Instrumentation测试的技术问询
当然可以!Firebase Test Lab完全支持对集成了Flutter InAppWebView的Android应用进行Instrumentation测试,你提到的填充输入框、点击按钮、验证下载和跳转URL这些核心场景都能完美覆盖。下面我会结合具体场景拆解实现流程和示例:
一、前期准备工作
- 配置Android端测试环境:确保你的Flutter项目的
android/app/src/androidTest目录结构完整,已添加Android Instrumentation测试的基础依赖(如androidx.test.ext:junit:1.1.5、androidx.test.espresso:espresso-core:3.5.1) - 添加Espresso Web依赖:因为要和InAppWebView内的Web元素交互,需要在
android/app/build.gradle的androidTestImplementation中加入:androidTestImplementation "androidx.test.espresso:espresso-web:3.5.1" - 开启WebView调试:在Flutter代码中初始化InAppWebView时,开启Web内容调试,方便测试工具定位Web元素:
InAppWebView( initialUrlRequest: URLRequest(url: Uri.parse("https://your-web-url.com")), initialOptions: InAppWebViewGroupOptions( android: AndroidInAppWebViewOptions( webContentsDebuggingEnabled: true, javascriptEnabled: true, domStorageEnabled: true ) ), )
二、编写Instrumentation测试用例
以下示例使用Java编写Android端Instrumentation测试,覆盖你提到的所有场景:
1. 定位InAppWebView并等待页面加载
首先需要等待WebView加载完成,确保Web元素可交互:
@Rule public ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class); @Test public void testInAppWebViewFlow() { // 等待WebView加载完成 onWebView() .withElement(findElement(Locator.TAG_NAME, "body")) .check(WebViewAssertions.webMatches(getText(), not(isEmptyString()))); }
2. 填充输入框与点击按钮
通过Espresso Web提供的API操作WebView内的表单元素:
// 填充用户名输入框 onWebView() .withElement(findElement(Locator.ID, "username-input")) .perform(DriverAtoms.webKeys("test-user-123")); // 填充密码输入框 onWebView() .withElement(findElement(Locator.NAME, "password")) .perform(DriverAtoms.webKeys("test-pass-456")); // 点击提交按钮 onWebView() .withElement(findElement(Locator.CSS_SELECTOR, ".submit-button")) .perform(DriverAtoms.webClick());
3. 验证跳转URL
断言WebView跳转到了预期的目标页面:
// 等待页面跳转完成后验证URL onWebView() .check(WebViewAssertions.webMatches( getCurrentUrl(), containsString("https://your-expected-landing-url.com/success") )); // 或者通过JavaScript获取当前URL String currentUrl = onWebView().evaluateJavascript("window.location.href", String.class); assertEquals("\"https://your-expected-landing-url.com/success\"", currentUrl);
4. 验证下载操作
检查设备下载目录中是否存在目标文件:
@Test public void testFileDownload() { // 触发下载操作(比如点击下载按钮) onWebView() .withElement(findElement(Locator.LINK_TEXT, "Download Report")) .perform(DriverAtoms.webClick()); // 等待下载完成(根据文件大小调整等待时间) SystemClock.sleep(5000); // 检查下载目录中的文件是否存在 Context context = InstrumentationRegistry.getInstrumentation().getContext(); File downloadDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS); File downloadedFile = new File(downloadDir, "report.pdf"); assertTrue("Downloaded file should exist", downloadedFile.exists()); assertTrue("File should have valid size", downloadedFile.length() > 0); }
三、配置并运行Firebase Test Lab测试
- 构建测试APK与应用APK:
- 构建应用的debug APK:
flutter build apk --debug - 构建Android测试APK:在Android项目根目录执行
./gradlew assembleDebug assembleAndroidTest
- 构建应用的debug APK:
- 上传到Firebase Test Lab:
- 打开Firebase控制台,进入Test Lab模块,选择你的Android应用
- 上传构建好的应用APK(
app-debug.apk)和测试APK(app-debug-androidTest.apk)
- 配置测试设备:
- 选择需要测试的设备型号、Android版本组合,配置测试运行的超时时间(建议设置为10-15分钟,覆盖下载操作的等待时间)
- 查看测试结果:
- 测试完成后,Firebase会生成详细报告,包括每个测试用例的执行状态、设备截图、Logcat日志以及下载验证的结果
- 可以通过报告中的失败截图和日志快速定位问题(比如Web元素未找到、下载超时等)
四、注意事项
- 处理异步操作:WebView加载、文件下载都是异步操作,测试中要避免硬编码等待时间,推荐使用
IdlingResource来等待异步任务完成,提升测试稳定性 - 权限配置:确保应用在测试Manifest中声明了必要的权限,如
INTERNET、WRITE_EXTERNAL_STORAGE(针对Android 10及以下),Android 11+使用Scoped Storage时无需额外权限 - Flutter与原生交互:如果InAppWebView有Flutter和Web的双向通信逻辑,测试时要确保这些交互不会阻塞测试流程,可在测试代码中模拟相应的回调
内容来源于stack exchange
相关产品推荐
相关产品推荐

