dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.89k stars 525 forks source link

HttpRequestException running UITest on Android #6011

Closed Eltee-Taiwo closed 1 month ago

Eltee-Taiwo commented 3 years ago

Steps to Reproduce

  1. Make a Xamarin.Forms project with Android
  2. Make a UITest project
  3. Try to run against android emulator

Expected Behavior

Tests work

Actual Behavior

An exception is thrown:

Message:
System.Net.Http.HttpRequestException : An error occurred while sending the request.
----> System.Net.WebException : The underlying connection was closed: The connection was closed unexpectedly.
Stack Trace:
HttpClient.SendData(String endpoint, String method, HttpContent content, ExceptionPolicy exceptionPolicy, Nullable1 timeOut) HttpClient.Post(String endpoint, String arguments, ExceptionPolicy exceptionPolicy, Nullable1 timeOut)
HttpApplicationStarter.Execute(String intentJson)
AndroidAppLifeCycle.LaunchApp(String appPackageName, ApkFile testServerApkFile, Int32 testServerPort)
AndroidApp.ctor(IAndroidAppConfiguration appConfiguration, IExecutor executor)
AndroidAppConfigurator.StartApp(AppDataMode appDataMode)
AppInitializer.StartApp(Platform platform)
Tests.BeforeEachTest()
--WebException
HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
HttpClientHandler.GetResponseCallback(IAsyncResult ar)

Workaround:

~Disable .EnableLocalScreenshots() in AppInitializer.cs and remove any code that's trying to take a screenshot.~ Looks like that was a false positive. It's back to not working but I'll keep playing around with it to see if I can figure one out.

Version Information

Microsoft Visual Studio Community 2019
Version 16.9.4
VisualStudio.16.Release/16.9.4+31205.134
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Community

Mono Debugging for Visual Studio   16.9.7 (df23ba6)
Support for debugging Mono processes with Visual Studio.

Xamarin   16.9.000.273 (d16-9@1bba9e0)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Templates   16.9.72 (426ebf6)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   11.2.2.1 (d16-9/877f572)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: 5e9cb6d
    Java.Interop: xamarin/java.interop/d16-9@54f8c24
    ProGuard: Guardsquare/proguard/v7.0.1@912d149
    SQLite: xamarin/sqlite/3.34.1@daff8f4
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-9@d210f11

Xamarin.iOS and Xamarin.Mac SDK   14.14.2.5 (3836759d4)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
Eltee-Taiwo commented 3 years ago

Duplicate of https://github.com/xamarin/xamarin-android/issues/4721 but that was closed by the user.

grendello commented 3 years ago

This looks like a network issue, most likely not a bug in Mono's BCL. However, since it happens in HttpClient which is not part of Xamarin.Android but rather of Mono, I'm reassigning to the Mono team to look at it. @steveisok, can you route it to whoever can take a look? Thanks!

edgiardina commented 2 years ago

Any update on this? Xamarin.UITest seems to be the easiest to implement framework for UITesting but can't get started due to this bug.

Greg-Bates-Trimble commented 2 years ago

Any chance of an update on this please?

iooovolts commented 2 years ago

I have had this issue a number of times now, my resolution steps are one of the following in no particular order:

Greg-Bates-Trimble commented 2 years ago

Thanks a lot for that @iooovolts. Now working. Appreciate it :)

ngreyes commented 1 year ago

Added this in case someone still running into this problem. I believe that it is common if you are Unit Testing in UnitTest, NUnit, XUnit (it doesn't matter), I'm on VS 2022. I recently had this problem and it caused me days to resolve when I ran into this post. Basically, the Android emulators in Visual Studio are very meticulous when it comes to apps doing too much when initializing the app and it disconnects if it senses is going for too long. There is nothing I found to force the emulator to complete rendering or extend it. The way I solved it is by unloading some of the stuff from the initializing construct. Just add basic stuff to help render the app and do not add any extended methods (async or sync, it doesn't matter). I was seeding a folder structure during initialization and that was apparently causing a heartburn. Note that the app was installing and running fine everywhere except launching from a Xamarin Test framework.

jpobst commented 1 month ago

With support for Classic Xamarin.Android ended May 1st, 2024, this issue is likely no longer relevant.

If this still persists in .NET 8+, please open a new issue with updated information based on net8.0-android or greater. Please include a link to this issue for context.