Skip to content

Commit

Permalink
UITest/InfoMan: extend ShowContactsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Sep 24, 2024
1 parent 6eb7621 commit af07aa7
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 19 deletions.
11 changes: 11 additions & 0 deletions src/Samples.UITest/InformationManager.Test/Controls/SearchBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FlaUI.Core.AutomationElements;
using FlaUI.Core;

namespace UITest.InformationManager.Controls;

public class SearchBox(FrameworkAutomationElementBase element) : AutomationElement(element)
{
public Label SearchHintLabel => this.Find("SearchHintLabel").AsLabel();

public TextBox SearchTextBox => this.Find("SearchTextBox").AsTextBox();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Xunit.Abstractions;
using FlaUI.Core.AutomationElements;
using Xunit.Abstractions;
using Xunit;
using UITest.InformationManager.Views;

namespace UITest.InformationManager.Tests;

Expand All @@ -13,8 +15,38 @@ public void ShowContactsTest() => Run(() =>
window.RootTreeItem.ContactsNode.Click();

var contactListView = window.ContactLayoutView.ContactListView;
Assert.Equal(5, contactListView.ContactList.Items.Length);
var contactView = window.ContactLayoutView.ContactView;
Assert.Equal(5, contactListView.ContactItems.Count);
Assert.Equal(contactListView.ContactList.SelectedItem.As<ContactListItem>().FirstnameLabel.Text, contactListView.ContactItems[0].FirstnameLabel.Text);
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("Search", contactListView.SearchBox.SearchHintLabel.Text);
contactListView.SearchBox.SearchTextBox.Text = "!";
Assert.Empty(contactListView.ContactItems);

contactListView.SearchBox.SearchTextBox.Text = "Mi";
Assert.Equal(2, contactListView.ContactItems.Count);
Assert.Null(contactListView.ContactList.SelectedItem);
contactListView.ContactList.Select(0);
AssertContactItem(contactListView.ContactList.SelectedItem.As<ContactListItem>(), contactView, "Michael", "Pfeiffer", "[email protected]", "(222) 555-0105");

window.ExitCommand.Click();
});

private static void AssertContactItem(ContactListItem contactItem, ContactView? contactView, string firstName, string lastName, string email, string phone)
{
Assert.Equal(firstName, contactItem.FirstnameLabel.Text);
Assert.Equal(lastName, contactItem.LastnameLabel.Text);
Assert.Equal(email, contactItem.EmailLabel.Text);
Assert.Equal(phone, contactItem.PhoneLabel.Text);

if (contactView is not null)
{
Assert.Equal(firstName, contactView.FirstnameBox.Text);
Assert.Equal(lastName, contactView.LastnameBox.Text);
Assert.Equal(email, contactView.EmailBox.Text);
Assert.Equal(phone, contactView.PhoneBox.Text);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
using FlaUI.Core.AutomationElements;
using FlaUI.Core;
using UITest.InformationManager.Controls;

namespace UITest.InformationManager.Views;

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

public ListBox ContactList => this.Find("ContactList").AsListBox();

public IReadOnlyList<ContactListItem> ContactItems => ContactList.Items.Select(x => x.As<ContactListItem>()).ToArray();
}

public class ContactListItem(FrameworkAutomationElementBase element) : ListBoxItem(element)
{
public Label FirstnameLabel => this.Find("FirstnameLabel").AsLabel();

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

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

public Label PhoneLabel => this.Find("PhoneLabel").AsLabel();
}
19 changes: 19 additions & 0 deletions src/Samples.UITest/InformationManager.Test/Views/ContactView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,23 @@ namespace UITest.InformationManager.Views;

public class ContactView(FrameworkAutomationElementBase element) : AutomationElement(element)
{
public TextBox FirstnameBox => this.Find("FirstnameBox").AsTextBox();

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

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

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

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

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

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

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

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

public TextBox CountryBox => this.Find("CountryBox").AsTextBox();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<RowDefinition/>
</Grid.RowDefinitions>

<ctrl:SearchBox SearchText="{Binding FilterText}"/>
<ctrl:SearchBox SearchText="{Binding FilterText}" AutomationProperties.AutomationId="SearchBox"/>

<ListBox x:Name="contactsBox" ItemsSource="{Binding Contacts}" SelectedItem="{Binding SelectedContact}" Grid.Row="1" HorizontalContentAlignment="Stretch"
BorderThickness="0,1,0,0" BorderBrush="{x:Static SystemColors.ActiveBorderBrush}"
Expand All @@ -35,13 +35,17 @@
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<TextBlock Text="{Binding Firstname, ValidatesOnNotifyDataErrors=False, Converter={x:Static cc:TargetStringEmptyValueConverter.Default}}" Margin="7"/>
<TextBlock Text="{Binding Firstname, ValidatesOnNotifyDataErrors=False, Converter={x:Static cc:TargetStringEmptyValueConverter.Default}}"
Margin="7" AutomationProperties.AutomationId="FirstnameLabel"/>

<TextBlock Text="{Binding Email, ValidatesOnNotifyDataErrors=False, Converter={x:Static cc:TargetStringEmptyValueConverter.Default}}" Grid.Column="1" Margin="7"/>
<TextBlock Grid.Column="1" Text="{Binding Email, ValidatesOnNotifyDataErrors=False, Converter={x:Static cc:TargetStringEmptyValueConverter.Default}}"
Margin="7" AutomationProperties.AutomationId="EmailLabel"/>

<TextBlock Text="{Binding Lastname, ValidatesOnNotifyDataErrors=False, Converter={x:Static cc:TargetStringEmptyValueConverter.Default}}" Grid.Row="1" Margin="7,0,7,7"/>
<TextBlock Grid.Row="1" Text="{Binding Lastname, ValidatesOnNotifyDataErrors=False, Converter={x:Static cc:TargetStringEmptyValueConverter.Default}}"
Margin="7,0,7,7" AutomationProperties.AutomationId="LastnameLabel"/>

<TextBlock Text="{Binding Phone, ValidatesOnNotifyDataErrors=False, Converter={x:Static cc:TargetStringEmptyValueConverter.Default}}" Grid.Column="1" Grid.Row="1" Margin="7,0,7,7"/>
<TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Phone, ValidatesOnNotifyDataErrors=False, Converter={x:Static cc:TargetStringEmptyValueConverter.Default}}"
Margin="7,0,7,7" AutomationProperties.AutomationId="PhoneLabel"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,42 @@

<Label Grid.Column="0" Grid.Row="0" Content="_Firstname" Target="{Binding ElementName=firstnameBox}"/>
<TextBox x:Name="firstnameBox" Grid.Column="2" Grid.Row="0" MaxLength="100" HorizontalAlignment="Stretch"
Text="{Binding Contact.Firstname, UpdateSourceTrigger=PropertyChanged}" />
Text="{Binding Contact.Firstname, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="FirstnameBox"/>

<Label Grid.Column="0" Grid.Row="2" Content="Lastname"/>
<TextBox Grid.Column="2" Grid.Row="2" MaxLength="100" HorizontalAlignment="Stretch"
Text="{Binding Contact.Lastname, UpdateSourceTrigger=PropertyChanged}"/>
Text="{Binding Contact.Lastname, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="LastnameBox"/>

<Label Grid.Column="0" Grid.Row="4" Content="Company"/>
<TextBox Grid.Column="2" Grid.Row="4" MaxLength="100" HorizontalAlignment="Stretch"
Text="{Binding Contact.Company, UpdateSourceTrigger=PropertyChanged}"/>
Text="{Binding Contact.Company, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="CompanyBox"/>

<Label Grid.Column="0" Grid.Row="6" Content="Email"/>
<TextBox Grid.Column="2" Grid.Row="6" MaxLength="100" HorizontalAlignment="Stretch"
Text="{Binding Contact.Email, UpdateSourceTrigger=PropertyChanged}"/>
Text="{Binding Contact.Email, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="EmailBox"/>

<Label Grid.Column="0" Grid.Row="8" Content="Phone"/>
<TextBox Grid.Column="2" Grid.Row="8" MaxLength="100" HorizontalAlignment="Stretch"
Text="{Binding Contact.Phone, UpdateSourceTrigger=PropertyChanged}"/>
Text="{Binding Contact.Phone, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="PhoneBox"/>

<Label Grid.Column="0" Grid.Row="10" Content="Street"/>
<TextBox Grid.Column="2" Grid.Row="10" MaxLength="100" HorizontalAlignment="Stretch"
Text="{Binding Contact.Address.Street, UpdateSourceTrigger=PropertyChanged}"/>
Text="{Binding Contact.Address.Street, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="StreetBox"/>

<Label Grid.Column="0" Grid.Row="12" Content="City"/>
<TextBox Grid.Column="2" Grid.Row="12" MaxLength="100" HorizontalAlignment="Stretch"
Text="{Binding Contact.Address.City, UpdateSourceTrigger=PropertyChanged}"/>
Text="{Binding Contact.Address.City, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="CityBox"/>

<Label Grid.Column="0" Grid.Row="14" Content="State"/>
<TextBox Grid.Column="2" Grid.Row="14" MaxLength="100" HorizontalAlignment="Stretch"
Text="{Binding Contact.Address.State, UpdateSourceTrigger=PropertyChanged}"/>
Text="{Binding Contact.Address.State, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="StateBox"/>

<Label Grid.Column="0" Grid.Row="16" Content="Postal Code"/>
<TextBox Grid.Column="2" Grid.Row="16" MaxLength="100" HorizontalAlignment="Stretch"
Text="{Binding Contact.Address.PostalCode, UpdateSourceTrigger=PropertyChanged}"/>
Text="{Binding Contact.Address.PostalCode, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="PostalCodeBox"/>

<Label Grid.Column="0" Grid.Row="18" Content="Country"/>
<TextBox Grid.Column="2" Grid.Row="18" MaxLength="100" HorizontalAlignment="Stretch"
Text="{Binding Contact.Address.Country, UpdateSourceTrigger=PropertyChanged}"/>
Text="{Binding Contact.Address.Country, UpdateSourceTrigger=PropertyChanged}" AutomationProperties.AutomationId="CountryBox"/>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;

namespace Waf.InformationManager.Common.Presentation.Controls;
Expand Down Expand Up @@ -30,4 +31,13 @@ public string HintText
get => (string)GetValue(HintTextProperty);
set => SetValue(HintTextProperty, value);
}

/// <inheritdoc />
protected override AutomationPeer OnCreateAutomationPeer() => new ControlAutomationPeer(this);


private sealed class ControlAutomationPeer(FrameworkElement owner) : FrameworkElementAutomationPeer(owner)
{
protected override bool IsControlElementCore() => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ControlTemplate TargetType="{x:Type ctrl:SearchBox}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Label Content="{TemplateBinding HintText}" Target="{Binding ElementName=searchBox}" Padding="0" Margin="5,3">
<Label Content="{TemplateBinding HintText}" Target="{Binding ElementName=searchBox}" Padding="0" Margin="5,3" AutomationProperties.AutomationId="SearchHintLabel">
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
Expand All @@ -23,7 +23,7 @@
</Label.Style>
</Label>
<TextBox x:Name="searchBox" Text="{Binding SearchText, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"
Background="Transparent" HorizontalAlignment="Stretch" Margin="0"/>
Background="Transparent" HorizontalAlignment="Stretch" Margin="0" AutomationProperties.AutomationId="SearchTextBox"/>
</Grid>
</Border>
</ControlTemplate>
Expand Down

0 comments on commit af07aa7

Please sign in to comment.