Skip to content

Commit

Permalink
[Testing] Fix for WebView flaky tests in CI which fails due to networ…
Browse files Browse the repository at this point in the history
…k connectivity. (dotnet#27178)

* Added connectivity functionality using NetworkAccess property

* Modified Internet Connectivity functionality in CoreGallery

* format corrected in Issue3262

* Update changes

* Updated changes

* Update commit

* Provided internet support for GalleryPages also.

* removed CorePageview

* Update src/Controls/tests/TestCases.HostApp/CoreViews/NoInternetConnectionPage.cs

Addressed review correction

Co-authored-by: MartyIX <[email protected]>

---------

Co-authored-by: NafeelaNazhir <[email protected]>
Co-authored-by: MartyIX <[email protected]>
  • Loading branch information
3 people authored Jan 21, 2025
1 parent 1807a6c commit 5ceff42
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/Controls/tests/CustomAttributes/TestAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,49 +48,53 @@ public class IssueAttribute : Attribute
{

public IssueAttribute(IssueTracker issueTracker, int issueNumber, string description,
NavigationBehavior navigationBehavior = NavigationBehavior.Default, int issueTestNumber = 0)
NavigationBehavior navigationBehavior = NavigationBehavior.Default, int issueTestNumber = 0, bool isInternetRequired = false)
{
IssueTracker = issueTracker;
IssueNumber = issueNumber.ToString();
Description = description;
PlatformsAffected = PlatformAffected.Default;
NavigationBehavior = navigationBehavior;
IssueTestNumber = issueTestNumber;
IsInternetRequired = isInternetRequired;
}

public IssueAttribute(IssueTracker issueTracker, string issueNumber, string description,
NavigationBehavior navigationBehavior = NavigationBehavior.Default, int issueTestNumber = 0)
NavigationBehavior navigationBehavior = NavigationBehavior.Default, int issueTestNumber = 0, bool isInternetRequired = false)
{
IssueTracker = issueTracker;
IssueNumber = issueNumber;
Description = description;
PlatformsAffected = PlatformAffected.Default;
NavigationBehavior = navigationBehavior;
IssueTestNumber = issueTestNumber;
IsInternetRequired = isInternetRequired;
}

public IssueAttribute(IssueTracker issueTracker, int issueNumber, string description,
PlatformAffected platformsAffected, NavigationBehavior navigationBehavior = NavigationBehavior.Default,
int issueTestNumber = 0)
int issueTestNumber = 0, bool isInternetRequired = false)
{
IssueTracker = issueTracker;
IssueNumber = issueNumber.ToString();
Description = description;
PlatformsAffected = platformsAffected;
NavigationBehavior = navigationBehavior;
IssueTestNumber = issueTestNumber;
IsInternetRequired = isInternetRequired;
}

public IssueAttribute(IssueTracker issueTracker, string issueNumber, string description,
PlatformAffected platformsAffected, NavigationBehavior navigationBehavior = NavigationBehavior.Default,
int issueTestNumber = 0)
int issueTestNumber = 0, bool isInternetRequired = false)
{
IssueTracker = issueTracker;
IssueNumber = issueNumber;
Description = description;
PlatformsAffected = platformsAffected;
NavigationBehavior = navigationBehavior;
IssueTestNumber = issueTestNumber;
IsInternetRequired = isInternetRequired;
}

public IssueTracker IssueTracker { get; }
Expand All @@ -105,6 +109,8 @@ public IssueAttribute(IssueTracker issueTracker, string issueNumber, string desc

public NavigationBehavior NavigationBehavior { get; }

public bool IsInternetRequired { get; }

public string DisplayName => IssueTestNumber == 0
? $"{IssueTracker.ToString().Substring(0, 1)}{IssueNumber}"
: $"{IssueTracker.ToString().Substring(0, 1)}{IssueNumber} ({IssueTestNumber})";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Maui.Controls.Sample
{
internal class NoInternetConnectionPage : ContentPage
{
public NoInternetConnectionPage()
{
Content = new Label()
{
Text = "This device doesn't have Internet access",
AutomationId = "NoInternetAccessLabel",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};
}
}
}
2 changes: 1 addition & 1 deletion src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 3262, "Adding Cookies ability to a WebView...")]
[Issue(IssueTracker.Github, 3262, "Adding Cookies ability to a WebView...", isInternetRequired: true)]
public class Issue3262 : TestContentPage // or TestFlyoutPage, etc ...
{
string _currentCookieValue;
Expand Down
16 changes: 16 additions & 0 deletions src/Controls/tests/TestCases.HostApp/TestCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ static TextCell MakeIssueCell(string text, string detail, Action tapped)
Action ActivatePageAndNavigate(IssueAttribute issueAttribute, Type type)
{
Action navigationAction = null;

if(issueAttribute.IsInternetRequired)
{
NetworkAccess accessType = Connectivity.Current.NetworkAccess;

if (accessType != NetworkAccess.Internet)
{
return () =>
{
var noInternetConnectionPage = ActivatePage(typeof(NoInternetConnectionPage));
Application.Current.Windows[0].Page = noInternetConnectionPage;
};
}
}

if (issueAttribute.NavigationBehavior == NavigationBehavior.PushAsync)
{
Expand Down Expand Up @@ -77,6 +91,7 @@ class IssueModel
public int IssueTestNumber { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsInternetRequired { get; set; }
public Action Action { get; set; }

public bool Matches(string filter)
Expand Down Expand Up @@ -149,6 +164,7 @@ public TestCaseScreen()
IssueTestNumber = attribute.IssueTestNumber,
Name = attribute.DisplayName,
Description = attribute.Description,
IsInternetRequired = attribute.IsInternetRequired,
Action = ActivatePageAndNavigate(attribute, type)
}).ToList();
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,21 @@ void ValidateSuccess()
throw;
}
}

public override void TestSetup()
{
base.TestSetup();

try
{
App.WaitForElement("NoInternetAccessLabel", timeout: TimeSpan.FromSeconds(1));
Assert.Inconclusive("This device doesn't have internet access");
}
catch (TimeoutException)
{
// Element not found within timeout, assume internet is available
// Continue with the test
}
}
}
}

0 comments on commit 5ceff42

Please sign in to comment.