我一直在阅读 Apple 新的和改进的 XCTest/UITesting 框架,作为一名 Android 开发人员,我有一些问题。在 android 中,要启动一个我没有运行 UiAutomator 的源代码的应用程序,我只需使用
@Before
public void setup() {
//Init UiDevice instance
Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
mDevice = UiDevice.getInstance(mInstrumentation);
//start from home screen on each new test
mDevice.pressHome();
//wait for launcher
final String launcherPackage = mDevice.getLauncherPackageName();
assertThat(launcherPackage, notNullValue());
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
//launch app
Context mContext = InstrumentationRegistry.getContext();
final Intent intent = mContext.getPackageManager()
.getLaunchIntentForPackage(GMAIL_APP_PACKAGE);
//Clear any previous instances
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
mContext.startActivity(intent);
//wait for app to appear
mDevice.wait(Until.hasObject(By.pkg(GMAIL_APP_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
mDevice.waitForWindowUpdate(null, 5000);
}
使用上面的代码,我可以启动我想要的包,然后使用 UiAutomatorView.bat 检查元素并获取它们的资源 ID(或通过文本查找),然后对它们执行操作。
在 iOS 中,我发现的所有教程都集中在测试您可以访问其源代码的应用程序上。虽然我知道在 iOS 中编写这些测试要简单得多,因为您只需记录您希望执行的操作,xcode 会为您编写代码,但我不确定如何将 UI 测试目标设置为当前正在运行的应用程序安装在我的设备上。我观看了 WWDC 视频,该视频使用以下内容进行了多应用测试:
var gmailApp: XCUIApplication!
gmailApp.launchArguments = "-StartFromCleanState", "YES"]
gmailApp.launch()
但是这不起作用,因为它没有目标。
TL;DR:如何将我的设备上安装的应用程序(例如 gmail)设置为我的 UI 测试目标,以及如何发现包名称以启动应用程序?
谢谢!