Skip to content

Commit ea45872

Browse files
Tamilarasan-ParanthamanPureWeen
authored andcommitted
[Android][iOS] WebView Navigated event not triggered when using HtmlWebViewSource (#28354)
* WebView navigated event fix * Test sample changes.
1 parent 585adcc commit ea45872

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 23502, "WebView Navigated event is not triggered", PlatformAffected.Android, isInternetRequired: true)]
4+
public partial class Issue23502 : ContentPage
5+
{
6+
public Issue23502()
7+
{
8+
9+
var verticalStackLayout = new VerticalStackLayout();
10+
verticalStackLayout.Spacing = 20;
11+
var webview = new WebView()
12+
{
13+
HeightRequest = 300,
14+
AutomationId = "webView",
15+
Source = new HtmlWebViewSource
16+
{
17+
Html = @"<HTML><BODY><H1>.NET MAUI</H1><P>Welcome to WebView.</P></BODY></HTML>"
18+
},
19+
};
20+
21+
var label1 = new Label
22+
{
23+
Text = "WebView Navigating event is not triggered",
24+
AutomationId = "navigatingLabel"
25+
};
26+
27+
var label = new Label
28+
{
29+
Text = "WebView Navigated event is not triggered",
30+
AutomationId = "navigatedLabel"
31+
};
32+
33+
webview.Navigating += (s, e) =>
34+
{
35+
label1.Text = "Navigating event is triggered";
36+
};
37+
38+
webview.Navigated += (s, e) =>
39+
{
40+
label.Text = "Navigated event is triggered";
41+
};
42+
43+
verticalStackLayout.Add(label1);
44+
verticalStackLayout.Add(label);
45+
verticalStackLayout.Add(webview);
46+
47+
Content = verticalStackLayout;
48+
}
49+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues
6+
{
7+
public class Issue23502 : _IssuesUITest
8+
{
9+
public override string Issue => "WebView Navigated event is not triggered";
10+
11+
public Issue23502(TestDevice device)
12+
: base(device)
13+
{ }
14+
15+
[Test]
16+
[Category(UITestCategories.WebView)]
17+
public void VerifyWebViewNavigatedEvent()
18+
{
19+
VerifyInternetConnectivity();
20+
var navigatingLabel = App.WaitForElement("navigatingLabel");
21+
var navigatedLabel = App.WaitForElement("navigatedLabel");
22+
23+
Assert.That(navigatingLabel.GetText(), Is.EqualTo("Navigating event is triggered"));
24+
Assert.That(navigatedLabel.GetText(), Is.EqualTo("Navigated event is triggered"));
25+
}
26+
}
27+
}

src/Core/src/Handlers/WebView/WebViewHandler.Android.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ protected internal bool NavigatingCanceled(string? url)
146146
if (VirtualView == null || string.IsNullOrWhiteSpace(url))
147147
return true;
148148

149-
if (url == AssetBaseUrl)
150-
return false;
151-
152149
SyncPlatformCookies(url);
153150
bool cancel = VirtualView.Navigating(CurrentNavigationEvent, url);
154151

src/Core/src/Platform/Android/MauiWebViewClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override bool ShouldOverrideUrlLoading(WebView? view, IWebResourceRequest
2222

2323
public override void OnPageStarted(WebView? view, string? url, Bitmap? favicon)
2424
{
25-
if (!_handler.TryGetTarget(out var handler) || handler.VirtualView == null || url == WebViewHandler.AssetBaseUrl)
25+
if (!_handler.TryGetTarget(out var handler) || handler.VirtualView == null)
2626
return;
2727

2828
if (!string.IsNullOrWhiteSpace(url))
@@ -51,7 +51,7 @@ public override void OnPageStarted(WebView? view, string? url, Bitmap? favicon)
5151

5252
public override void OnPageFinished(WebView? view, string? url)
5353
{
54-
if (!_handler.TryGetTarget(out var handler) || handler.VirtualView == null || string.IsNullOrWhiteSpace(url) || url == WebViewHandler.AssetBaseUrl)
54+
if (!_handler.TryGetTarget(out var handler) || handler.VirtualView == null || string.IsNullOrWhiteSpace(url))
5555
return;
5656

5757
bool navigate = _navigationResult != WebNavigationResult.Failure || !GetValidUrl(url).Equals(_lastUrlNavigatedCancel, StringComparison.OrdinalIgnoreCase);

src/Core/src/Platform/iOS/MauiWebViewNavigationDelegate.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ public void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
4343

4444
var url = GetCurrentUrl();
4545

46-
if (url == $"file://{NSBundle.MainBundle.BundlePath}/")
47-
return;
48-
4946
virtualView.Navigated(_lastEvent, url, WebNavigationResult.Success);
5047

5148
// ProcessNavigatedAsync calls UpdateCanGoBackForward

0 commit comments

Comments
 (0)