Skip to content

Commit

Permalink
Fix UITests so it can recover from unresponsive app (#22240)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen authored May 7, 2024
1 parent 752eed7 commit 599af59
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public Issue19786()
Position++;
});

#if !WINDOWS
BindingContext = this;
#endif
}
}
}
17 changes: 9 additions & 8 deletions src/Controls/tests/UITests/Tests/Issues/Issue19786.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ public Issue19786(TestDevice device) : base(device)
[Category(UITestCategories.CarouselView)]
public void RemovingItemsShouldNotCauseCrash()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Windows });
_ = App.WaitForElement("addItemButton");
App.Click("addItemButton");
App.Click("addItemButton");
App.Click("addItemButton");
App.Click("goToNextItemButton");
App.Click("goToNextItemButton");
App.Click("removeLastItemButton");
App.Click("removeLastItemButton");
App.Click("removeLastItemButton");
App.Tap("addItemButton");
App.Tap("addItemButton");
App.Tap("addItemButton");
App.Tap("goToNextItemButton");
App.Tap("goToNextItemButton");
App.Tap("removeLastItemButton");
App.Tap("removeLastItemButton");
App.Tap("removeLastItemButton");
}

}
Expand Down
13 changes: 11 additions & 2 deletions src/TestUtils/src/UITest.Appium/Actions/AppiumLifecycleActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,17 @@ CommandResponse CloseApp(IDictionary<string, object> parameters)
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;

if (_app.AppState == ApplicationState.NotRunning)
return CommandResponse.SuccessEmptyResponse;
try
{
if (_app.AppState == ApplicationState.NotRunning)
return CommandResponse.SuccessEmptyResponse;
}
catch (Exception)
{
// Occasionally the app seems to get so locked up it can't
// even report back the appstate. In that case, we'll just
// try to trigger a reset.
}

if (_app.GetTestDevice() == TestDevice.Mac)
{
Expand Down
4 changes: 4 additions & 0 deletions src/TestUtils/src/UITest.Appium/AppiumWindowsApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public override ApplicationState AppState
{
return ApplicationState.NotRunning;
}
catch(Exception)
{
return ApplicationState.Unknown;
}
}
}

Expand Down
40 changes: 24 additions & 16 deletions src/TestUtils/src/UITest.NUnit/UITestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected virtual void FixtureSetup()
TestContext.Progress.WriteLine($">>>>> {DateTime.Now} {nameof(FixtureSetup)} for {name}");
}

protected virtual void FixtureTeardown()
protected virtual void FixtureOneTimeTearDown()
{
try
{
Expand All @@ -65,7 +65,7 @@ public void UITestBaseTearDown()
{
try
{
if (App.AppState == ApplicationState.NotRunning)
if (App.AppState != ApplicationState.Running)
{
SaveDeviceDiagnosticInfo();

Expand Down Expand Up @@ -117,37 +117,45 @@ public void OneTimeTearDown()
{
SaveDeviceDiagnosticInfo();

if (App.AppState != ApplicationState.NotRunning)
if (App.AppState == ApplicationState.Running)
SaveUIDiagnosticInfo();
}

FixtureTeardown();
FixtureOneTimeTearDown();
}

void SaveDeviceDiagnosticInfo([CallerMemberName] string? note = null)
{
var types = App.GetLogTypes().ToArray();
TestContext.Progress.WriteLine($">>>>> {DateTime.Now} Log types: {string.Join(", ", types)}");

foreach (var logType in new[] { "logcat" })
try
{
if (!types.Contains(logType, StringComparer.InvariantCultureIgnoreCase))
continue;
var types = App.GetLogTypes().ToArray();
TestContext.Progress.WriteLine($">>>>> {DateTime.Now} Log types: {string.Join(", ", types)}");

var logsPath = GetGeneratedFilePath($"AppLogs-{logType}.log", note);
if (logsPath is not null)
foreach (var logType in new[] { "logcat" })
{
var entries = App.GetLogEntries(logType);
File.WriteAllLines(logsPath, entries);
if (!types.Contains(logType, StringComparer.InvariantCultureIgnoreCase))
continue;

AddTestAttachment(logsPath, Path.GetFileName(logsPath));
var logsPath = GetGeneratedFilePath($"AppLogs-{logType}.log", note);
if (logsPath is not null)
{
var entries = App.GetLogEntries(logType);
File.WriteAllLines(logsPath, entries);

AddTestAttachment(logsPath, Path.GetFileName(logsPath));
}
}
}
catch (Exception e)
{
var name = TestContext.CurrentContext.Test.MethodName ?? TestContext.CurrentContext.Test.Name;
TestContext.Error.WriteLine($">>>>> {DateTime.Now} The SaveDeviceDiagnosticInfo threw an exception during {name}.{Environment.NewLine}Exception details: {e}");
}
}

protected bool SaveUIDiagnosticInfo([CallerMemberName] string? note = null)
{
if (App.AppState == ApplicationState.NotRunning)
if (App.AppState != ApplicationState.Running)
return false;

var screenshotPath = GetGeneratedFilePath("ScreenShot.png", note);
Expand Down

0 comments on commit 599af59

Please sign in to comment.