Skip to content

Commit

Permalink
UITest/InfoMan: Add NewEmailTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Dec 11, 2024
1 parent d415f9c commit fd05fcf
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Samples.UITest/BookLibrary.Test/Views/BookListView.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using UITest.BookLibrary.Controls;
using UITest.Controls;

namespace UITest.BookLibrary.Views;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FlaUI.Core.AutomationElements;
using FlaUI.Core;
using UITest.BookLibrary.Controls;
using UITest.Controls;

namespace UITest.BookLibrary.Views;

Expand Down
97 changes: 93 additions & 4 deletions src/Samples.UITest/InformationManager.Test/Tests/EmailTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FlaUI.Core.AutomationElements;
using UITest.InformationManager.Controls;
using UITest.InformationManager.Views;
using UITest.SystemViews;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -23,7 +23,7 @@ public void SearchEmailsAndAssertLists() => Run(() =>
for (int i = 0; i < count; i++) Log.WriteLine($"{i:00}: {emailListView.EmailItems[i].ToReceivedTuple()}");

AssertEmail(true, emailListView.EmailItems[0], emailView, "[email protected]", "[email protected]", "8/9/2012", "5:58:21 AM", "Nunc sed dis suscipit");
Assert.Equal("Scelerisque est odio", emailView.Document.As<Document>().GetText(20));
Assert.Equal("Scelerisque est odio", emailView.Document.GetText(20));

Assert.Equal("Search", emailListView.SearchBox.SearchHintLabel.Text);
emailListView.SearchBox.SearchTextBox.Text = "!";
Expand Down Expand Up @@ -104,7 +104,89 @@ public void RemoveEmailsTest() => Run(() =>
Assert.Empty(emailListView.EmailItems);
});

private static void AssertEmail(bool received, EmailListItem? emailItem, EmailView? emailView, string from, string to, string sentDate, string sentTime, string title)
[Fact]
public void NewEmailTest() => Run(() =>
{
Launch();
var window = GetShellWindow();
window.RootTreeItem.SentNode.Select();
var emailListView = window.EmailLayoutView.EmailListView;
var emailView = window.EmailLayoutView.EmailView;
Assert.Equal(5, emailListView.EmailItems.Count);

window.NewEmailCommand.Click();
var newEmailWindow = window.NewEmailWindows[0];
newEmailWindow.SendButton.Click();

// ItemStatus contains the validation error message or string.Empty if no error exists
Assert.Equal("", newEmailWindow.ToTextBox.Text);
Assert.Equal("This email doesn't define a recipient.", newEmailWindow.ToTextBox.ItemStatus);
Assert.Equal("", newEmailWindow.CCTextBox.Text);
Assert.Empty(newEmailWindow.CCTextBox.ItemStatus);

newEmailWindow.SendButton.Click();
var errorBox = newEmailWindow.FirstModalWindow().As<MessageBox>();
Assert.Contains("One or more fields are not valid.", errorBox.Message);
errorBox.Buttons[0].Click();

newEmailWindow.ToTextBox.Text = "[email protected]";
newEmailWindow.CCTextBox.Click();
Assert.Empty(newEmailWindow.ToTextBox.ItemStatus);

newEmailWindow.CCSelectContactButton.Click();
var contactWindow = newEmailWindow.FirstModalWindow().As<SelectContactWindow>();
Assert.Equal(5, contactWindow.ContactListView.ContactItems.Count);
Assert.True(contactWindow.ContactListView.ContactItems[0].IsSelected);
contactWindow.ContactListView.SearchBox.SearchTextBox.Text = "Mi";
Assert.Equal(2, contactWindow.ContactListView.ContactItems.Count);
Assert.False(contactWindow.ContactListView.ContactItems[0].IsSelected);
contactWindow.ContactListView.ContactList.Items[0].Select();
contactWindow.OkButton.Click();
Assert.Equal("[email protected]", newEmailWindow.CCTextBox.Text);

newEmailWindow.BccSelectContactButton.Click();
contactWindow = newEmailWindow.FirstModalWindow().As<SelectContactWindow>();
contactWindow.CancelButton.Click();
Assert.Empty(newEmailWindow.BccTextBox.Text);
newEmailWindow.BccSelectContactButton.Click();
contactWindow = newEmailWindow.FirstModalWindow().As<SelectContactWindow>();
contactWindow.OkButton.Click();
Assert.Equal("[email protected]", newEmailWindow.BccTextBox.Text);

newEmailWindow.TitleTextBox.Text = "ATitle";
newEmailWindow.MessageTextBox.Text = "AMessage";
newEmailWindow.SendButton.Click();

Assert.Equal(6, emailListView.EmailItems.Count);
emailListView.EmailItems[0].Select();
AssertEmail(false, emailListView.EmailItems[0], emailView, "[email protected]", "[email protected]", DateTime.Now.ToShortDateString(), null, "ATitle");
Assert.Equal("AMessage", emailView.Document.GetText(20));


// Create email but do not send it -> Close
window.NewEmailCommand.Click();
newEmailWindow = window.NewEmailWindows[0];
newEmailWindow.ToTextBox.Text = "[email protected]";
newEmailWindow.TitleTextBox.Text = "Don't send";
newEmailWindow.CloseButton.Click();
Assert.Equal(6, emailListView.EmailItems.Count);
});

[Fact]
public void EmailAccountsTest() => Run(() =>
{
Launch();
var window = GetShellWindow();
window.EmailAccountsCommand.Click();
var emailAccountsWindow = window.FirstModalWindow().As<EmailAccountsWindow>();
var row0 = emailAccountsWindow.EmailAccountsDataGrid.Rows[0].As<EmailAccountGridRow>();

// TODO: Add test EmailAccountsTest()
// 2. Select email account (just one in ComboBox)

});

private static void AssertEmail(bool received, EmailListItem? emailItem, EmailView? emailView, string from, string to, string sentDate, string? sentTime, string title)
{
if (emailItem is not null)
{
Expand All @@ -113,6 +195,13 @@ private static void AssertEmail(bool received, EmailListItem? emailItem, EmailVi
}
if (emailView is not null)
{
Assert.Equal((from, to, sentDate + " " + sentTime, title), (emailView.FromLabel.Text, emailView.ToLabel.Text, emailView.SentLabel.Text, emailView.TitleLabel.Text)); }
var expectedDate = sentTime is null ? "" : sentDate + " " + sentTime;
var actualDate = sentTime is null ? "" : emailView.SentLabel.Text;
Assert.Equal((from, to, expectedDate, title), (emailView.FromLabel.Text, emailView.ToLabel.Text, actualDate, emailView.TitleLabel.Text));
if (sentTime is null)
{
Assert.Contains(expectedDate, actualDate);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using UITest.Controls;

namespace UITest.InformationManager.Views;

public class EmailAccountsWindow(FrameworkAutomationElementBase element) : Window(element)
{
public Button NewButton => this.Find("NewButton").AsButton();

public Button RemoveButton => this.Find("RemoveButton").AsButton();

public Button EditButton => this.Find("EditButton").AsButton();

public Button CloseButton => this.Find("CloseButton").AsButton();

public Grid EmailAccountsDataGrid => this.Find("EmailAccountsDataGrid").AsGrid();
}

public class EmailAccountGridRow(FrameworkAutomationElementBase element) : GridRow(element)
{
public TextGridCell NameCell => Cells[0].As<TextGridCell>();

public TextGridCell EmailCell => Cells[1].As<TextGridCell>();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Definitions;
using FlaUI.Core;
using UITest.InformationManager.Controls;

namespace UITest.InformationManager.Views;

Expand All @@ -18,5 +19,5 @@ public class EmailView(FrameworkAutomationElementBase element) : AutomationEleme

public Label SentLabel => this.Find("SentLabel").AsLabel();

public AutomationElement Document => this.Find(x => x.ByControlType(ControlType.Document));
public Document Document => this.Find(x => x.ByControlType(ControlType.Document)).As<Document>();
}
29 changes: 29 additions & 0 deletions src/Samples.UITest/InformationManager.Test/Views/NewEmailWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FlaUI.Core;
using FlaUI.Core.AutomationElements;

namespace UITest.InformationManager.Views;

public class NewEmailWindow(FrameworkAutomationElementBase element) : Window(element)
{
public Button SendButton => this.Find("SendButton").AsButton();

public Button CloseButton => this.Find("CloseButton").AsButton();

public ComboBox EmailAccountsComboBox => this.Find("EmailAccountsComboBox").AsComboBox();

public TextBox ToTextBox => this.Find("ToTextBox").AsTextBox();

public Button ToSelectContactButton => this.Find("ToSelectContactButton").AsButton();

public TextBox CCTextBox => this.Find("CCTextBox").AsTextBox();

public Button CCSelectContactButton => this.Find("CCSelectContactButton").AsButton();

public TextBox BccTextBox => this.Find("BccTextBox").AsTextBox();

public Button BccSelectContactButton => this.Find("BccSelectContactButton").AsButton();

public TextBox TitleTextBox => this.Find("TitleTextBox").AsTextBox();

public TextBox MessageTextBox => this.Find("MessageTextBox").AsTextBox();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using FlaUI.Core;
using FlaUI.Core.AutomationElements;

namespace UITest.InformationManager.Views;

public class SelectContactWindow(FrameworkAutomationElementBase element) : Window(element)
{
public ContactListView ContactListView => this.Find("ContactListView").As<ContactListView>();

public Button OkButton => this.Find("OkButton").AsButton();

public Button CancelButton => this.Find("CancelButton").AsButton();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace UITest.InformationManager.Views;

// TODO: Consider to rename ..Command to ..Button
public class ShellWindow(FrameworkAutomationElementBase element) : Window(element)
{
public Button NewEmailCommand => this.Find("NewEmailCommand").AsButton();
Expand All @@ -27,6 +28,8 @@ public class ShellWindow(FrameworkAutomationElementBase element) : Window(elemen

public ContactLayoutView ContactLayoutView => this.Find("ContactLayoutView").As<ContactLayoutView>();


public IReadOnlyList<NewEmailWindow> NewEmailWindows => this.FindAll("NewEmailWindow").Select(x => x.As<NewEmailWindow>()).ToArray();

public WindowVisualState WindowState
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Definitions;

namespace UITest.BookLibrary.Controls;
namespace UITest.Controls;

public class HyperlinkGridCell(FrameworkAutomationElementBase element) : GridCell(element)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using FlaUI.Core.Input;
using FlaUI.Core.WindowsAPI;

namespace UITest.BookLibrary.Controls;
namespace UITest.Controls;

public class TextGridCell(FrameworkAutomationElementBase element) : GridCell(element)
{
Expand All @@ -21,5 +21,5 @@ public string Text
TextBox.Text = value;
Keyboard.TypeSimultaneously(VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

<Border Grid.Row="1" Background="{x:Static SystemColors.ControlBrush}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="11">
<Button Command="{Binding OkCommand}" Content="_OK" IsDefault="True" Style="{StaticResource DialogButton}"/>
<Button Content="_Cancel" IsCancel="True" Style="{StaticResource DialogButton}" Margin="11,0,0,0"/>
<Button Command="{Binding OkCommand}" Content="_OK" IsDefault="True" Style="{StaticResource DialogButton}" AutomationProperties.AutomationId="OkButton"/>
<Button Content="_Cancel" IsCancel="True" Style="{StaticResource DialogButton}" Margin="11,0,0,0" AutomationProperties.AutomationId="CancelButton"/>
</StackPanel>
</Border>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public string Bcc

public void Close() => ViewCore.Close();

private static IReadOnlyList<string> ParseEmails(string text) => text.Trim().Split((char[])[ ';', ',', ' ' ], StringSplitOptions.RemoveEmptyEntries);
private static IReadOnlyList<string> ParseEmails(string text) => text.Trim().Split([ ';', ',', ' ' ], StringSplitOptions.RemoveEmptyEntries);

private static string FormatEmails(IEnumerable<string> emailList) => string.Join("; ", emailList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

<DockPanel>
<ToolBar DockPanel.Dock="Top">
<Button Command="{Binding NewAccountCommand}" ToolTip="Creates a new email account."><AccessText Text="_New account"/></Button>
<Button Command="{Binding RemoveAccountCommand}" ToolTip="Deletes the selected email account."><AccessText Text="_Delete"/></Button>
<Button Command="{Binding EditAccountCommand}" ToolTip="Edits the selected email account."><AccessText Text="_Edit"/></Button>
<Button IsCancel="True" ToolTip="Close this window."><AccessText Text="_Close"/></Button>
<Button Command="{Binding NewAccountCommand}" ToolTip="Creates a new email account." AutomationProperties.AutomationId="NewButton"><AccessText Text="_New account"/></Button>
<Button Command="{Binding RemoveAccountCommand}" ToolTip="Deletes the selected email account." AutomationProperties.AutomationId="RemoveButton"><AccessText Text="_Delete"/></Button>
<Button Command="{Binding EditAccountCommand}" ToolTip="Edits the selected email account."><AccessText Text="_Edit" AutomationProperties.AutomationId="EditButton"/></Button>
<Button IsCancel="True" ToolTip="Close this window." AutomationProperties.AutomationId="CloseButton"><AccessText Text="_Close"/></Button>
</ToolBar>

<DataGrid x:Name="emailAccountsGrid" ItemsSource="{Binding EmailClientRoot.EmailAccounts}" SelectedItem="{Binding SelectedEmailAccount}"
Background="{x:Static SystemColors.WindowBrush}" MouseDoubleClick="EmailAccountsGridMouseDoubleClick" SelectionMode="Single"
BorderThickness="0">
BorderThickness="0" AutomationProperties.AutomationId="EmailAccountsDataGrid">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name, Mode=OneWay}" IsReadOnly="True" Header="Name" Width="*"
ElementStyle="{StaticResource TextCellElementStyle}"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="New email" Icon="{StaticResource InformationManagerImageSource}" Height="370" Width="550"
d:DataContext="{d:DesignInstance dd:SampleNewEmailViewModel, IsDesignTimeCreatable=True}">
d:DataContext="{d:DesignInstance dd:SampleNewEmailViewModel, IsDesignTimeCreatable=True}" AutomationProperties.AutomationId="NewEmailWindow">

<DockPanel>
<ToolBar DockPanel.Dock="Top">
<Button Command="{Binding SendCommand}" ToolTip="This does not really send the email. It just saves the email in the Sent items."><AccessText Text="_Send"/></Button>
<Button Command="{Binding CloseCommand}" ToolTip="Close this window. The email will not be saved."><AccessText Text="_Close"/></Button>
<Button Command="{Binding SendCommand}" ToolTip="This does not really send the email. It just saves the email in the Sent items."
AutomationProperties.AutomationId="SendButton"><AccessText Text="_Send"/></Button>
<Button Command="{Binding CloseCommand}" ToolTip="Close this window. The email will not be saved."
AutomationProperties.AutomationId="CloseButton"><AccessText Text="_Close"/></Button>
</ToolBar>

<Grid Margin="11,11,11,11">
Expand Down Expand Up @@ -51,25 +53,27 @@
</Grid.RowDefinitions>

<Label Grid.Row="0" Grid.Column="0" Content="From: "/>
<ComboBox Grid.Row="0" Grid.Column="2" ItemsSource="{Binding EmailAccounts}" SelectedItem="{Binding SelectedEmailAccount}" DisplayMemberPath="Name"/>
<ComboBox Grid.Row="0" Grid.Column="2" ItemsSource="{Binding EmailAccounts}" SelectedItem="{Binding SelectedEmailAccount}" DisplayMemberPath="Name"
AutomationProperties.AutomationId="EmailAccountsComboBox"/>

<Label Grid.Row="2" Grid.Column="0" Content="To:"/>
<!-- Use Tag with Binding to domain object for validation -->
<TextBox x:Name="toBox" Grid.Row="2" Grid.Column="2" Text="{Binding To}" Tag="{Binding Email.To}"/>
<Button Grid.Row="2" Grid.Column="4" Command="{Binding SelectContactCommand}" CommandParameter="To" Content="..."/>
<TextBox x:Name="toBox" Grid.Row="2" Grid.Column="2" Text="{Binding To}" Tag="{Binding Email.To}" AutomationProperties.AutomationId="ToTextBox"/>
<Button Grid.Row="2" Grid.Column="4" Command="{Binding SelectContactCommand}" CommandParameter="To" Content="..." AutomationProperties.AutomationId="ToSelectContactButton"/>

<Label Grid.Row="0" Grid.Column="8" Content="CC:"/>
<TextBox Grid.Row="0" Grid.Column="10" Text="{Binding CC}" Tag="{Binding Email.CC}"/>
<Button Grid.Row="0" Grid.Column="12" Command="{Binding SelectContactCommand}" CommandParameter="CC" Content="..."/>
<TextBox Grid.Row="0" Grid.Column="10" Text="{Binding CC}" Tag="{Binding Email.CC}" AutomationProperties.AutomationId="CCTextBox"/>
<Button Grid.Row="0" Grid.Column="12" Command="{Binding SelectContactCommand}" CommandParameter="CC" Content="..." AutomationProperties.AutomationId="CCSelectContactButton"/>

<Label Grid.Row="2" Grid.Column="8" Content="BCC:"/>
<TextBox Grid.Row="2" Grid.Column="10" Text="{Binding Bcc}" Tag="{Binding Email.Bcc}"/>
<Button Grid.Row="2" Grid.Column="12" Command="{Binding SelectContactCommand}" CommandParameter="Bcc" Content="..."/>
<TextBox Grid.Row="2" Grid.Column="10" Text="{Binding Bcc}" Tag="{Binding Email.Bcc}" AutomationProperties.AutomationId="BccTextBox"/>
<Button Grid.Row="2" Grid.Column="12" Command="{Binding SelectContactCommand}" CommandParameter="Bcc" Content="..." AutomationProperties.AutomationId="BccSelectContactButton"/>

<Label Grid.Row="4" Grid.Column="0" Content="Title:"/>
<TextBox Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="11" Text="{Binding Email.Title, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="11" Text="{Binding Email.Title, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="TitleTextBox"/>

<TextBox Grid.Row="6" Grid.ColumnSpan="13" Text="{Binding Email.Message}" AcceptsReturn="True" AcceptsTab="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto"/>
<TextBox Grid.Row="6" Grid.ColumnSpan="13" Text="{Binding Email.Message}" AcceptsReturn="True" AcceptsTab="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto"
AutomationProperties.AutomationId="MessageTextBox"/>
</Grid>
</DockPanel>
</Window>

0 comments on commit fd05fcf

Please sign in to comment.