Skip to content

Commit

Permalink
UITest: fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Feb 25, 2025
1 parent 579cd08 commit 7e7d8c4
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 36 deletions.
10 changes: 5 additions & 5 deletions src/Samples.UITest/BookLibrary.Test/Tests/AddressBookTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public void SearchPersonListAndChangeEntriesTest()
Log.WriteLine($"List of Persons ({rowCount}):");
for (int i = 0; i < rowCount; i++)
{
Log.WriteLine($"{i:00}: {personListView.PersonDataGrid.GetRowByIndex(i).As<PersonGridRow>().FirstnameCell.Label.Text}");
Log.WriteLine($"{i:00}: {personListView.PersonDataGrid.GetRowByIndex(i)!.As<PersonGridRow>().FirstnameCell.Label.Text}");
}

personListView.SearchBox.Text = "H";
Assert.Equal(2, personListView.PersonDataGrid.RowCount);
personListView.SearchBox.Text = "Ha";
Assert.Equal(1, personListView.PersonDataGrid.RowCount);
var personRow1 = personListView.PersonDataGrid.GetRowByIndex(0).As<PersonGridRow>();
var personRow1 = personListView.PersonDataGrid.GetRowByIndex(0)!.As<PersonGridRow>();
personRow1.Select();

AssertEqual("Harry", personRow1.FirstnameCell.Name, personView.FirstnameTextBox.Text);
Expand Down Expand Up @@ -59,7 +59,7 @@ public void AddAndRemoveEntriesTest()
Assert.Equal(4, personListView.PersonDataGrid.RowCount);
personListView.AddButton.Click();
Assert.Equal(5, personListView.PersonDataGrid.RowCount);
var newRow = personListView.PersonDataGrid.SelectedItem.As<PersonGridRow>();
var newRow = personListView.PersonDataGrid.SelectedItem!.As<PersonGridRow>();
Assert.Equal(personListView.PersonDataGrid.Rows[^1].As<PersonGridRow>().ToTuple(), newRow.ToTuple());

// ItemStatus contains the validation error message or string.Empty if no error exists
Expand All @@ -76,7 +76,7 @@ public void AddAndRemoveEntriesTest()
Assert.Equal("ALastname", personView.LastnameTextBox.Text);
AssertEqual("", personView.LastnameTextBox.ItemStatus, newRow.LastnameCell.Label.ItemStatus);

var secondLastRow = personListView.PersonDataGrid.GetRowByIndex(personListView.PersonDataGrid.RowCount - 2).As<PersonGridRow>();
var secondLastRow = personListView.PersonDataGrid.GetRowByIndex(personListView.PersonDataGrid.RowCount - 2)!.As<PersonGridRow>();
Assert.False(secondLastRow.IsOffscreen);
Assert.StartsWith("Ron", secondLastRow.FirstnameCell.Name);

Expand Down Expand Up @@ -120,7 +120,7 @@ public void ValidateFieldsTest()
window.TabControl.AddressBookTabItem.Select();
var personListView = window.TabControl.AddressBookTabItem.PersonListView;
var personView = window.TabControl.AddressBookTabItem.PersonView;
var row1 = personListView.PersonDataGrid.SelectedItem.As<PersonGridRow>();
var row1 = personListView.PersonDataGrid.SelectedItem!.As<PersonGridRow>();

var text31 = new string('a', 31);
var text101 = new string('a', 101);
Expand Down
26 changes: 13 additions & 13 deletions src/Samples.UITest/BookLibrary.Test/Tests/BookLibraryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public void SearchBookListAndChangeEntriesTest()
Log.WriteLine($"List of Books ({rowCount}):");
for (int i = 0; i < rowCount; i++)
{
Log.WriteLine($"{i:00}: {bookListView.BookDataGrid.GetRowByIndex(i).As<BookGridRow>().TitleCell.Label.Text}");
Log.WriteLine($"{i:00}: {bookListView.BookDataGrid.GetRowByIndex(i)!.As<BookGridRow>().TitleCell.Label.Text}");
}
var firstBook = bookListView.BookDataGrid.GetRowByIndex(0).As<BookGridRow>();
var lastBook = bookListView.BookDataGrid.GetRowByIndex(rowCount - 1).As<BookGridRow>();
var firstBook = bookListView.BookDataGrid.GetRowByIndex(0)!.As<BookGridRow>();
var lastBook = bookListView.BookDataGrid.GetRowByIndex(rowCount - 1)!.As<BookGridRow>();
// GetRowByIndex scrolls to the item -> let's scroll back to the first book
firstBook.ScrollIntoView();
Assert.False(firstBook.IsOffscreen);
Expand All @@ -32,7 +32,7 @@ public void SearchBookListAndChangeEntriesTest()
Assert.Equal(13, bookListView.BookDataGrid.RowCount);
bookListView.SearchBox.Text = "Harr";
Assert.Equal(7, bookListView.BookDataGrid.RowCount);
var bookRow1 = bookListView.BookDataGrid.GetRowByIndex(0).As<BookGridRow>();
var bookRow1 = bookListView.BookDataGrid.GetRowByIndex(0)!.As<BookGridRow>();
bookRow1.Select();

AssertEqual("Harry Potter and the Deathly Hallows", bookRow1.TitleCell.Name, bookView.TitleTextBox.Text);
Expand All @@ -41,7 +41,7 @@ public void SearchBookListAndChangeEntriesTest()
Assert.Equal("1/1/2007", bookRow1.PublishDateCell.Name);
Assert.Equal(new DateTime(2007, 1, 1), bookView.PublishDatePicker.SelectedDate);
Assert.Equal("9780747591054", bookView.IsbnTextBox.Text);
Assert.Equal("English", bookView.LanguageComboBox.SelectedItem.Text);
Assert.Equal("English", bookView.LanguageComboBox.SelectedItem?.Text);
Assert.Equal("607", bookView.PagesTextBox.Text);
AssertEqual("Ginny Weasley", bookRow1.LendToCell.LendToLabel.Name, bookView.LendToTextBox.Text);

Expand All @@ -54,14 +54,14 @@ public void SearchBookListAndChangeEntriesTest()
Assert.Equal(["Undefined", "English", "German", "French", "Spanish", "Chinese", "Japanese"], bookView.LanguageComboBox.Items.Select(x => x.Name));
bookView.LanguageComboBox.Select(2);
bookView.LanguageComboBox.Click(); // To close the combo box popup
Assert.Equal("German", bookView.LanguageComboBox.SelectedItem.Text);
Assert.Equal("German", bookView.LanguageComboBox.SelectedItem?.Text);

bookView.LendToButton.Click();
var lendToWindow = window.FirstModalWindow().As<LendToWindow>();
Assert.False(lendToWindow.WasReturnedRadioButton.IsChecked);
Assert.True(lendToWindow.LendToRadioButton.IsChecked);
Assert.True(lendToWindow.PersonListBox.IsEnabled);
Assert.Equal("Ginny", lendToWindow.PersonListBox.SelectedItem.Text);
Assert.Equal("Ginny", lendToWindow.PersonListBox.SelectedItem?.Text);
lendToWindow.WasReturnedRadioButton.Click();
Assert.False(lendToWindow.PersonListBox.IsEnabled);
lendToWindow.OkButton.Click();
Expand Down Expand Up @@ -96,7 +96,7 @@ public void AddAndRemoveEntriesTest()
Assert.Equal(41, bookListView.BookDataGrid.RowCount);
bookListView.AddButton.Click();
Assert.Equal(42, bookListView.BookDataGrid.RowCount);
var newRow = bookListView.BookDataGrid.SelectedItem.As<BookGridRow>();
var newRow = bookListView.BookDataGrid.SelectedItem!.As<BookGridRow>();
Assert.Equal(bookListView.BookDataGrid.Rows[^1], newRow);

// ItemStatus contains the validation error message or string.Empty if no error exists
Expand All @@ -113,7 +113,7 @@ public void AddAndRemoveEntriesTest()
Assert.Equal("TAuthor", bookView.AuthorTextBox.Text);
AssertEqual("", bookView.AuthorTextBox.ItemStatus, newRow.AuthorCell.Label.ItemStatus);

var secondLastRow = bookListView.BookDataGrid.GetRowByIndex(bookListView.BookDataGrid.RowCount - 2).As<BookGridRow>();
var secondLastRow = bookListView.BookDataGrid.GetRowByIndex(bookListView.BookDataGrid.RowCount - 2)!.As<BookGridRow>();
Assert.False(secondLastRow.IsOffscreen);
Assert.StartsWith("WPF", secondLastRow.TitleCell.Name);

Expand Down Expand Up @@ -155,7 +155,7 @@ public void ValidateFieldsTest()
var window = GetShellWindow();
var bookListView = window.TabControl.BookLibraryTabItem.BookListView;
var bookView = window.TabControl.BookLibraryTabItem.BookView;
var row1 = bookListView.BookDataGrid.SelectedItem.As<BookGridRow>();
var row1 = bookListView.BookDataGrid.SelectedItem!.As<BookGridRow>();

var text101 = new string('a', 101);

Expand Down Expand Up @@ -197,15 +197,15 @@ public void RemoveLendToPersonTest()
var bookView = window.TabControl.BookLibraryTabItem.BookView;

bookListView.SearchBox.Text = "Harr";
var bookRow1 = bookListView.BookDataGrid.GetRowByIndex(0).As<BookGridRow>();
var bookRow1 = bookListView.BookDataGrid.GetRowByIndex(0)!.As<BookGridRow>();
bookRow1.Select();

AssertEqual("Ginny Weasley", bookRow1.LendToCell.LendToLabel.Name, bookView.LendToTextBox.Text);

window.TabControl.AddressBookTabItem.Select();
var personListView = window.TabControl.AddressBookTabItem.PersonListView;
personListView.SearchBox.Text = "Ginny";
var personRow = personListView.PersonDataGrid.GetRowByIndex(0).As<PersonGridRow>();
var personRow = personListView.PersonDataGrid.GetRowByIndex(0)!.As<PersonGridRow>();
personRow.Select();
personListView.RemoveButton.Click();

Expand All @@ -223,7 +223,7 @@ public void RemoveLendToPersonTest()
bookView = window.TabControl.BookLibraryTabItem.BookView;

bookListView.SearchBox.Text = "Harr";
bookRow1 = bookListView.BookDataGrid.GetRowByIndex(1).As<BookGridRow>();
bookRow1 = bookListView.BookDataGrid.GetRowByIndex(1)!.As<BookGridRow>();
bookRow1.Select();

window.TabControl.BookLibraryTabItem.Select();
Expand Down
2 changes: 1 addition & 1 deletion src/Samples.UITest/BookLibrary.Test/UITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Application Launch(LaunchArguments? arguments = null, bool resetSettings
return App = Application.Launch(Executable, args);
}

public ShellWindow GetShellWindow() => App!.GetMainWindow(Automation).As<ShellWindow>();
public ShellWindow GetShellWindow() => (App!.GetMainWindow(Automation) ?? throw new ElementFoundException("MainWindow not found")).As<ShellWindow>();
}

public record LaunchArguments(string? UICulture = "en-US", string? Culture = "en-US", string? AdditionalArguments = null) : LaunchArgumentsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void SearchContactsAndChangeEntriesTest()

AssertContactItem(contactListView.ContactItems[0], contactView, "Jesper", "Aaberg", "[email protected]", "(111) 555-0100");
AssertContactItem(contactListView.ContactItems[^1], null, "Miles", "Reid", "[email protected]", "(444) 555-0123");
Assert.Equal(contactListView.ContactList.SelectedItem.As<ContactListItem>().ToTuple(), contactListView.ContactItems[0].ToTuple());
Assert.Equal(contactListView.ContactList.SelectedItem!.As<ContactListItem>().ToTuple(), contactListView.ContactItems[0].ToTuple());
Assert.Equal("Main St. 4567", contactView.StreetBox.Text);
Assert.Equal("Buffalo", contactView.CityBox.Text);
Assert.Equal("New York", contactView.StateBox.Text);
Expand All @@ -38,7 +38,7 @@ public void SearchContactsAndChangeEntriesTest()
Assert.Equal(2, contactListView.ContactItems.Count);
Assert.Null(contactListView.ContactList.SelectedItem);
contactListView.ContactList.Select(0);
var item = contactListView.ContactList.SelectedItem.As<ContactListItem>();
var item = contactListView.ContactList.SelectedItem!.As<ContactListItem>();
AssertContactItem(item, contactView, "Michael", "Pfeiffer", "[email protected]", "(222) 555-0105");
contactView.FirstnameBox.Text = "TFirstname";
Assert.Equal("TFirstname", item.FirstnameLabel.Text);
Expand All @@ -61,7 +61,7 @@ public void AddAndRemoveEntriesTest()
Assert.Equal(5, contactListView.ContactItems.Count);
window.NewContactCommand.Click();
Assert.Equal(6, contactListView.ContactItems.Count);
var newItem = contactListView.ContactList.SelectedItem.As<ContactListItem>();
var newItem = contactListView.ContactList.SelectedItem!.As<ContactListItem>();

// ItemStatus contains the validation error message or string.Empty if no error exists
AssertContactItem(null, contactView, "", "", "", "");
Expand All @@ -80,7 +80,7 @@ public void AddAndRemoveEntriesTest()
contactListView.ContactList.Select(0);
window.DeleteCommand.Click();
Assert.Equal(5, contactListView.ContactItems.Count);
Assert.Equal(secondItem.FirstnameLabel.Text, contactListView.ContactList.SelectedItem.As<ContactListItem>().FirstnameLabel.Text);
Assert.Equal(secondItem.FirstnameLabel.Text, contactListView.ContactList.SelectedItem!.As<ContactListItem>().FirstnameLabel.Text);

window.ExitButton.Click();

Expand All @@ -107,7 +107,6 @@ public void AddAndRemoveEntriesTest()
window.RootTreeItem.ContactsNode.Click();
contactListView = window.ContactLayoutView.ContactListView;
contactView = window.ContactLayoutView.ContactView;
newItem = contactListView.ContactItems[^1];
contactListView.ContactList.Select(contactListView.ContactItems.Count - 1);
Assert.Equal("The Firstname field is required.", contactView.FirstnameBox.ItemStatus);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void RemoveEmailsTest()

window.RootTreeItem.SentNode.Select();
emailListView = window.EmailLayoutView.EmailListView;
emailView = window.EmailLayoutView.EmailView;
Assert.Equal(5, emailListView.EmailItems.Count);
for (int i = 0; i < 5; i++) window.DeleteEmailCommand.Click();
Assert.Empty(emailListView.EmailItems);
Expand All @@ -99,7 +98,6 @@ public void RemoveEmailsTest()

window.RootTreeItem.SentNode.Select();
emailListView = window.EmailLayoutView.EmailListView;
emailView = window.EmailLayoutView.EmailView;
Assert.Empty(emailListView.EmailItems);
}

Expand Down Expand Up @@ -259,7 +257,7 @@ public void EmailAccountsTest()
Assert.Equal(2, newEmailWindow.EmailAccountsComboBox.Items.Length);
newEmailWindow.EmailAccountsComboBox.Select(1);
newEmailWindow.EmailAccountsComboBox.Click(); // To close the combo box popup
Assert.Equal("Lea Thompson", newEmailWindow.EmailAccountsComboBox.SelectedItem.Text);
Assert.Equal("Lea Thompson", newEmailWindow.EmailAccountsComboBox.SelectedItem?.Text);
newEmailWindow.CloseButton.Click();

// Email accounts -> remove all of them
Expand Down
2 changes: 1 addition & 1 deletion src/Samples.UITest/InformationManager.Test/UITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Application Launch(LaunchArguments? arguments = null, bool resetSettings
return App = Application.Launch(Executable, args);
}

public ShellWindow GetShellWindow() => App!.GetMainWindow(Automation).As<ShellWindow>();
public ShellWindow GetShellWindow() => (App!.GetMainWindow(Automation) ?? throw new ElementFoundException("MainWindow not found")).As<ShellWindow>();
}

public record LaunchArguments(string? UICulture = "en-US", string? Culture = "en-US", string? AdditionalArguments = null) : LaunchArgumentsBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace UITest.Controls;

public class HyperlinkGridCell(FrameworkAutomationElementBase element) : GridCell(element)
{
public AutomationElement RootElement => FindFirstChild();
public AutomationElement RootElement => FindFirstChild() ?? throw new ElementNotFoundException("RootElement was not found");

public AutomationElement Hyperlink => this.Find(x => x.ByControlType(ControlType.Hyperlink));

Expand Down
5 changes: 3 additions & 2 deletions src/Samples.UITest/UITest.Core/UITestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static Window GetWindow(this AutomationElement element)
public static Menu ShowContextMenu(this AutomationElement element)
{
element.RightClick();
return element.GetWindow().ContextMenu.AsMenu();
return (element.GetWindow().ContextMenu ?? throw new ElementFoundException("The ContextMenu was not found.")).AsMenu();
}

public static Window FirstModalWindow(this Window window, TimeSpan? timeout = null)
Expand Down Expand Up @@ -76,6 +76,7 @@ static void GetTreeCore(StringBuilder sb, AutomationElement element, string padd
private static string? TryAutomationId(this AutomationElement element)
{
try { return element.Properties.AutomationId.ValueOrDefault; }
catch { } return null;
catch { /* ignore exception */ }
return null;
}
}
8 changes: 4 additions & 4 deletions src/Samples.UITest/Writer.Test/Tests/WriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using UITest.Writer.Views;
using Xunit;

namespace UITest.Writer;
namespace UITest.Writer.Tests;

public class WriterTest : UITest
{
Expand Down Expand Up @@ -194,7 +194,7 @@ public void MultipleNewSaveRestartOpenRecentChangeAskToSave()
// Create new document #3, check tab name, close tab - no save dialog comes as text has not been changed
fileRibbonMenu.MenuButton.Click();
fileRibbonMenu.NewMenuItem.Click();
var tab3 = window.DocumentTab.SelectedTabItem.As<DocumentTabItem>();
var tab3 = window.DocumentTab.SelectedTabItem!.As<DocumentTabItem>();
Assert.Equal("Document 3.rtf", tab3.TabName);
tab3.CloseButton.Click();

Expand Down Expand Up @@ -256,7 +256,7 @@ public void MultipleNewSaveRestartOpenRecentChangeAskToSave()
// Open recent file #2
fileRibbonMenu.MenuButton.Click();
fileRibbonMenu.RecentFileListItems[^1].OpenFileButton.Click();
tab2 = window.DocumentTab.SelectedTabItem.As<DocumentTabItem>();
tab2 = window.DocumentTab.SelectedTabItem!.As<DocumentTabItem>();
Assert.Equal(Path.GetFileName(fileName2), tab2.TabName);
AssertTextEqual("Hello World 2", tab2.RichTextView.RichTextBox);

Expand Down Expand Up @@ -288,7 +288,7 @@ public void MultipleNewSaveRestartOpenRecentChangeAskToSave()

contextMenu = startView.RecentFileListItems[0].ShowContextMenu();
contextMenu.OpenFileMenuItem.Click();
tab1 = window.DocumentTab.SelectedTabItem.As<DocumentTabItem>();
tab1 = window.DocumentTab.SelectedTabItem!.As<DocumentTabItem>();
Assert.Equal(Path.GetFileName(fileName2), tab1.TabName);
AssertTextEqual("Hello World 2", tab1.RichTextView.RichTextBox);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Samples.UITest/Writer.Test/UITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Application Launch(LaunchArguments? arguments = null, bool resetSettings
return App = Application.Launch(Executable, args);
}

public ShellWindow GetShellWindow() => App!.GetMainWindow(Automation).As<ShellWindow>();
public ShellWindow GetShellWindow() => (App!.GetMainWindow(Automation) ?? throw new ElementFoundException("MainWindow not found")).As<ShellWindow>();
}

public record LaunchArguments(string? UICulture = "en-US", string? Culture = "en-US", string? AdditionalArguments = null) : LaunchArgumentsBase
Expand Down

0 comments on commit 7e7d8c4

Please sign in to comment.