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

时间:2017-08-02 19:32:03

标签: ios swift xcode9

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

谢谢!

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

我在MacOS测试中经常使用它,因此我认为它也适用于iOS。