執行遊戲迴圈測試

如果遊戲應用程式是採用不同的 UI 架構建構,自動化遊戲測試可能會很困難。透過遊戲迴圈測試,您可以將原生測試與 Test Lab 整合,並在選取的裝置上輕鬆執行測試。本指南說明如何準備使用 Firebase Test Lab 執行的遊戲迴圈測試。

關於遊戲迴圈測試

遊戲迴圈測試會模擬真實玩家的動作,以快速且可擴充的方式,驗證遊戲是否能為使用者提供良好的效能體驗。迴圈是指在遊戲應用程式中完整或部分執行測試。您可以在模擬器上或 Test Lab 中的一組裝置上,在本機執行遊戲迴圈測試。遊戲迴圈測試可用於:

  • 以使用者身分執行遊戲。您可以編寫使用者輸入內容的指令碼、讓使用者處於閒置狀態,或是以 AI 取代使用者 (舉例來說,如果您在賽車遊戲中導入 AI,可以讓 AI 駕駛人負責使用者的輸入內容)。
  • 將遊戲畫質調到最高,看看哪些裝置可以支援。
  • 執行技術測試,例如編譯多個著色器、執行這些著色器,並檢查輸出內容是否符合預期。

步驟 1:註冊 Test Lab 的自訂網址配置

  1. 在 Xcode 中選取專案目標。

  2. 按一下「資訊」分頁,然後新增網址類型

  3. 在「URL Schemes」(網址架構) 欄位中輸入 firebase-game-loop。 您也可以將自訂網址架構新增至專案的 Info.plist 設定檔 (位於 <dict> 標記內的任何位置),藉此註冊自訂網址架構:

    <key>CFBundleURLTypes</key>
     <array>
         <dict>
             <key>CFBundleURLName</key>
             <string></string>
             <key>CFBundleTypeRole</key>
             <string>Editor</string>
             <key>CFBundleURLSchemes</key>
             <array>
                 <string>firebase-game-loop</string>
             </array>
         </dict>
     </array>
    

應用程式現已設定完畢,可使用 Test Lab 執行測試。

步驟 2:視需要設定應用程式

執行多個迴圈

如果您打算在測試中執行多個迴圈 (又稱情境),則必須在啟動應用程式時指定要執行的迴圈。

在應用程式委派中,覆寫 application(_:open:options:) 方法:

SwiftObjective-C
func application(_app: UIApplication,
                 open url: URL
                 options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let components = URLComponents(url: url, resolvingAgainstBaseURL: true)!
    if components.scheme == "firebase-game-loop" {
        // ...Enter Game Loop Test logic to override application(_:open:options:).
    }
    return true
}
- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary &lt;UIApplicationOpenURLOptionsKey, id&gt; *)options {
  if ([url.scheme isEqualToString:(@"firebase-game-loop")]) {
      // ...Enter Game Loop Test logic to override application(_:open:options:).
  }
}

在測試中執行多個迴圈時,系統會將目前的迴圈當做參數傳遞至用於啟動應用程式的網址。您也可以剖析用於擷取自訂網址配置的 URLComponents 物件,取得目前的迴圈編號:

SwiftObjective-C
if components.scheme == "firebase-game-loop" {
    // Iterate over all parameters and find the one with the key "scenario".
    let scenarioNum = Int(components.queryItems!.first(where: { $0.name == "scenario" })!.value!)!
    // ...Write logic specific to the current loop (scenarioNum).
}
if ([url.scheme isEqualToString:(@"firebase-game-loop")]) {
    // Launch the app as part of a game loop.
    NSURLComponents *components = [NSURLComponents componentsWithURL:url
                                             resolvingAgainstBaseURL:YES];
    for (NSURLQueryItem *item in [components queryItems]) {
        if ([item.name isEqualToString:@"scenario"]) {
            NSInteger scenarioNum = [item.value integerValue];
            // ...Write logic specific to the current loop (scenarioNum).
        }
    }
}

提早結束測試

根據預設,即使所有迴圈都已執行完畢,遊戲迴圈測試仍會持續執行,直到達到五分鐘的逾時時間為止。達到逾時時間後,測試就會結束,並取消所有待處理的迴圈。如要加快測試速度或提早結束測試,請在應用程式的 AppDelegate 中呼叫Test Lab的自訂網址架構firebase-game-loop-complete。例如:

SwiftObjective-C
/// End the loop by calling our custom url scheme.
func finishLoop() {
    let url = URL(string: "firebase-game-loop-complete://")!
    UIApplication.shared.open(url)
}
- (void)finishLoop {
  UIApplication *app = [UIApplication sharedApplication];
  [app openURL:[NSURL URLWithString:@"firebase-game-loop-complete://"]
      options:@{}
completionHandler:^(BOOL success) {}];
}

遊戲迴圈測試會終止目前的迴圈,並執行下一個迴圈。 如果沒有要執行的迴圈,測試就會結束。

撰寫自訂測試結果

您可以設定遊戲迴圈測試,將自訂測試結果寫入裝置的檔案系統。這樣一來,測試開始執行時,Test Lab 會將結果檔案儲存在測試裝置的 GameLoopsResults 目錄中 (您必須自行建立該目錄)。測試結束時,Test Lab 會將 GameLoopResults 目錄中的所有檔案移至專案的 bucket。設定測試時,請注意下列事項:

  • 無論檔案類型、大小或數量為何,所有結果檔案都會上傳。

  • Test Lab 會等到測試中的所有迴圈都執行完畢,才會處理測試結果。因此,如果測試包含多個會寫入輸出的迴圈,請務必將輸出內容附加至不重複的結果檔案,或為每個迴圈建立結果檔案。這樣就能避免覆寫先前迴圈的結果。

如要設定測試來編寫自訂測試結果,請按照下列步驟操作:

  1. 在應用程式的 Documents 目錄中,建立名為 GameLoopResults 的目錄。

  2. 在應用程式程式碼中的任意位置 (例如應用程式委派),加入下列內容:

    SwiftObjective-C
    /// Write to a results file.
    func writeResults() {
      let text = "Greetings from game loops!"
      let fileName = "results.txt"
      let fileManager = FileManager.default
      do {
    
      let docs = try fileManager.url(for: .documentDirectory,
                                     in: .userDomainMask,
                                     appropriateFor: nil,
                                     create: true)
      let resultsDir = docs.appendingPathComponent("GameLoopResults")
      try fileManager.createDirectory(
          at: resultsDir,
          withIntermediateDirectories: true,
          attributes: nil)
      let fileURL = resultsDir.appendingPathComponent(fileName)
      try text.write(to: fileURL, atomically: false, encoding: .utf8)
      } catch {
        // ...Handle error writing to file.
      }
    }
    
    /// Write to a results file.
    - (void)writeResults:(NSString *)message {
        // Locate and create the results directory (if it doesn't exist already).
        NSFileManager *manager = [NSFileManager defaultManager];
        NSURL* url = [[manager URLsForDirectory:NSDocumentDirectory
                                      inDomains:NSUserDomainMask] lastObject];
        NSURL* resultsDir = [url URLByAppendingPathComponent:@"GameLoopResults"
                                                 isDirectory:YES];
        [manager createDirectoryAtURL:resultsDir
          withIntermediateDirectories:NO
                           attributes:nil
                                error:nil];
    
        // Write the result message to a text file.
        NSURL* resultFile = [resultsDir URLByAppendingPathComponent:@"result.txt"];
        if ([manager fileExistsAtPath:[resultFile path]]) {
            // Append to the existing file
            NSFileHandle *handle = [NSFileHandle fileHandleForWritingToURL:resultFile
                                                                     error:nil];
            [handle seekToEndOfFile];
            [handle writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
            [handle closeFile];
        } else {
            // Create and write to the file.
            [message writeToURL:resultFile
                     atomically:NO
                       encoding:NSUTF8StringEncoding error:nil];
        }
    }
    

步驟 3:簽署應用程式

  1. 請確認應用程式中的所有構件都已簽署。舉例來說,您可以在 Xcode 中指定佈建設定和身分等簽署設定,詳情請參閱「Apple 程式碼簽署」。

步驟 4:將應用程式封裝以供上傳

產生應用程式的 IPA 檔案 (稍後需要找到該檔案)。

  1. 在隨即顯示的下拉式選單中,依序按一下「產品」>「封存」。 選取最新的封存檔,然後按一下「發布應用程式」

  2. 在隨即顯示的視窗中,依序點選「開發」>「下一步」

  3. 選用:如要加快建構速度,請取消選取「從 Bitcode 重建」選項,然後按一下「下一步」Test Lab 不需要縮減或重建應用程式即可執行測試,因此您可以放心停用這個選項。

  4. 按一下「匯出」,然後輸入要下載應用程式 IPA 檔案的目錄。

步驟 5:驗證應用程式簽章

  1. 如要驗證應用程式簽章,請解壓縮 .ipa 檔案,然後執行 codesign --verify --deep --verbose /path/to/MyApp.app,其中「MyApp」是解壓縮資料夾中的應用程式名稱 (每個專案都不盡相同)。預期的輸出內容為 MyApp.app: valid on disk

步驟 6:在本地執行測試

您可以在本機執行測試,檢查測試行為,然後再使用 Test Lab 執行測試。如要在本機測試,請在模擬器中載入遊戲應用程式,然後執行下列指令:

xcrun simctl openurl SIMULATOR_UDID firebase-game-loop://
  • 執行 instruments -s devices 指令,即可找出模擬器的 UDID。

  • 如果只有一個模擬器正在執行,請輸入特殊字串 "booted",取代 SIMULATOR_UDID

如果測試包含多個迴圈,您可以將迴圈編號傳遞至 scenario 標記,指定要執行的迴圈。請注意,在本機執行測試時,一次只能執行一個迴圈。舉例來說,如要執行迴圈 1、2 和 5,必須為每個迴圈執行個別指令:

xcrun simctl openurl SIMULATOR_UDID firebase-game-loop://?scenario=1
xcrun simctl openurl SIMULATOR_UDID firebase-game-loop://?scenario=2
xcrun simctl openurl SIMULATOR_UDID firebase-game-loop://?scenario=5

後續步驟

使用 Firebase 控制台gcloud CLI 執行測試。