Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Testing] Reenable test ported from Xamarin.UITest #25624

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,67 +1,71 @@
using NUnit.Framework;
#if TEST_FAILS_ON_WINDOWS
using NUnit.Framework;
using NUnit.Framework.Legacy;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue3809 : _IssuesUITest
{
const string SetPagePadding = "Set Page Padding";
const string SafeAreaText = "Safe Area Enabled: ";
const string PaddingLabel = "paddingLabel";
const string SafeAreaAutomationId = "SafeAreaAutomation";

public Issue3809(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "SetUseSafeArea is wiping out Page Padding ";

// TODO: The _ variables need to be AutomationId values
//void AssertSafeAreaText(string text)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the UI tests using Xamarin.UITest from Xamarin.Forms have been ported to .NET MAUI. The goal is to have all existing Xamarin.Forms tests running alongside everything created in .NET MAUI. However, a number of tests are commented. Let's review the steps required to enable them.

Step 1: Uncomment the code.

//{
// var element =
// RunningApp
// .WaitForFirstElement(_safeAreaAutomationId);

// element.AssertHasText(text);
//}

//[Test]
//[Category(UITestCategories.Layout)]
//public void SafeAreaInsetsBreaksAndroidPadding()
//{
// // ensure initial paddings are honored
// AssertSafeAreaText($"{_safeAreaText}{true}");
// var element = App.WaitForFirstElement(_paddingLabel);
void AssertSafeAreaText(string text)
{
var element = App.WaitForFirstElement(SafeAreaAutomationId);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Step 2: Rename RunningApp to App.

// bool usesSafeAreaInsets = false;
// if (element.ReadText() != "25, 25, 25, 25")
// usesSafeAreaInsets = true;
ClassicAssert.AreEqual(element.GetText(), text);
}

// Assert.AreNotEqual(element.ReadText(), "0, 0, 0, 0");
// if (!usesSafeAreaInsets)
// Assert.AreEqual(element.ReadText(), "25, 25, 25, 25");
[Test]
[Category(UITestCategories.Layout)]
[Category(UITestCategories.Page)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Step 3: Add Category. Without a category, the project test project will not compile.

If the category is relevant to all tests in a class, you can also put it on the class.

public void SafeAreaInsetsBreaksAndroidPadding()
{
// Ensure initial paddings are honored
AssertSafeAreaText($"{SafeAreaText}{true}");
var element = App.WaitForFirstElement(PaddingLabel);

// // disable Safe Area Insets
// App.Tap(_safeAreaAutomationId);
// AssertSafeAreaText($"{_safeAreaText}{false}");
// element = App.WaitForFirstElement(_paddingLabel);
bool usesSafeAreaInsets = false;
if (element.ReadText() != "25, 25, 25, 25")
usesSafeAreaInsets = true;

// Assert.AreEqual(element.ReadText(), "25, 25, 25, 25");
ClassicAssert.AreNotEqual(element.ReadText(), "0, 0, 0, 0");
if (!usesSafeAreaInsets)
ClassicAssert.AreEqual(element.ReadText(), "25, 25, 25, 25");

// // enable Safe Area insets
// App.Tap(_safeAreaAutomationId);
// AssertSafeAreaText($"{_safeAreaText}{true}");
// element = App.WaitForFirstElement(_paddingLabel);
// Assert.AreNotEqual(element.ReadText(), "0, 0, 0, 0");
// Disable Safe Area Insets
App.Tap(SafeAreaAutomationId);
AssertSafeAreaText($"{SafeAreaText}{false}");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Step 4: Find replacement methods in Appium. See the reference table from the PR. We already have a lot of helper methods to interact with the UI through Appium. You can find those under src\TestUtils\src\UITest.Appium.

If you do think there is no alternative, please reach out and lets see if we need to add something.

element = App.WaitForFirstElement(PaddingLabel);

// if (!usesSafeAreaInsets)
// Assert.AreEqual(element.ReadText(), "25, 25, 25, 25");
ClassicAssert.AreEqual(element.ReadText(), "25, 25, 25, 25");

// Enable Safe Area insets
App.Tap(SafeAreaAutomationId);
AssertSafeAreaText($"{SafeAreaText}{true}");
element = App.WaitForFirstElement(PaddingLabel);
ClassicAssert.AreNotEqual(element.ReadText(), "0, 0, 0, 0");

// // Set Padding and then disable safe area insets
// App.Tap(_setPagePadding);
// App.Tap(_safeAreaAutomationId);
// AssertSafeAreaText($"{_safeAreaText}{false}");
// element = App.WaitForFirstElement(_paddingLabel);
// Assert.AreEqual(element.ReadText(), "25, 25, 25, 25");
if (!usesSafeAreaInsets)
ClassicAssert.AreEqual(element.ReadText(), "25, 25, 25, 25");

//}
// Set Padding and then disable safe area insets
App.Tap(SetPagePadding);
App.Tap(SafeAreaAutomationId);
AssertSafeAreaText($"{SafeAreaText}{false}");
element = App.WaitForFirstElement(PaddingLabel);
ClassicAssert.AreEqual(element.ReadText(), "25, 25, 25, 25");
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Step 5: Make test work! Obvious, right? Run it locally as much as you can and make sure the tests pass. Then push them up to a pull request and see if they run there and pass there too.

Apply good practices:

  • Avoid complex XPath queries, they are more expensive than locating elements using AutomationId.
  • In a good number of tests you will find Task.Delay(500) or other ways to have a random delay while we wait for the UI to catch up. That will make tests unreliable so please prevent using those and remove them where you can. There are methods WaitForElement("AutomationId") and WaitForNoElement("AutomationId") that will wait for an element to appear or disappear, those are usually good replacements.

}
}
}
#endif
Loading