Xamarin.UITest 速查表

重要

Visual Studio App Center 已排定於 2025 年 3 月 31 日淘汰。 雖然您可以繼續使用 Visual Studio App Center,直到它完全淘汰為止,但有數個建議您考慮移轉至的建議替代方案。

深入瞭解支持時程表和替代方案。

本檔是一份速查表,會壓縮一些 UITest 資訊以供快速參考,其中包含下列主題:

撰寫測試

此代碼段是單一平台上測試類別的重複使用 TestFixture

using System;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Android;
using Xamarin.UITest.Queries;

namespace MyApp.MyUITests
{
    [TestFixture]
    public class Tests
    {
        IApp app;

        [SetUp]
        public void BeforeEachTest()
        {
            // Uncomment the line that's appropriate for the platform:
            // app = ConfigureApp.Android.StartApp();
            // app = ConfigureApp.iOS.StartApp();
        }

        // Test cases here
    }
}

針對涉及 Android 和 iOS 的解決方案,下列程式代碼將協助您撰寫跨平臺 UITests。

using System;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Queries;

namespace MyApp.MyCrossPlatformUITests
{

    public class AppInitializer
    {
        public static IApp StartApp(Platform platform)
        {
            if(platform == Platform.Android)
            {
                return ConfigureApp.Android.StartApp();
            }
            return ConfigureApp.iOS.StartApp();
        }
    }

    [TestFixture(Platform.Android)]
    [TestFixture(Platform.iOS)]
    public class Tests
    {
        IApp app;
        Platform platform;

        public Tests(Platform platform)
        {
            this.platform = platform;
        }

        [SetUp]
        public void BeforeEachTest()
        {
            app = AppInitializer.StartApp(platform);
        }
    }
}

Xamarin.Forms 解決方案應遵循使用 Xamarin.UITest 和 App Center 自動化 Xamarin.Forms 測試指南中所述的指示。

在 iOS 上初始化 Xamarin.UITest

將下列代碼段新增至 FinishedLaunchingAppDelegate 類別的 方法:

#region Code for starting up the Xamarin Test Cloud Agent

// Newer version of Visual Studio for Mac and Visual Studio provide the
// ENABLE_TEST_CLOUD compiler directive to prevent the Calabash DLL from
// being included in the released version of the application.
#if ENABLE_TEST_CLOUD
Xamarin.Calabash.Start();
#endif
#endregion

Xamarin Test Cloud Agent 使用非公用 Apple API,這會導致應用程式遭到 App Store 拒絕。 Xamarin.iOS 連結器會在程式代碼未明確參考任何位置時,從最終 IPA 移除 Xamarin Test Cloud Agent。 發行組建沒有 ENABLE_TEST_CLOUD 編譯程式變數,這會導致 Xamarin Test Cloud Agent 從應用程式套件組合中移除。 偵錯組建已定義編譯程式指示詞,以防止連結器移除 Xamarin Test Cloud Agent。

判斷 iOS 模擬器的裝置標識碼

您可以在電腦上判斷 iOS 模擬器的 UUID,請使用 instruments 命令,如下所示:

$ xcrun xctrace list devices
Known Devices:
bushmaster [5A4B28A1-392A-59FB-81C5-137E881D61E9]
Resizable iPad (8.1 Simulator) [B3BF8A06-2938-4B74-BF87-16C223F8690C]
Resizable iPhone (8.1 Simulator) [E712409B-CFCC-409A-8162-627B6254EB3C]
iPad 2 (7.1 Simulator) [E8572F8F-227B-4DB0-8C92-590DC770360D]
iPad 2 (8.1 Simulator) [1F425263-3F96-4DAB-B843-0D041C3C71EA]
iPad Air (7.1 Simulator) [2863AFF6-D9FC-45E8-8385-E2A548F19002]
iPad Air (8.1 Simulator) [BBCF5CF2-20A4-4C47-9FA5-EBFF7311B071]
iPad Retina (7.1 Simulator) [B7CBB024-E1D3-4B24-8C20-3E9F7B54CF61]
iPad Retina (8.1 Simulator) [3E21ECD3-397A-4251-AEB6-2ADCF29AEE89]
iPhone 4s (7.1 Simulator) [D36354DD-D6A3-4E08-A25B-276620D844B8]
iPhone 4s (8.1 Simulator) [5C8FE602-8BA7-494D-A113-66C8B9AB3CB7]
iPhone 5 (7.1 Simulator) [C696E83D-F9FE-4DBC-8C67-FA0FC533246E]
iPhone 5 (8.1 Simulator) [9A8A5D92-A7D9-4A3C-81AA-97A9924F7D09]
iPhone 5s (7.1 Simulator) [6CDF5B5C-A315-4A8C-9D38-29437FE59C6D]
iPhone 5s (8.1 Simulator) [3F1C286F-3D5D-47B2-92B8-66B673BD0236]
iPhone 6 (8.1 Simulator) [995FF713-9DE4-460B-800E-F5A20FD93AA7]
iPhone 6 Plus (8.1 Simulator) [AB1C20F6-BFFC-4C80-879C-F19A7E3F0B5C]

啟動 iOS 模擬器實例

您可以使用裝置識別碼,在特定 iOS 版本和模擬器上執行 UITests。

const string simId = "3F1C286F-3D5D-47B2-92B8-66B673BD0236"; //iPhone 5s (8.1 Simulator)
app = ConfigureApp.iOS.DeviceIdentifier(simId).StartApp();

將 iOS 模擬器重設為原廠預設值

此代碼段可用來停止指定的 iOS 模擬器,並將它重設為原廠預設值:

static void ResetSimulator(string deviceId)
{
    var shutdownCmdLine = string.Format("simctl shutdown {0}", deviceId);
    var shutdownProcess = Process.Start("xcrun", shutdownCmdLine);
    shutdownProcess.WaitForExit();

    var eraseCmdLine = string.Format("simctl erase {0}", deviceId);
    var eraseProcess = Process.Start("xcrun", eraseCmdLine);
    eraseProcess.WaitForExit();
}

程式碼片段

本節將提供一些代碼段,有助於撰寫 UITests。

根據屬性值查詢專案

//Finds all elements that have a "hint" property with a value of "Search"
app.Query(e => e.All().Property("hint", "Search"));

在本機啟用螢幕快照

app = ConfigureApp.Android
    .EnableLocalScreenshots()
    .StartApp();

此程式代碼範例會將螢幕快照放在測試元件所在的目錄中,將影像 screenshot-X-png命名為 。

在 AppResult 或 UI 元素上叫用方法

使用方法可以在基礎檢視 AppQuery.Invoke 上執行原生方法。 叫用的方法必須符合原生方法名稱,而不是 C# 方法名稱。 例如,若要在AndroidTextView上叫用 setGravity 方法:

app.Query(e => e.Id("userName").Invoke("setGravity", 1)); //center text

使用 Invoke Java TextView.setGravity 方法,而不是 C# TextView.Gravity 屬性。

處理Android許可權

ConfigureApp.Android.Debug().ApkFile(apkpath).StartApp()

如果您使用 而非PreferIdeSettings()安裝 .ApkFile(apkpath) ,則會將應用程式授與「全部」許可權,以移除許可權彈出視窗。 在方法中 .ApkFile(apkpath)apkpath 必須指向已編譯的 apk 檔案。