Test Automation‎ > ‎

WPF manipulation




The following samples are [ from / here ] downloadable. 

When operating a WinForm application, of course, a Friendly basic function is also used, 
but if the following libraries are used, it can be operated still more easily. 

This sample is the code which operates the following application using them. 
If the Add button is pushed, a corporate information input screen is modal, it is displayed, and the input data is stored in ListBox of MainForm. 
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Codeer.Friendly;
using Codeer.Friendly.Dynamic;
using Codeer.Friendly.Windows;
using System.Diagnostics;
using WpfTarget;
using System.Windows;
using Codeer.Friendly.Windows.Grasp;
using RM.Friendly.WPFStandardControls;

namespace WpfSample
{
    [
TestClass]
   
public class UnitTest
    {
       
WindowsAppFriend _app;

        [
TestInitialize]
       
public void TestInitialize()
        {
            _app =
new WindowsAppFriend(Process.Start("WpfTarget.exe"));
        }

        [
TestCleanup]
       
public void TestCleanup()
        {
           
Process.GetProcessById(_app.ProcessId).CloseMainWindow();
        }

        [
TestMethod]
       
public void Test()
        {

           
dynamic mainCore = _app.Type<Application>().Current.MainWindow;
           
var main = new WindowControl(mainCore);

           
var _buttonAdd = new WPFButtonBase(mainCore._buttonAdd);
           
var _listBoxData = new WPFListBox(mainCore._listBoxData);

           
var async = new Async();
            _buttonAdd.EmulateClick(async);

           
var input = main.WaitForNextModal();
           
dynamic inputCore = input.Dynamic();

           
var _grid = new WPFDataGrid(inputCore._grid);
           
var _buttonOK = new WPFButtonBase(inputCore._buttonOK);
           
var _dateTimePickerFounding = new WPFDatePicker(inputCore._dateTimePickerFounding);
           
var _textBoxCompanyName = new WPFTextBox(inputCore._textBoxCompanyName);

            _textBoxCompanyName.EmulateChangeText(
"Codeer");
            _dateTimePickerFounding.EmulateChangeDate(
new DateTime(2011, 3, 14));
            _grid.EmulateChangeCellText(0, 0,
"ishikawa");
            _grid.EmulateChangeCellComboSelect(0, 1, 2);
            _grid.EmulateChangeCellText(1, 0,
"yamada");
            _grid.EmulateChangeCellComboSelect(1, 1, 1);
            _buttonOK.EmulateClick();

            async.WaitForCompletion();

           
var data = (Company)_listBoxData.Dynamic().Items[0];

           
Assert.AreEqual("Codeer", data.Name);
           
Assert.AreEqual(new DateTime(2011, 3, 14), data.FoundingDate);
           
Assert.AreEqual(2, data.Members.Count);
           
Assert.AreEqual("ishikawa", data.Members[0].Name);
           
Assert.AreEqual(Language.CS, data.Members[0].Language);
           
Assert.AreEqual("yamada", data.Members[1].Name);
           
Assert.AreEqual(Language.CPP, data.Members[1].Language);
        }
    }
}

As remarks, If you make WinForms app, but a native window may be displayed. 
That is MessageBox, FileDialog, FolderDialog.