System.Net.WebException : POST Failed while UiTesting on local emulator

532 views Asked by At

I am trying to verify that the UI elements of my LoginPage work as expected. The test is supposed to write a user name, user password and server adress into the corresponding entry fields then tap the sign in button.

The UITest loads the LoginPage and then fails with this exception:

Nachricht: 
    System.Net.WebException : POST Failed
  Stapelüberwachung: 
    HttpClient.HandleHttpError(String method, Exception exception, ExceptionPolicy exceptionPolicy)
    HttpClient.SendData(String endpoint, String method, HttpContent content, ExceptionPolicy exceptionPolicy, Nullable`1 timeOut)
    HttpClient.Post(String endpoint, String arguments, ExceptionPolicy exceptionPolicy, Nullable`1 timeOut)
    HttpApplicationStarter.Execute(String intentJson)
    AndroidAppLifeCycle.LaunchApp(String appPackageName, ApkFile testServerApkFile, Int32 testServerPort)
    AndroidApp.ctor(IAndroidAppConfiguration appConfiguration, IExecutor executor)
    AndroidAppConfigurator.StartApp(AppDataMode appDataMode)
    ReplDroid.ConnectToDeviceAndr() Zeile 33
    ReplDroid.BeforeEachTest() Zeile 24

Now the really interesting part is that it worked before, but after I added code which threw and exception (and removed it again) it is stuck in this POST Failed exception.

To fix my problem I already tried: doing a factory reset of the emulator, cleaning the solution and rebuilding it, deleting the .vs folder, restarting vs and the emulator, testing on different emulator instances and restarting the entire PC. Nothing worked reliably.

The code of my test project is below:

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

namespace REPL
{
    [TestFixture(Platform.Android)]
    public class ReplDroid
    {
        IApp app;
        Platform platform;
        LoginPageController loginPageController;
        MainMenuController mainMenuController;

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

        [SetUp]
        public void BeforeEachTest()
        {
            ConnectToDeviceAndr();

            loginPageController = new LoginPageController(app);
            mainMenuController = new MainMenuController(app);

        }

        private void ConnectToDeviceAndr()
        {
            app = ConfigureApp.Android
                .ApkFile(ApkLocation)
                .PreferIdeSettings()
                .EnableLocalScreenshots()
                .StartApp();
        }

        [Test]
        public void Login()
        {
            loginPageController.EnterUserNumber(UserNumber);
            loginPageController.EnterPassword(Password);
            loginPageController.EnterServerUrl(ServerUrl);
            loginPageController.SignIn();
        }
    }
}

using Xamarin.UITest;
using Xamarin.UITest.Queries;

namespace REPL
{
    public class LoginPageController
    {
        private readonly IApp app;

        private const string UserEntryAutomationId = "UserEntry";

        private const string PasswordEntryAutomationId = "PasswordEntry";

        private const string ServerUrlEntryAutomationId = "ServerUrlEntry";

        private const string LoginButtonAutomationId = "LoginButton";

        public LoginPageController(IApp app)
        {
            this.app = app;
        }

        public AppResult GetUserEntry() => app.GetByAutomationId(UserEntryAutomationId);

        public AppResult GetPasswordEntry() => app.GetByAutomationId(PasswordEntryAutomationId);

        public AppResult GetServerUrlEntry() => app.GetByAutomationId(ServerUrlEntryAutomationId);

        public AppResult GetLoginButton() => app.GetByAutomationId(LoginButtonAutomationId);

        public void EnterUserNumber(string userNumber) => app.WaitAndContinue(UserEntryAutomationId).EnterText(userNumber);

        public void EnterPassword(string password) => app.WaitAndContinue(PasswordEntryAutomationId).EnterText(password);

        public void EnterServerUrl(string serverUrl) => app.WaitAndContinue(ServerUrlEntryAutomationId).EnterText(serverUrl);

        public void SignIn()
        {
            app.DismissKeyboard();
            app.WaitAndContinue(LoginButtonAutomationId).Tap();
        }
    }
}

All similar posts I could find were about failing to test on App Center, if I somehow missed one relevant to my problem please point me in the right direction.

1

There are 1 answers

0
meschi On

I had the same problem. running an empty android Xamarin forms UiTest in my IDE (Jetbrains Rider)

[Test]
public void WelcomeTextIsDisplayed()
{
    AppResult[] results = app.WaitForElement(c => c.Marked("Welcome to Xamarin.Forms!"));
    app.Screenshot("Welcome screen.");

    Assert.IsTrue(results.Any());
}

I fixed it by updating the Nudget package Xamarin.UiTests from 2.2.4 to the newest (3.2.0 currently)