Tuesday, 23 April 2013

Simple WPF app automation testing using TestStack.White

few reads before please
http://teststack.github.io/White/
http://blog.benhall.me.uk/2008/02/12/project-white-automated-ui-testing/
http://blogs.msdn.com/b/john_daddamio/archive/2008/04/04/testing-wpf-applications-with-the-white-ui-test-framework.aspx
http://www.codeproject.com/Articles/289028/White-An-UI-Automation-tool-for-windows-applicatio

To get AutomationId's use C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\Inspect.exe

Simple WPF app now

Concept of this application is simple - we will presented with a Login screen, once details are entered we will see a Welcome screen.






Mainwindow.xaml

<Window x:Class="SimpleWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Closing="Window_Closing" Closed="Window_Closed" >
    <Grid>
        <Label Content="User Name" Height="26" HorizontalAlignment="Left" Margin="30,41,0,0" Name="user" VerticalAlignment="Top" Width="88" />
        <Label Content="Password" Height="26" HorizontalAlignment="Left" Margin="30,76,0,0" Name="label1" VerticalAlignment="Top" Width="88" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="137,41,0,0" Name="userValue" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Right" Margin="0,76,246,0" Name="passwordValue" VerticalAlignment="Top" Width="120" />
        <Button Content="Login" Height="23" HorizontalAlignment="Left" Margin="137,121,0,0" Name="login" VerticalAlignment="Top" Width="75" Click="login_Click" />
    </Grid>
</Window>

Mainwindow.xaml.cs




using System;
using System.Windows;
 
namespace SimpleWPF
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 

    public partial class MainWindow : Window
    {
        Welcome wel = new Welcome();
 
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void login_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(userValue.Text))
            {
                MessageBox.Show("Please enter user name");
                return;
            }
 
            if (string.IsNullOrWhiteSpace(passwordValue.Text))
            {
                MessageBox.Show("Please enter password");
                return;
            }
 
            if (userValue.Text.Equals("suresh") && passwordValue.Text.Equals("password"))
            {
                wel.Show();
            }
        }
 
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (wel.Visibility == System.Windows.Visibility.Visible) wel.Close();
        }
 
        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}



Welcome.xaml

<Window x:Class="SimpleWPF.Welcome"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Welcome" Height="300" Width="300">
    <Grid>
        <Label Content="Welcome to hell" Height="28" HorizontalAlignment="Left" Margin="53,119,0,0" Name="label1" VerticalAlignment="Top" Width="164" />
    </Grid>
</Window>

SimpleWPFShould.xaml


using System.Diagnostics;
 
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
using White.Core;
using White.Core.Factory;
using White.Core.UIItems.Finders;
using White.Core.InputDevices;
 
namespace SimpleWPFTest
{
    [TestClass]
    public class SimpleWPFShould
    {
        // source exe file path.
        private const string ExeSourceFile = @"C:\\Users\\sureshM\\Documents\\Visual Studio 2010\\Projects\\MyUIAutomation\\SimpleWPF\\bin\\Debug\\SimpleWPF.exe";
        
        //Global Variable to for Application launch
        private static Application _application = default(Application);
        
        //Global variable to get the Main window of calculator from application.
        private static White.Core.UIItems.WindowItems.Window _mainWindow = null;
 
        [ClassInitialize]
        public static void Initialize(TestContext context)
        {
            //start process for the above exe file location
            var psi = new ProcessStartInfo(ExeSourceFile);
            psi.UseShellExecute = false;
 
            // launch the process through white application
            _application = White.Core.Application.AttachOrLaunch(psi);
 
            //Get the window of calculator from white application 
            _mainWindow = _application.GetWindow(SearchCriteria.ByText("MainWindow"), InitializeOption.NoCache);
        }
 
        [ClassCleanup]
        public static void Cleanup()
        {
            _application.Close();
        }
 
        [TestMethod]
        public void OpenAndCloseSuccessfully()
        {
            Assert.IsNotNull(_application);
        }
 
        [TestMethod]
        public void LoadMainWindowSuccessfully()
        {
            Assert.IsNotNull(_mainWindow);
        }
 
        [TestMethod]
        public void LoginSuccessfully()
        {
            var user = _mainWindow.Get<White.Core.UIItems.TextBox>(SearchCriteria.ByAutomationId("userValue"));
            var password = _mainWindow.Get<White.Core.UIItems.TextBox>(SearchCriteria.ByAutomationId("passwordValue"));
            var login = _mainWindow.Get<White.Core.UIItems.Button>(SearchCriteria.ByAutomationId("login"));
 
            user.Text = "suresh";
            password.Text = "password";
 
            login.Click();
 
            var welcome = _application.GetWindow(SearchCriteria.ByText("Welcome"), InitializeOption.NoCache);
            var welcomeMessage = welcome.Get<White.Core.UIItems.Label>(SearchCriteria.ByAutomationId("label1"));
 
            Assert.IsTrue(welcomeMessage.Text.Equals("Welcome to hell"));
            Assert.IsNotNull(welcome);
 
            welcome.Close();
        }
    }
}

2 comments:

Unknown said...

Hello Suresh,
The Article on Simple WPF app automation testing using TestStack.White is nice give detail information about it.Thanks for Sharing the information about App Automation Testing. mobile application testing

Chad said...

The Article on Simple WPF app automation testing using TestStack. Really good information to show through this blog. I really appreciate you for all the valuable information that you are providing us through your blog.

Penetration testing services
Vulnerability assessment services
Load Testing Services
Mobile app testing services