如何在我没有源代码的应用程序上执行 UITesting?



我一直在阅读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 测试目标,以及如何发现程序包名称以启动应用?

谢谢!

不幸的是,我不相信这是不可能的。

您只需要捆绑标识符即可启动另一个应用程序。 https://developer.apple.com/documentation/xctest/xcuiapplication/2879415-initwithbundleidentifier?language=objc

我经常使用它进行macOS测试,所以我想它也适用于iOS。

相关内容

最新更新