Xamarin.UITest 2.2.4 启动会话在第一次测试中失败

Posted

技术标签:

【中文标题】Xamarin.UITest 2.2.4 启动会话在第一次测试中失败【英文标题】:Xamarin.UITest 2.2.4 start session failed on the first test 【发布时间】:2018-04-25 14:13:35 【问题描述】:

由于我的 macOS 让我更新到 XCode 9.3,我不得不将 Xamarin.UITest 从 2.2.2 升级到 2.2.4。

问题是,当我运行所有测试时,每次第一个测试都失败并附加错误。我认为这不是测试问题,因为所有测试都可以在带有 XCode 9.2 和 Xamarin.UITest 2.2.2 的 macOS 上正确运行。此外,如果我删除该测试,那么下一个将成为第一个测试的测试将失败并出现相同的错误。

谁能帮帮我,好吗?提前非常感谢您!

1) SetUp Error : UI.Tests.TestDashboardDetailsPage(ios)
Xamarin.UITest.XDB.Exceptions.DeviceAgentException : DeviceAgent start session failed: An error occurred while sending the request
  ----> System.Net.Http.HttpRequestException : An error occurred while sending the request
  ----> System.Net.WebException : Error: ConnectFailure (Connection refused)
  ----> System.Net.Sockets.SocketException : Connection refused
  at Xamarin.UITest.XDB.Services.iOSDeviceAgentService+<RequestAsync>c__async14`1[T].MoveNext () [0x0010e] in <1bd397951f434cd4913ddc08b7bc45af>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at Xamarin.UITest.XDB.Services.iOSDeviceAgentService+<StartAppAsync>c__asyncE.MoveNext () [0x000ba] in <1bd397951f434cd4913ddc08b7bc45af>:0 
--- End of stack trace from previous location where exception was thrown ---
  at Xamarin.UITest.iOS.iOSAppLauncher.LaunchAppLocal (Xamarin.UITest.Configuration.IiOSAppConfiguration appConfiguration, Xamarin.UITest.Shared.Http.HttpClient httpClient, System.Boolean clearAppData) [0x00216] in <1bd397951f434cd4913ddc08b7bc45af>:0 
  at Xamarin.UITest.iOS.iOSAppLauncher.LaunchApp (Xamarin.UITest.Configuration.IiOSAppConfiguration appConfiguration, Xamarin.UITest.Shared.Http.HttpClient httpClient, Xamarin.UITest.TestCloud.TestCloudiOSAppConfiguration testCloudAppConfiguration, Xamarin.UITest.Shared.Http.HttpClient testCloudWsClient, Xamarin.UITest.Shared.Http.HttpClient xtcServicesClient, System.Boolean testCloudUseDeviceAgent) [0x0007a] in <1bd397951f434cd4913ddc08b7bc45af>:0 
  at Xamarin.UITest.iOS.iOSApp..ctor (Xamarin.UITest.Configuration.IiOSAppConfiguration appConfiguration, Xamarin.UITest.Shared.Execution.IExecutor executor) [0x002de] in <1bd397951f434cd4913ddc08b7bc45af>:0 
  at Xamarin.UITest.iOS.iOSApp..ctor (Xamarin.UITest.Configuration.IiOSAppConfiguration appConfiguration) [0x00000] in <1bd397951f434cd4913ddc08b7bc45af>:0 
  at Xamarin.UITest.Configuration.iOSAppConfigurator.StartApp (Xamarin.UITest.Configuration.AppDataMode appDataMode) [0x00017] in <1bd397951f434cd4913ddc08b7bc45af>:0 
  at UI.Tests.AppInitializer.StartApp (Xamarin.UITest.Platform platform) [0x00041] in <2cd1cd61003a486e92c6ffb9d58f1488>:0 
  at UI.Tests.TestDashboardDetailsPage.OneTimeSetUp () [0x00000] in <2cd1cd61003a486e92c6ffb9d58f1488>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
--HttpRequestException
  at System.Net.Http.HttpClientHandler+<SendAsync>d__63.MoveNext () [0x004ab] in <7e581108ecb143d78ca072a020c287c3>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Net.Http.HttpClient+<SendAsyncWorker>d__47.MoveNext () [0x000ca] in <7e581108ecb143d78ca072a020c287c3>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at Xamarin.UITest.XDB.Services.HttpService+<PostAsJsonAsync>c__async3`1+<PostAsJsonAsync>c__AnonStorey11+<PostAsJsonAsync>c__async10[T].MoveNext () [0x00094] in <1bd397951f434cd4913ddc08b7bc45af>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at Xamarin.UITest.XDB.Services.HttpService+<ExecuteRequestAsync>c__async5.MoveNext () [0x0008b] in <1bd397951f434cd4913ddc08b7bc45af>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at Xamarin.UITest.XDB.Services.HttpService+<RequestAsync>c__async4`1[T].MoveNext () [0x0031b] in <1bd397951f434cd4913ddc08b7bc45af>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at Xamarin.UITest.XDB.Services.HttpService+<PostAsJsonAsync>c__async3`1[T].MoveNext () [0x00132] in <1bd397951f434cd4913ddc08b7bc45af>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at Xamarin.UITest.XDB.Services.iOSDeviceAgentService+<RequestAsync>c__async14`1[T].MoveNext () [0x000be] in <1bd397951f434cd4913ddc08b7bc45af>:0 
--WebException
  at System.Net.HttpWebRequest.EndGetRequestStream (System.IAsyncResult asyncResult) [0x0003a] in <a24e193d5cf14a42ac00ebaea26ec7bc>:0 
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <4fdc5ed61a074cafb49fa42deb20d521>:0 
  at System.Net.Http.HttpClientHandler+<SendAsync>d__63.MoveNext () [0x002e7] in <7e581108ecb143d78ca072a020c287c3>:0 
--SocketException
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000b6] in <a24e193d5cf14a42ac00ebaea26ec7bc>:0 
  at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0016d] in <a24e193d5cf14a42ac00ebaea26ec7bc>:0 

【问题讨论】:

【参考方案1】:

我使用一个脚本来启动模拟器并在执行测试之前等待 - 这似乎有助于解决这些问题。

如果您还想在执行测试之前安装 .app 文件,还有一个可选的 .app 文件路径:

#!/bin/sh -
# Boots the iOS simulator using specified OS and simulator name

set -e

SIMULATOR_VERSION="$1:?You must pass an iOS version as the first argument"
SIMULATOR_NAME="$2:?You must pass a simulator name as the second argument"
OPTIONAL_APP_PATH="$3"

SIMULATOR_GUID="$(xcrun simctl list | grep -vi unavailable | sed -e "1,/^-- iOS $SIMULATOR_VERSION --\$/d" -e '/^-- .* --$/,$ d' | grep -E "^\s+$SIMULATOR_NAME \(" | sed -e 's/^[^(]*(//g' -e 's/).*//g' | tail -1)"

if [ -z "$SIMULATOR_GUID" ]; then
    echo "ERROR: Could not find simulator GUID for $SIMULATOR_NAME" >&2
    exit 123
fi

shutDownSimulator()

    GUID=$1
    echo "==> Shutting down simulator with GUID $GUID..."
    if xcrun simctl shutdown "$GUID" ; then
        sleep 10s
        echo "==> Successfully shutdown desired simulator."
    else
        echo "==> Desired simulator is already in shutdown state."
    fi


eraseSimulator()

    GUID=$1
    echo "==> Erasing..."
    xcrun simctl erase "$GUID"
    echo "==> Waiting for 30 seconds..."
    sleep 30s


bootSimulator()

    GUID=$1
    echo "==> Booting..."
    xcrun simctl boot "$GUID"
    echo "==> Waiting for 30 seconds..."
    sleep 30s


installOptionalAppFile()

    GUID=$1
    if [ -z "$OPTIONAL_APP_PATH" ] ; then
        echo "==> No app file to install specified, continuing..."
    else
        if [ -e "$OPTIONAL_APP_PATH" ]
        then
            echo "==> Installing app file at $OPTIONAL_APP_PATH"
            xcrun simctl install "$GUID" "$OPTIONAL_APP_PATH"
        else
            echo "==> App file was not found at path: $OPTIONAL_APP_PATH"
        fi
    fi


echo "==> Erasing and launching simulator with name: $SIMULATOR_NAME / iOS version: $SIMULATOR_VERSION / GUID: $SIMULATOR_GUID..."
shutDownSimulator "$SIMULATOR_GUID"
eraseSimulator "$SIMULATOR_GUID"
bootSimulator "$SIMULATOR_GUID"

echo "==> Launching Simulator app..."
open `xcode-select -p`/Applications/Simulator.app

echo "Waiting for simulator to boot for 120 seconds..."
sleep 120s

installOptionalAppFile "$SIMULATOR_GUID"

exit 0

等待时间可能超过 2 分钟,但我这样做是为了以防我的一些 Mac Mini 有点旧

【讨论】:

【参考方案2】:

回答我自己的问题。问题出在 XCode 9.3 上。希望他们尽快解决这个问题。

已知问题

在 macOS 10.13.4 上,首次在模拟器中启动 iOS 11 及更高版本可能需要几分钟时间。 (35628711)

来源:https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-DontLinkElementID_1

【讨论】:

【参考方案3】:

我也遇到了物理设备的这个问题。我发现重启设备可以解决这个问题。 重新启动设备后,UITests 在第一个上不再失败。 我们可以使用名为 libimobiledevice 的库重新启动设备

https://www.mankier.com/1/idevicediagnostics

我正在运行来自 Jenkins 的 UITests,并且在运行测试之前的构建脚本中我们重新启动设备

idevicediagnostics -u[deviceUDID] restart
sleep 90s

【讨论】:

以上是关于Xamarin.UITest 2.2.4 启动会话在第一次测试中失败的主要内容,如果未能解决你的问题,请参考以下文章

如何停止 Xamarin.UITest 删除测试之间的数据?

Xamarin.UITest:如何检索设备显示的坐标

Xamarin.UITest 与 OS X 10.15 不兼容?

Xamarin.UITest :: 如何点击段落中的特定单词?

如何阻止我的 Xamarin.UITest 两次运行测试?

将 Xamarin.UITest 与 MvvmCross 绑定一起使用