Skip to content

Commit

Permalink
UITest: Add UIAssert.NotExists method and extend a test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Apr 30, 2024
1 parent a8cb408 commit f8c8558
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Samples.UITest/Writer.Test/Tests/WriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@

namespace UITest.Writer;

public class WriterTest(ITestOutputHelper log) : UITest(log)
public class WriterTest : UITest
{
public WriterTest(ITestOutputHelper log) : base(log)
{
// SkipTextBoxReadText = true; // Workaround: Uncomment this line if your machine provides empty strings for RichTextBox.Text.
}

/// <summary>Workaround: On some machines reading the RichTextBox.Text returns always an empty string. Use this property to skip the step.</summary>
public bool SkipTextBoxReadText { get; set; }

protected void AssertTextEqual(string expected, TextBox actual) { if (!SkipTextBoxReadText) Assert.Equal(expected, actual.Text); }

[Fact]
public void AboutTest()
{
Expand Down Expand Up @@ -158,7 +168,7 @@ public void MultipleNewSaveRestartOpenRecentChangeAskToSave()
startView.RecentFileListItems[0].OpenFileButton.Invoke();
tab1 = window.DocumentTabItems.Single();
Assert.Equal(Path.GetFileName(fileName), tab1.TabName);
Assert.Equal("Hello World", tab1.RichTextView.RichTextBox.Text);
AssertTextEqual("Hello World", tab1.RichTextView.RichTextBox);
tab1.RichTextView.RichTextBox.Text = "- ";
Assert.Equal(Path.GetFileName(fileName) + "*", tab1.TabName);

Expand All @@ -167,7 +177,7 @@ public void MultipleNewSaveRestartOpenRecentChangeAskToSave()
fileRibbonMenu.RecentFileListItems[^1].OpenFileButton.Click();
tab2 = window.DocumentTab.SelectedTabItem.As<DocumentTabItem>();
Assert.Equal(Path.GetFileName(fileName2), tab2.TabName);
Assert.Equal("Hello World 2", tab2.RichTextView.RichTextBox.Text);
AssertTextEqual("Hello World 2", tab2.RichTextView.RichTextBox);

// Close app, save dialog shows just file #1, don't save
fileRibbonMenu = window.FileRibbonMenu;
Expand All @@ -186,17 +196,19 @@ public void MultipleNewSaveRestartOpenRecentChangeAskToSave()
Assert.Equal([ fileName, fileName2 ], startView.RecentFileListItems.Select(x => x.ToolTip));
Assert.True(startView.RecentFileListItems[0].PinButton.IsToggled);
var contextMenu = startView.RecentFileListItems[0].ShowContextMenu();
UIAssert.NotExists(() => _ = contextMenu.PinFileMenuItem);
contextMenu.UnpinFileMenuItem.Invoke();
Assert.False(startView.RecentFileListItems[0].PinButton.IsToggled);

contextMenu = startView.RecentFileListItems[0].ShowContextMenu();
UIAssert.NotExists(() => _ = contextMenu.UnpinFileMenuItem);
contextMenu.RemoveFileMenuItem.Invoke();
Assert.Equal([ fileName2 ], startView.RecentFileListItems.Select(x => x.ToolTip));

contextMenu = startView.RecentFileListItems[0].ShowContextMenu();
contextMenu.OpenFileMenuItem.Invoke();
tab1 = window.DocumentTab.SelectedTabItem.As<DocumentTabItem>();
Assert.Equal(Path.GetFileName(fileName2), tab1.TabName);
Assert.Equal("Hello World 2", tab1.RichTextView.RichTextBox.Text);
AssertTextEqual("Hello World 2", tab1.RichTextView.RichTextBox);
}
}
30 changes: 30 additions & 0 deletions src/Samples.UITest/Writer.Test/UIAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using FlaUI.Core.Tools;
using System.Runtime.CompilerServices;

namespace UITest.Writer;

public class ElementFoundException(string message, Exception? innerException = null) : Exception(message, innerException) { }

public static class UIAssert
{
public static void NotExists(Action accessElement, [CallerArgumentExpression(nameof(accessElement))]string? argumentExpression = null)
{
var notFound = false;
var timeout = Retry.DefaultTimeout;
Retry.DefaultTimeout = TimeSpan.Zero;
try
{

accessElement();
}
catch (ElementNotFoundException)
{
notFound = true;
}
finally
{
Retry.DefaultTimeout = timeout;
}
if (!notFound) throw new ElementFoundException($"Element found with '{argumentExpression}'");
}
}

0 comments on commit f8c8558

Please sign in to comment.