diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/FeatureMatrix/WebViewFeatureTests.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/FeatureMatrix/WebViewFeatureTests.cs index d606f184fcd8..49d45cac5bab 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/FeatureMatrix/WebViewFeatureTests.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/FeatureMatrix/WebViewFeatureTests.cs @@ -6,6 +6,7 @@ namespace Microsoft.Maui.TestCases.Tests; public class WebViewFeatureTests : _GalleryUITest { + const int ApplyTapMaxAttempts = 3; public const string WebViewFeatureMatrix = "WebView Feature Matrix"; public override string GalleryPageName => WebViewFeatureMatrix; public const string Options = "Options"; @@ -28,6 +29,28 @@ public WebViewFeatureTests(TestDevice device) { } + public void TapApplyAndWaitForMainPage() + { + Exception? lastError = null; + + for (var attempt = 1; attempt <= ApplyTapMaxAttempts; attempt++) + { + try + { + App.WaitForElement(Apply); + App.Tap(Apply); + App.WaitForElementTillPageNavigationSettled(Options); + return; + } + catch (Exception ex) + { + lastError = ex; + } + } + + Assert.Fail($"Failed to tap '{Apply}' toolbar item and return to main page after {ApplyTapMaxAttempts} attempts. Last error: {lastError}"); + } + [Test, Order(1)] [Category(UITestCategories.WebView)] public void WebView_ValidateDefaultValues_VerifyInitialState() @@ -46,20 +69,15 @@ public void WebView_VerifyCanGoBackForward() App.Tap(Options); App.WaitForElement("HtmlSourceButton"); App.Tap("HtmlSourceButton"); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElement(Options); + TapApplyAndWaitForMainPage(); App.Tap(Options); App.WaitForElement("MicrosoftUrlButton"); App.Tap("MicrosoftUrlButton"); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElement(Options); + TapApplyAndWaitForMainPage(); App.Tap(Options); App.WaitForElement("GithubUrlButton"); App.Tap("GithubUrlButton"); - App.WaitForElement(Apply); - App.Tap(Apply); + TapApplyAndWaitForMainPage(); App.WaitForElement(CanGoBackLabel, timeout: TimeSpan.FromSeconds(3)); Assert.That(App.FindElement(CanGoBackLabel).GetText(), Is.EqualTo("True")); App.WaitForElement(GoBackButton); @@ -78,8 +96,7 @@ public void WebView_SetHtmlSource_VerifyJavaScript() App.Tap(Options); App.WaitForElement(HtmlSourceButton); App.Tap(HtmlSourceButton); - App.WaitForElement(Apply); - App.Tap(Apply); + TapApplyAndWaitForMainPage(); App.WaitForElement(EvaluateJSButton); App.Tap(EvaluateJSButton); App.WaitForElement(JSResultLabel); @@ -95,9 +112,7 @@ public void WebView_SetUrlSource_VerifyNavigatingEvent() App.Tap(Options); App.WaitForElement(GithubUrlButton); App.Tap(GithubUrlButton); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElementTillPageNavigationSettled(Options); + TapApplyAndWaitForMainPage(); var navigatingText = App.FindElement(NavigatingStatusLabel).GetText(); Assert.That(navigatingText, Is.Not.Null.And.Not.Empty); } @@ -110,9 +125,7 @@ public void WebView_SetUrlSource_VerifyNavigatedEvent() App.Tap(Options); App.WaitForElement(GithubUrlButton); App.Tap(GithubUrlButton); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElementTillPageNavigationSettled(Options); + TapApplyAndWaitForMainPage(); var navigatedText = App.FindElement(NavigatedStatusLabel).GetText(); Assert.That(navigatedText, Is.EqualTo("Navigated: Success")); } @@ -125,9 +138,7 @@ public void WebView_SetHtmlSource_VerifyNavigatingEvent() App.Tap(Options); App.WaitForElement(HtmlSourceButton); App.Tap(HtmlSourceButton); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElementTillPageNavigationSettled(Options); + TapApplyAndWaitForMainPage(); var navigatingText = App.FindElement(NavigatingStatusLabel).GetText(); Assert.That(navigatingText, Is.Not.Null.And.Not.Empty); } @@ -140,9 +151,7 @@ public void WebView_SetHtmlSource_VerifyNavigatedEvent() App.Tap(Options); App.WaitForElement(HtmlSourceButton); App.Tap(HtmlSourceButton); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElementTillPageNavigationSettled(Options); + TapApplyAndWaitForMainPage(); var navigatedText = App.FindElement(NavigatedStatusLabel).GetText(); Assert.That(navigatedText, Is.EqualTo("Navigated: Success")); } @@ -157,9 +166,7 @@ public void WebView_TestCookieManagement_VerifyAddCookie() App.Tap(AddTestCookieButton); App.WaitForElement(HtmlSourceButton); App.Tap(HtmlSourceButton); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElementTillPageNavigationSettled(Options); + TapApplyAndWaitForMainPage(); var cookiesStatusText = App.FindElement(CookieStatusMainLabel).GetText(); Assert.That(cookiesStatusText, Does.Contain("Domain: localhost").And.Contain("Count: 1").And.Contain("DotNetMAUICookie = My cookie")); } @@ -174,9 +181,7 @@ public void WebView_TestCookieManagement_VerifyAddCookieWithUrlSource() App.Tap(GithubUrlButton); App.WaitForElement(AddTestCookieButton); App.Tap(AddTestCookieButton); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElementTillPageNavigationSettled(Options); + TapApplyAndWaitForMainPage(); var cookiesStatusText = App.FindElement(CookieStatusMainLabel).GetText(); Assert.That(cookiesStatusText, Does.Contain("Domain: github.com").And.Contain("Count: 1").And.Contain("DotNetMAUICookie = My cookie")); } @@ -191,9 +196,7 @@ public void WebView_TestCookieManagement_VerifyAddCookieAndEvaluateJavaScript() App.Tap(AddTestCookieButton); App.WaitForElement(HtmlSourceButton); App.Tap(HtmlSourceButton); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElementTillPageNavigationSettled(Options); + TapApplyAndWaitForMainPage(); App.WaitForElement(EvaluateJSButton); App.Tap(EvaluateJSButton); App.WaitForElement(JSResultLabel); @@ -211,9 +214,7 @@ public void WebView_TestClearCookies_VerifyCookiesCleared() App.Tap(Options); App.WaitForElement(ClearCookiesButton); App.Tap(ClearCookiesButton); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElementTillPageNavigationSettled(Options); + TapApplyAndWaitForMainPage(); var clearCookiesText = App.FindElement(CookieStatusMainLabel).GetText(); Assert.That(clearCookiesText, Is.EqualTo("No cookies available.")); } @@ -226,8 +227,7 @@ public void WebView_TestReloadMethod_VerifyReloadFunctionality() App.Tap(Options); App.WaitForElement(GithubUrlButton); App.Tap(GithubUrlButton); - App.WaitForElement(Apply); - App.Tap(Apply); + TapApplyAndWaitForMainPage(); App.WaitForElement("ReloadButton"); App.Tap("ReloadButton"); var navigatedText = App.FindElement(NavigatedStatusLabel).GetText(); @@ -243,8 +243,7 @@ public void WebView_VerifyReloadFunctionalityForHtmlWebViewSource() App.Tap(Options); App.WaitForElement(HtmlSourceButton); App.Tap(HtmlSourceButton); - App.WaitForElement(Apply); - App.Tap(Apply); + TapApplyAndWaitForMainPage(); App.WaitForElement("ReloadButton"); App.Tap("ReloadButton"); var navigatedText = App.FindElement(NavigatedStatusLabel).GetText(); @@ -260,8 +259,7 @@ public void WebView_TestEvaluateJavaScriptAsync_VerifyJavaScriptExecution() App.Tap(Options); App.WaitForElement("LoadPage1Button"); App.Tap("LoadPage1Button"); - App.WaitForElement(Apply); - App.Tap(Apply); + TapApplyAndWaitForMainPage(); App.WaitForElement(EvaluateJSButton); App.Tap(EvaluateJSButton); App.WaitForElement(JSResultLabel); @@ -277,8 +275,7 @@ public void WebView_TestEvaluateJavaScriptAsync_VerifyJavaScriptExecutionWithMul App.Tap(Options); App.WaitForElement("LoadMultiplePagesButton"); App.Tap("LoadMultiplePagesButton"); - App.WaitForElement(Apply); - App.Tap(Apply); + TapApplyAndWaitForMainPage(); App.WaitForElement(EvaluateJSButton); App.Tap(EvaluateJSButton); App.WaitForElement(JSResultLabel); @@ -296,9 +293,7 @@ public void WebView_SetIsVisibleFalse_VerifyWebViewHidden() App.Tap(GithubUrlButton); App.WaitForElement("IsVisibleFalse"); App.Tap("IsVisibleFalse"); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElementTillPageNavigationSettled(Options); + TapApplyAndWaitForMainPage(); App.WaitForNoElement(WebViewControl); } @@ -311,9 +306,7 @@ public void VerifyWebViewWithShadow() App.Tap(Options); App.WaitForElement("ShadowTrue"); App.Tap("ShadowTrue"); - App.WaitForElement(Apply); - App.Tap(Apply); - App.WaitForElementTillPageNavigationSettled(Options, timeout: TimeSpan.FromSeconds(3)); + TapApplyAndWaitForMainPage(); VerifyScreenshot(tolerance: 0.5, retryTimeout: TimeSpan.FromSeconds(2)); } #endif