Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI test book library #42

Merged
merged 13 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/System.Waf.CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
src/System.Waf/System.Waf/**/*.snupkg

- name: UI Test
run: dotnet test ./src/Samples.UITest/Samples.UITest.sln --logger "console;verbosity=detailed"
run: dotnet test ./src/Samples.UITest/Samples.UITest.sln --logger "console;verbosity=detailed" -maxCpuCount:1
- name: Upload UI Test results
uses: actions/upload-artifact@v4
if: always()
Expand Down
19 changes: 19 additions & 0 deletions src/Samples.UITest/BookLibrary.Test/BookLibrary.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>UITest.BookLibrary</RootNamespace>

<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0" />
<PackageReference Include="xunit" Version="2.8.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../UITest.Core/UITest.Core.csproj" />
</ItemGroup>
</Project>
93 changes: 93 additions & 0 deletions src/Samples.UITest/BookLibrary.Test/Tests/BookLibraryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Capturing;
using UITest.BookLibrary.Views;
using UITest.SystemViews;
using Xunit;
using Xunit.Abstractions;

namespace UITest.BookLibrary.Tests;

public class BookLibraryTest(ITestOutputHelper log) : UITest(log)
{
[Fact]
public void AboutTest() => Run(() =>
{
Launch();
var window = GetShellWindow();

var helpMenu = window.HelpMenu;
helpMenu.Click();
helpMenu.AboutMenuItem.Click();

var messageBox = window.FirstModalWindow().As<MessageBox>();
Assert.Equal("Waf Book Library", messageBox.Title);
Log.WriteLine(messageBox.Message);
Assert.StartsWith("Waf Book Library ", messageBox.Message);
Capture.Screen().ToFile(GetScreenshotFile("About.png"));
messageBox.Buttons[0].Click();

var dataMenu = window.DataMenu;
dataMenu.Click();
dataMenu.ExitMenuItem.Click();
});

[Fact]
public void SearchBookListAndChangeEntriesTest() => Run(() =>
{
Launch();
var window = GetShellWindow();
var bookListView = window.TabControl.BookLibraryTabItem.BookListView;
var bookView = window.TabControl.BookLibraryTabItem.BookView;

Assert.Equal(41, bookListView.BookDataGrid.RowCount);
bookListView.SearchBox.Text = "Ha";
Assert.Equal(13, bookListView.BookDataGrid.RowCount);
bookListView.SearchBox.Text = "Harr";
Assert.Equal(7, bookListView.BookDataGrid.RowCount);
var bookRow2 = bookListView.BookDataGrid.GetRowByIndex(1).As<BookGridRow>();
bookRow2.Select();

AssertEqual("Harry Potter and the Deathly Hallows", bookRow2.TitleCell.Name, bookView.TitleTextBox.Text);
AssertEqual("J.K. Rowling", bookRow2.AuthorCell.Name, bookView.AuthorTextBox.Text);
Assert.Equal("Bloomsbury", bookView.PublisherTextBox.Text);
Assert.Equal("1/1/2007", bookRow2.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("607", bookView.PagesTextBox.Text);
AssertEqual("Ginny Weasley", bookRow2.LendToCell.LendToLabel.Name, bookView.LendToTextBox.Text);

bookView.TitleTextBox.Text = "Test Title";
Assert.Equal("Test Title", bookRow2.TitleCell.Name);
bookView.AuthorTextBox.Text = "TAuthor";
Assert.Equal("TAuthor", bookRow2.AuthorCell.Name);
bookView.PublishDatePicker.SelectedDate = new DateTime(2024, 3, 2);
Assert.Equal("3/2/2024", bookRow2.PublishDateCell.Name);
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);

bookView.LendToButton.Click();
var lendToWindow = window.FirstModalWindow().As<LendToWindow>();
Assert.True(lendToWindow.WasReturnedRadioButton.IsChecked);
Assert.False(lendToWindow.LendToRadioButton.IsChecked);
Assert.False(lendToWindow.PersonListBox.IsEnabled);
lendToWindow.LendToRadioButton.Click();
Assert.True(lendToWindow.PersonListBox.IsEnabled);
Assert.Equal(["Ginny", "Hermione", "Harry", "Ron"], lendToWindow.PersonListBox.Items.Select(x => x.Text));
lendToWindow.PersonListBox.Items[2].Select();
lendToWindow.OkButton.Click();
AssertEqual("Harry Potter", bookRow2.LendToCell.LendToLabel.Name, bookView.LendToTextBox.Text);

window.Close();
var messageBox = window.FirstModalWindow().As<MessageBox>(); // MessageBox that asks user to save the changes
messageBox.Buttons[1].Click(); // No button

void AssertEqual(string expected, string actual1, string actual2)
{
Assert.Equal(expected, actual1);
Assert.Equal(expected, actual2);
}
});
}
41 changes: 41 additions & 0 deletions src/Samples.UITest/BookLibrary.Test/UITest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using System.Diagnostics;
using UITest.BookLibrary.Views;
using Xunit;
using Xunit.Abstractions;

[assembly: CollectionBehavior(DisableTestParallelization = true)]

namespace UITest.BookLibrary;

public abstract class UITest(ITestOutputHelper log) : UITestBase(log, "BookLibrary.exe",
Environment.GetEnvironmentVariable("UITestExePath") ?? "out/BookLibrary/Release/net8.0-windows/",
Environment.GetEnvironmentVariable("UITestOutputPath") ?? "out/Samples.UITest/BookLibrary/")
{
public Application Launch(LaunchArguments? arguments = null, bool resetSettings = true)
{
Log.WriteLine("");
if (resetSettings)
{
var productName = FileVersionInfo.GetVersionInfo(Executable).ProductName ?? throw new InvalidOperationException("Could not read the ProductName from the exe.");
var settingsFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), productName, "Settings", "Settings.xml");
if (File.Exists(settingsFile)) File.Delete(settingsFile);
Log.WriteLine($"Delete settings: {settingsFile}");
}
var args = (arguments ?? new LaunchArguments()).ToArguments();
Log.WriteLine($"Launch: {args}");
return App = Application.Launch(Executable, args);
}

public ShellWindow GetShellWindow() => App!.GetMainWindow(Automation).As<ShellWindow>();
}

public record LaunchArguments(string? UICulture = "en-US", string? Culture = "en-US", string? AdditionalArguments = null) : LaunchArgumentsBase
{
public override string ToArguments()
{
string?[] args = [CreateArg(UICulture), CreateArg(Culture), AdditionalArguments];
return string.Join(" ", args.Where(x => !string.IsNullOrEmpty(x)));
}
}
29 changes: 29 additions & 0 deletions src/Samples.UITest/BookLibrary.Test/Views/BookListView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FlaUI.Core;
using FlaUI.Core.AutomationElements;

namespace UITest.BookLibrary.Views;

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

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

public class BookGridRow(FrameworkAutomationElementBase element) : GridRow(element)
{
public GridCell TitleCell => Cells[0];

public GridCell AuthorCell => Cells[1];

public GridCell PublishDateCell => Cells[2];

public LendToGridCell LendToCell => Cells[3].As<LendToGridCell>();
}

public class LendToGridCell(FrameworkAutomationElementBase element) : GridCell(element)
{
public Label LendToLabel => this.Find("LendToLabel").AsLabel();

public Button LendToButton => this.Find("LendToButton").AsButton();
}
25 changes: 25 additions & 0 deletions src/Samples.UITest/BookLibrary.Test/Views/BookView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using FlaUI.Core.AutomationElements;
using FlaUI.Core;

namespace UITest.BookLibrary.Views;

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

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

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

public DateTimePicker PublishDatePicker => this.Find("PublishDatePicker").AsDateTimePicker();

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

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

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

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

public Button LendToButton => this.Find("LendToButton").AsButton();
}
17 changes: 17 additions & 0 deletions src/Samples.UITest/BookLibrary.Test/Views/LendToWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using FlaUI.Core;
using FlaUI.Core.AutomationElements;

namespace UITest.BookLibrary.Views;

public class LendToWindow(FrameworkAutomationElementBase element) : Window(element)
{
public RadioButton WasReturnedRadioButton => this.Find("WasReturnedRadioButton").AsRadioButton();

public RadioButton LendToRadioButton => this.Find("LendToRadioButton").AsRadioButton();

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

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

public Button CancelButton => this.Find("CancelButton").AsButton();
}
41 changes: 41 additions & 0 deletions src/Samples.UITest/BookLibrary.Test/Views/ShellWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using FlaUI.Core;
using FlaUI.Core.AutomationElements;

namespace UITest.BookLibrary.Views;

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

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

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

public class DataMenu(FrameworkAutomationElementBase element) : Menu(element)
{
public MenuItem SaveMenuItem => this.Find("SaveMenuItem").AsMenuItem();

public MenuItem ExitMenuItem => this.Find("ExitMenuItem").AsMenuItem();
}

public class HelpMenu(FrameworkAutomationElementBase element) : Menu(element)
{
public MenuItem AboutMenuItem => this.Find("AboutMenuItem").AsMenuItem();
}

public class TabControl(FrameworkAutomationElementBase element) : Tab(element)
{
public BookLibraryTabItem BookLibraryTabItem => this.Find("BookLibraryTabItem").As<BookLibraryTabItem>();

public TabItem AddressBookTabItem => this.Find("AddressBookTabItem").AsTabItem();

public TabItem ReportingTabItem => this.Find("ReportingTabItem").AsTabItem();
}

public class BookLibraryTabItem(FrameworkAutomationElementBase element) : TabItem(element)
{
public BookListView BookListView => this.Find("BookListView").As<BookListView>();

public BookView BookView => this.Find("BookView").As<BookView>();
}
14 changes: 13 additions & 1 deletion src/Samples.UITest/Samples.UITest.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34714.143
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Writer.Test", "Writer.Test\Writer.Test.csproj", "{8FBAB64A-51F5-40F8-8B56-4AEE7BE49838}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Writer.Test", "Writer.Test\Writer.Test.csproj", "{8FBAB64A-51F5-40F8-8B56-4AEE7BE49838}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BookLibrary.Test", "BookLibrary.Test\BookLibrary.Test.csproj", "{A239DB72-F7D9-4DE9-83F9-E0D852FA5F8F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UITest.Core", "UITest.Core\UITest.Core.csproj", "{4B2174D9-B723-42AA-90DA-2F556409F19A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +19,14 @@ Global
{8FBAB64A-51F5-40F8-8B56-4AEE7BE49838}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FBAB64A-51F5-40F8-8B56-4AEE7BE49838}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FBAB64A-51F5-40F8-8B56-4AEE7BE49838}.Release|Any CPU.Build.0 = Release|Any CPU
{A239DB72-F7D9-4DE9-83F9-E0D852FA5F8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A239DB72-F7D9-4DE9-83F9-E0D852FA5F8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A239DB72-F7D9-4DE9-83F9-E0D852FA5F8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A239DB72-F7D9-4DE9-83F9-E0D852FA5F8F}.Release|Any CPU.Build.0 = Release|Any CPU
{4B2174D9-B723-42AA-90DA-2F556409F19A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B2174D9-B723-42AA-90DA-2F556409F19A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B2174D9-B723-42AA-90DA-2F556409F19A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B2174D9-B723-42AA-90DA-2F556409F19A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 10 additions & 0 deletions src/Samples.UITest/UITest.Core/LaunchArgumentsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Runtime.CompilerServices;

namespace UITest;

public abstract record LaunchArgumentsBase
{
protected string? CreateArg(object? value, [CallerArgumentExpression(nameof(value))] string propertyName = null!) => value is null ? null : $"--{propertyName}=\"{value}\"";

public abstract string ToArguments();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Definitions;

namespace UITest.Writer.Views;
namespace UITest.SystemViews;

public class MessageBox(FrameworkAutomationElementBase element) : AutomationElement(element)
{
public string Title => Name;

public string Message => this.Find(x => x.ByControlType(ControlType.Text)).Name;

public Button OkButton => this.Find(x => x.ByControlType(ControlType.Button)).AsButton();
public Button[] Buttons => this.FindAll(x => x.ByControlType(ControlType.Button)).Select(x => x.AsButton()).ToArray();
}
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.Writer.Views;
namespace UITest.SystemViews;

public class SaveFileDialog(FrameworkAutomationElementBase element) : Window(element)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FlaUI.Core.Tools;
using System.Runtime.CompilerServices;

namespace UITest.Writer;
namespace UITest;

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

Expand Down
14 changes: 14 additions & 0 deletions src/Samples.UITest/UITest.Core/UITest.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>UITest</RootNamespace>

<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FlaUI.UIA3" Version="4.0.0" />
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
</ItemGroup>
</Project>
Loading