Skip to content

Instantly share code, notes, and snippets.

@fearofcode
Last active September 19, 2018 07:49
Show Gist options
  • Save fearofcode/9f92378ca433f78d36c134c3cf048834 to your computer and use it in GitHub Desktop.
Save fearofcode/9f92378ca433f78d36c134c3cf048834 to your computer and use it in GitHub Desktop.
Simple window switching example with windows passing data to one another. Also shows a working TestStack.White test.
<Application x:Class="LoginExample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LoginExample"
StartupUri="StartingWindow.xaml">
<Application.Resources></Application.Resources>
</Application>
using System.Windows;
namespace LoginExample
{
public partial class App : Application
{
public int loginCount = 0;
}
}
<Window x:Class="LoginExample.LoggedInWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LoginExample"
mc:Ignorable="d"
Title="LoggedInWindow" Height="450" Width="800">
<StackPanel>
<TextBlock>Username:</TextBlock>
<TextBlock Name="username" />
<TextBlock>Password:</TextBlock>
<TextBlock Name="password" />
<TextBlock>Total logins:</TextBlock>
<TextBlock Name="loginCount" />
<Button Content="Logout"
Name="Logout"
Click="Logout_Click" />
</StackPanel>
</Window>
using System.Windows;
namespace LoginExample
{
public partial class LoggedInWindow : Window
{
public string currentUsername;
public string currentPassword;
public LoggedInWindow()
{
InitializeComponent();
Loaded += LoggedInWindow_Loaded;
}
private void LoggedInWindow_Loaded(object sender, RoutedEventArgs e)
{
if (currentUsername != null && currentUsername.Length > 0)
{
username.Text = currentUsername;
}
if (currentPassword != null && currentPassword.Length > 0)
{
password.Text = currentPassword;
}
loginCount.Text = ((App)Application.Current).loginCount.ToString();
}
private void Logout_Click(object sender, RoutedEventArgs e)
{
var startingWindow = new StartingWindow();
App.Current.MainWindow = startingWindow;
Close();
startingWindow.Show();
}
}
}
<Window x:Class="LoginExample.StartingWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LoginExample"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid Width="200"
Height="50"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="Username" />
<TextBox Grid.Row="0"
Grid.Column="1"
Name="username" />
<TextBlock Grid.Row="1"
Grid.Column="0"
Text="Password" />
<PasswordBox Grid.Row="1"
Grid.Column="1"
Name="password" />
</Grid>
<Button Content="Login"
Click="Button_Click" />
</StackPanel>
</Window>
using System.Windows;
namespace LoginExample
{
public partial class StartingWindow : Window
{
public StartingWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Authenticate())
{
var loggedInWindow = new LoggedInWindow()
{
currentUsername = username.Text,
currentPassword = password.Password
};
App.Current.MainWindow = loggedInWindow;
// global mutable state. you can't stop me from writing, running, and using this code. deal with it.
((App)Application.Current).loginCount++;
Close();
loggedInWindow.Show();
}
else
{
MessageBox.Show("Invalid username/password");
}
}
private bool Authenticate()
{
return username.Text.Equals("warren") && password.Password.Equals("farts");
}
}
}
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestStack.White;
using TestStack.White.Factory;
using TestStack.White.UIItems;
using TestStack.White.UIItems.Finders;
using TestStack.White.UIItems.WindowItems;
namespace LoginExampleUITests
{
[TestClass]
public class UnitTest1
{
Application application;
[TestMethod]
public void TestLogin()
{
application = Application.Launch(@"C:\Users\Warren\source\repos\LoginExample\LoginExample\bin\Debug\LoginExample.exe");
Window window = application.GetWindow("Login", InitializeOption.NoCache);
TextBox textBox = (TextBox)window.Get(SearchCriteria.ByAutomationId("username"));
textBox.Text = "warren";
TextBox textBox2 = (TextBox)window.Get(SearchCriteria.ByAutomationId("password"));
textBox2.Text = "farts";
Button button = window.Get<Button>("LoginButton");
button.Click();
Window window2 = application.GetWindow("Logged In", InitializeOption.NoCache);
Button button2 = window2.Get<Button>("Logout");
button2.Click();
}
[TestCleanup]
public void CloseApplication()
{
if (application != null) application.Kill();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment