Skip to content

Commit

Permalink
[Testing] Enabling ported UITests from Xamarin.UITests to Appium - 11 (
Browse files Browse the repository at this point in the history
…#25855)

* enabled ui tests from xamarinuitests into appium

* Updated the TabBackArrow() method

* Update AppiumApp.cs

Refered the code from 9dab842

* Updated the fails message content

* Update Issue12320.cs

* Addressed the review changes

Add WaitForElementTillPageNavigationSettled Extension Method
Description
Also introduces a new extension method WaitForElementTillPageNavigationSettled to enhance the stability of UI tests, particularly for MacCatalyst applications. The method provides a more robust way to wait for elements during page transitions, helping to prevent null reference exceptions.

* Update HelperExtensions.cs

* Update HelperExtensions.cs
  • Loading branch information
anandhan-rajagopal authored Dec 2, 2024
1 parent 0f6f6dc commit 1a88b34
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ int CommandCount

protected override void Init()
{
PressedLabel = new Label();
ReleasedLabel = new Label();
ClickedLabel = new Label();
CommandLabel = new Label();
PressedLabel = new Label { AutomationId = "PressedLabel" };
ReleasedLabel = new Label { AutomationId = "ReleasedLabel" };
ClickedLabel = new Label { AutomationId = "ClickedLabel" };
CommandLabel = new Label { AutomationId = "CommandLabel" };

var button = new Button
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ protected override async void OnNavigated(ShellNavigatedEventArgs args)
};

Shell.SetTabBarIsVisible(contentPage, false);
Shell.SetBackButtonBehavior(contentPage, new BackButtonBehavior() { IconOverride = "coffee.png" });
Shell.SetBackButtonBehavior(contentPage, new BackButtonBehavior
{
IconOverride = new FileImageSource
{
AutomationId = "BackButtonImage",
File = "coffee.png"
}
});
await Navigation.PushAsync(contentPage);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public HeaderCell()
Height = 44;
var label = new Label { BackgroundColor = Colors.Pink };
label.SetBinding(Label.TextProperty, "GroupName");
label.SetBinding(Label.AutomationIdProperty, "GroupName");
View = label;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ protected override void Init()
});

item2.Title = "Visible After Remove";
#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable CS0612 // Type or member is obsolete
Device.BeginInvokeOnMainThread(() =>

MainThread.BeginInvokeOnMainThread(() =>
{
this.Items.Remove(item1);
});
#pragma warning restore CS0612 // Type or member is obsolete
#pragma warning restore CS0618 // Type or member is obsolete


}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if MACCATALYST
using NUnit.Framework;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using UITest.Appium;
using UITest.Core;
Expand All @@ -14,17 +13,15 @@ public Github1776(TestDevice testDevice) : base(testDevice)

public override string Issue => "Button Released not being triggered";

// [Test]
// [Category(UITestCategories.Button)]
// public void GitHub1776Test()
// {
// App.WaitForElement(q => q.Marked("TheButton"));
// App.Tap(q => q.Marked("TheButton"));

// Assert.AreEqual(1, _pressedCount, "Pressed should fire once per tap");
// Assert.AreEqual(1, _releasedCount, "Released should fire once per tap");
// Assert.AreEqual(1, _clickedCount, "Clicked should fire once per tap");
// Assert.AreEqual(1, _commandCount, "Command should fire once per tap");
// }
[Test]
[Category(UITestCategories.Button)]
public void GitHub1776Test()
{
App.WaitForElement("TheButton");
App.Tap("TheButton");
Assert.That(App.FindElement("PressedLabel").GetText(), Is.EqualTo("Pressed: 1"));
Assert.That(App.FindElement("ReleasedLabel").GetText(), Is.EqualTo("Released: 1"));
Assert.That(App.FindElement("ClickedLabel").GetText(), Is.EqualTo("Clicked: 1"));
Assert.That(App.FindElement("CommandLabel").GetText(), Is.EqualTo("Command: 1"));
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ public Issue12320(TestDevice testDevice) : base(testDevice)

public override string Issue => "[iOS] TabBarIsVisible = True/False doesn't work on Back Navigation When using BackButtonBehavior";

// Where does TapBackArrow come from?
// [Test]
// [Category(UITestCategories.Shell)]
// public void PopLogicExecutesWhenUsingBackButtonBehavior()
// {
// App.WaitForElement("TestReady");
// base.TapBackArrow();
// App.WaitForElement("Tab 1");
// }
}
[Test]
[Category(UITestCategories.Shell)]
public void PopLogicExecutesWhenUsingBackButtonBehavior()
{
App.WaitForElementTillPageNavigationSettled("TestReady");
App.TapBackArrow("BackButtonImage");
App.WaitForElement("Tab 1");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ public Issue2953(TestDevice testDevice) : base(testDevice)

public override string Issue => "GroupHeaderCells disappear when item is removed from a group in ListView (iOS only) ";

// [Test]
// [Category(UITestCategories.ListView)]
// [FailsOnIOS]
// public void Issue2953Test()
// {
// App.Screenshot("I am at Issue 2953");
// App.WaitForElement(q => q.Marked("Header 3"));
// App.Screenshot("I see the Header 3");
// App.Tap(q => q.Marked("btnRemove"));
// App.WaitForElement(q => q.Marked("Header 3"));
// App.Screenshot("I still see the Header 3");
// }
[Test]
[Category(UITestCategories.ListView)]
public void Issue2953Test()
{
App.WaitForElement("Header 3");
App.Tap("btnRemove");
App.WaitForElement("Header 3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ public Issue8008(TestDevice testDevice) : base(testDevice)

public override string Issue => "Removing Shell Item can cause Shell to try and set a MenuItem as the default visible item";

//[Test]
//[Category(UITestCategories.Shell)]
//[FailsOnIOS]
//public void RemovingShellItemCorrectlyPicksNextValidShellItemAsVisibleShellItem()
//{
// App.WaitForElement("Success");
//}
[Test]
[Category(UITestCategories.Shell)]
public void RemovingShellItemCorrectlyPicksNextValidShellItemAsVisibleShellItem()
{
App.WaitForElement("Success");
}
}
34 changes: 32 additions & 2 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ public static IDictionary<string, object> GetSystemBars(this IApp app)
}

throw new InvalidOperationException($"Could not get the Android System Bars");
}
}

/// <summary>
/// Navigates back in the application by simulating a tap on the platform-specific back navigation button.
Expand Down Expand Up @@ -1812,6 +1812,36 @@ public static void TapBackArrow(this IApp app, string customBackButtonIdentifier
break;
}
}

/// <summary>
/// Waits for an element to be ready until page navigation has settled, with additional waiting for MacCatalyst.
/// This method helps prevent null reference exceptions during page transitions, especially in MacCatalyst.
/// </summary>
/// <param name="app">The IApp instance.</param>
/// <param name="elementId">The id of the element to wait for.</param>
/// <param name="timeout">Optional timeout for the wait operation. Default is null, which uses the default timeout.</param>
public static void WaitForElementTillPageNavigationSettled(this IApp app, string elementId, TimeSpan? timeout = null)
{
if(app is AppiumCatalystApp)
app.WaitForElement(AppiumQuery.ById(elementId), timeout: timeout);

app.WaitForElement(elementId, timeout: timeout);
}

/// <summary>
/// Waits for an element to be ready until page navigation has settled, with additional waiting for MacCatalyst.
/// This method helps prevent null reference exceptions during page transitions, especially in MacCatalyst.
/// </summary>
/// <param name="app">The IApp instance.</param>
/// <param name="query">The query to use for finding the element.</param>
/// <param name="timeout">Optional timeout for the wait operation. Default is null, which uses the default timeout.</param>
public static void WaitForElementTillPageNavigationSettled(this IApp app, IQuery query, TimeSpan? timeout = null)
{
if(app is AppiumCatalystApp)
app.WaitForElement(query, timeout: timeout);

app.WaitForElement(query, timeout: timeout);
}

/// <summary>
/// Taps the "More" button in the app, with platform-specific logic for Android and Windows.
Expand Down Expand Up @@ -1905,4 +1935,4 @@ public static void SetTestConfigurationArg(this IConfig config, string key, stri
config.SetProperty("TestConfigurationArgs", startupArg);
}
}
}
}

0 comments on commit 1a88b34

Please sign in to comment.