|
| 1 | +using FlaUI.Core.AutomationElements; |
| 2 | +using UITest.BookLibrary.Views; |
| 3 | +using Xunit.Abstractions; |
| 4 | +using Xunit; |
| 5 | +using UITest.SystemViews; |
| 6 | + |
| 7 | +namespace UITest.BookLibrary.Tests; |
| 8 | + |
| 9 | +public class AddressBookTest(ITestOutputHelper log) : UITest(log) |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public void SearchPersonListAndChangeEntriesTest() => Run(() => |
| 13 | + { |
| 14 | + Launch(); |
| 15 | + var window = GetShellWindow(); |
| 16 | + window.TabControl.AddressBookTabItem.Select(); |
| 17 | + var personListView = window.TabControl.AddressBookTabItem.PersonListView; |
| 18 | + var personView = window.TabControl.AddressBookTabItem.PersonView; |
| 19 | + |
| 20 | + var rowCount = personListView.PersonDataGrid.RowCount; |
| 21 | + Assert.Equal(4, rowCount); |
| 22 | + Log.WriteLine($"List of Persons ({rowCount}):"); |
| 23 | + for (int i = 0; i < rowCount; i++) |
| 24 | + { |
| 25 | + Log.WriteLine($"{i:00}: {personListView.PersonDataGrid.GetRowByIndex(i).As<PersonGridRow>().FirstnameCell.Label.Text}"); |
| 26 | + } |
| 27 | + |
| 28 | + personListView.SearchBox.Text = "H"; |
| 29 | + Assert.Equal(2, personListView.PersonDataGrid.RowCount); |
| 30 | + personListView.SearchBox.Text = "Ha"; |
| 31 | + Assert.Equal(1, personListView.PersonDataGrid.RowCount); |
| 32 | + var personRow1 = personListView.PersonDataGrid.GetRowByIndex(0).As<PersonGridRow>(); |
| 33 | + personRow1.Select(); |
| 34 | + |
| 35 | + AssertEqual("Harry", personRow1.FirstnameCell.Name, personView.FirstnameTextBox.Text); |
| 36 | + AssertEqual("Potter", personRow1.LastnameCell.Name, personView.LastnameTextBox.Text); |
| 37 | + AssertEqual("[email protected]", personRow1.EmailCell.Label.Text, personView.EmailTextBox.Text); |
| 38 | + |
| 39 | + personView.FirstnameTextBox.Text = "TFirstname"; |
| 40 | + Assert.Equal("TFirstname", personRow1.FirstnameCell.Name); |
| 41 | + personView.LastnameTextBox.Text = "TLastname"; |
| 42 | + Assert.Equal("TLastname", personRow1.LastnameCell.Name); |
| 43 | + personView.EmailTextBox.Text = "[email protected]"; |
| 44 | + Assert.Equal("[email protected]", personRow1.EmailCell.Label.Text); |
| 45 | + |
| 46 | + window.Close(); |
| 47 | + var messageBox = window.FirstModalWindow().As<MessageBox>(); // MessageBox that asks user to save the changes |
| 48 | + messageBox.Buttons[1].Click(); // No button |
| 49 | + }); |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void AddAndRemoveEntriesTest() => Run(() => |
| 53 | + { |
| 54 | + Launch(); |
| 55 | + var window = GetShellWindow(); |
| 56 | + window.TabControl.AddressBookTabItem.Select(); |
| 57 | + var personListView = window.TabControl.AddressBookTabItem.PersonListView; |
| 58 | + var personView = window.TabControl.AddressBookTabItem.PersonView; |
| 59 | + |
| 60 | + Assert.Equal(4, personListView.PersonDataGrid.RowCount); |
| 61 | + personListView.AddButton.Click(); |
| 62 | + Assert.Equal(5, personListView.PersonDataGrid.RowCount); |
| 63 | + var newRow = personListView.PersonDataGrid.SelectedItem.As<PersonGridRow>(); |
| 64 | + Assert.Equal(personListView.PersonDataGrid.Rows[0], newRow); |
| 65 | + |
| 66 | + // ItemStatus contains the validation error message or string.Empty if no error exists |
| 67 | + AssertEqual("", personView.FirstnameTextBox.Text, newRow.FirstnameCell.Label.Text); |
| 68 | + AssertEqual("Firstname is mandatory.", personView.FirstnameTextBox.ItemStatus, newRow.FirstnameCell.Label.ItemStatus); |
| 69 | + AssertEqual("", personView.LastnameTextBox.Text, newRow.LastnameCell.Label.Text); |
| 70 | + AssertEqual("Lastname is mandatory.", personView.LastnameTextBox.ItemStatus, newRow.LastnameCell.Label.ItemStatus); |
| 71 | + AssertEqual("", personView.EmailTextBox.ItemStatus, newRow.EmailCell.Label.Text); |
| 72 | + |
| 73 | + personView.FirstnameTextBox.Text = "AFirstname"; |
| 74 | + Assert.Equal("AFirstname", newRow.FirstnameCell.Name); |
| 75 | + AssertEqual("", personView.FirstnameTextBox.ItemStatus, newRow.FirstnameCell.Label.ItemStatus); |
| 76 | + personView.LastnameTextBox.Text = "ALastname"; |
| 77 | + Assert.Equal("ALastname", newRow.LastnameCell.Name); |
| 78 | + AssertEqual("", personView.LastnameTextBox.ItemStatus, newRow.LastnameCell.Label.ItemStatus); |
| 79 | + |
| 80 | + var lastRow = personListView.PersonDataGrid.GetRowByIndex(personListView.PersonDataGrid.RowCount - 1).As<PersonGridRow>(); |
| 81 | + Assert.False(lastRow.IsOffscreen); |
| 82 | + Assert.StartsWith("Ron", lastRow.FirstnameCell.Name); |
| 83 | + |
| 84 | + var lastNotRemovedRow = personListView.PersonDataGrid.GetRowByIndex(personListView.PersonDataGrid.RowCount - 3); |
| 85 | + personListView.PersonDataGrid.Select(personListView.PersonDataGrid.RowCount - 2); |
| 86 | + personListView.PersonDataGrid.AddToSelection(personListView.PersonDataGrid.RowCount - 1); |
| 87 | + personListView.RemoveButton.Click(); |
| 88 | + Assert.Equal(3, personListView.PersonDataGrid.RowCount); |
| 89 | + Assert.Equal(lastNotRemovedRow, personListView.PersonDataGrid.SelectedItem); |
| 90 | + Assert.Equal(lastNotRemovedRow, personListView.PersonDataGrid.Rows[^1]); |
| 91 | + |
| 92 | + window.DataMenu.Click(); |
| 93 | + window.DataMenu.SaveMenuItem.Click(); |
| 94 | + window.Close(); |
| 95 | + |
| 96 | + |
| 97 | + Launch(resetSettings: false, resetDatabase: false); |
| 98 | + window = GetShellWindow(); |
| 99 | + window.TabControl.AddressBookTabItem.Select(); |
| 100 | + personListView = window.TabControl.AddressBookTabItem.PersonListView; |
| 101 | + Assert.Equal(3, personListView.PersonDataGrid.RowCount); |
| 102 | + |
| 103 | + personListView.PersonDataGrid.Select(0); |
| 104 | + for (int i = 1; i < personListView.PersonDataGrid.RowCount; i++) personListView.PersonDataGrid.AddToSelection(i); |
| 105 | + Assert.Equal(3, personListView.PersonDataGrid.SelectedItems.Length); |
| 106 | + Assert.True(personListView.RemoveButton.IsEnabled); |
| 107 | + personListView.RemoveButton.Click(); |
| 108 | + Assert.False(personListView.RemoveButton.IsEnabled); |
| 109 | + Assert.Null(personListView.PersonDataGrid.SelectedItem); |
| 110 | + |
| 111 | + window.Close(); |
| 112 | + var messageBox = window.FirstModalWindow().As<MessageBox>(); // MessageBox that asks user to save the changes |
| 113 | + messageBox.Buttons[1].Click(); // No button |
| 114 | + }); |
| 115 | + |
| 116 | + [Fact] |
| 117 | + public void ValidateFieldsTest() => Run(() => |
| 118 | + { |
| 119 | + Launch(); |
| 120 | + var window = GetShellWindow(); |
| 121 | + window.TabControl.AddressBookTabItem.Select(); |
| 122 | + var personListView = window.TabControl.AddressBookTabItem.PersonListView; |
| 123 | + var personView = window.TabControl.AddressBookTabItem.PersonView; |
| 124 | + var row1 = personListView.PersonDataGrid.SelectedItem.As<PersonGridRow>(); |
| 125 | + |
| 126 | + var text31 = new string('a', 31); |
| 127 | + var text101 = new string('a', 101); |
| 128 | + |
| 129 | + // Note: "Required" validation of Title and Author are covered by the AddAndRemoveEntriesTest test |
| 130 | + // ItemStatus contains the validation error message or string.Empty if no error exists |
| 131 | + personView.FirstnameTextBox.Text = text31; |
| 132 | + AssertEqual("Firstname can contain 30 characters at maximum.", personView.FirstnameTextBox.ItemStatus, row1.FirstnameCell.Label.ItemStatus); |
| 133 | + |
| 134 | + personView.LastnameTextBox.Text = text31; |
| 135 | + AssertEqual("Lastname can contain 30 characters at maximum.", personView.LastnameTextBox.ItemStatus, row1.LastnameCell.Label.ItemStatus); |
| 136 | + |
| 137 | + personView.EmailTextBox.Text = text101; |
| 138 | + Assert.Equal("Email can contain 100 characters at maximum.\r\nThe provided email address is invalid.", personView.EmailTextBox.ItemStatus); |
| 139 | + personView.EmailTextBox.Text = "test"; |
| 140 | + Assert.Equal("The provided email address is invalid.", personView.EmailTextBox.ItemStatus); |
| 141 | + personView.EmailTextBox.Text = "[email protected]"; |
| 142 | + Assert.Empty(personView.EmailTextBox.ItemStatus); |
| 143 | + |
| 144 | + window.Close(); |
| 145 | + var messageBox = window.FirstModalWindow().As<MessageBox>(); // MessageBox that asks user to really close the app although unsafed changes exist |
| 146 | + messageBox.Buttons[0].Click(); // Yes button |
| 147 | + }); |
| 148 | + |
| 149 | + private static void AssertEqual(string expected, string actual1, string actual2) |
| 150 | + { |
| 151 | + Assert.Equal(expected, actual1); |
| 152 | + Assert.Equal(expected, actual2); |
| 153 | + } |
| 154 | +} |
0 commit comments