Test Automation‎ > ‎

Win32 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. 

Here, correspondence of the message box for which .Net application is also used, a file dialog, and a folder dialog is introduced. 
The example of menu manipulation has also come out. 
If menu manipulation is native, most can respond by transmitting WM_COMMAND. 

However, we recommend you to avoid, since difference appears in an action with PC about a folder dialog and a file dialog. 
Please refer to TestSkipFolderDialog. 
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Diagnostics;
using Codeer.Friendly;
using Codeer.Friendly.Dynamic;
using Codeer.Friendly.Windows;
using Codeer.Friendly.Windows.Grasp;
using Codeer.Friendly.Windows.NativeStandardControls;
using System.Runtime.InteropServices;
using System.Text;

namespace Win32Sample
{
    [
TestClass]
   
public class UnitTest
    {
       
const int WM_COMMAND = 0x0111;
       
const int ID_DIALOGS_MESSAGEBOX = 32771;
       
const int ID_DIALOGS_FILEDIALOG = 32772;
       
const int ID_DIALOGS_FOLDERDIALOG = 32773;

       
WindowsAppFriend _app;
       
        [
TestInitialize]
       
public void TestInitialize()
        {
#if DEBUG
            var dir = "Debug";
#else
           
var dir = "Release";
#endif
           
var pathExe = Path.GetFullPath("../../../Win32Target/" + dir + "/Win32Target.exe");
            _app =
new WindowsAppFriend(Process.Start(pathExe));
        }

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

        [
TestMethod]
       
public void TestMessageBox()
        {

           
var main = WindowControl.FromZTop(_app);

           
var async = new Async();
            main.SendMessage(WM_COMMAND,
new IntPtr(ID_DIALOGS_MESSAGEBOX), IntPtr.Zero, async);

           
var next = main.WaitForNextModal();

           
var msg = new NativeMessageBox(next);
           
Assert.AreEqual(msg.Message, "Message");
            msg.EmulateButtonClick(
"OK");

            async.WaitForCompletion();
        }

        [
TestMethod]
       
public void TestFileDialog()
        {
           
var main = WindowControl.FromZTop(_app);

            
var async = new Async();
            main.SendMessage(WM_COMMAND,
new IntPtr(ID_DIALOGS_FILEDIALOG), IntPtr.Zero, async);

           
var fileDialog = main.WaitForNextModal();
           
var buttonOpen = new NativeButton(fileDialog.IdentifyFromWindowText("開く(&O)"));

           
WindowControl comboBoxPathSrc = null;
           
int top = 0;
           
foreach(var c in fileDialog.GetFromWindowClass("ComboBoxEx32"))
            {
               
RECT rc;
                GetWindowRect(c.Handle,
out rc);
               
if (top < rc.Top)
                {
                    comboBoxPathSrc = c;
                    top = rc.Top;
                }
            }
           
var comboBoxPath = new NativeComboBox(comboBoxPathSrc);

           
string myPath = GetType().Assembly.Location;
            comboBoxPath.EmulateChangeEditText(myPath);

            buttonOpen.EmulateClick();

            async.WaitForCompletion();

           
WindowsAppExpander.LoadAssembly(_app, GetType().Assembly);
           
string path = _app.Type(GetType()).GetCurrentPathEx();
           
Assert.AreEqual(myPath, path);
        }

        [
TestMethod]
       
public void TestFolderDialog()
        {
           
var main = WindowControl.FromZTop(_app);

           
var async = new Async();
            main.SendMessage(WM_COMMAND,
new IntPtr(ID_DIALOGS_FOLDERDIALOG), IntPtr.Zero, async);

           
var folderDialog = main.WaitForNextModal();
           
NativeTree tree = new NativeTree(folderDialog.IdentifyFromWindowClass("SysTreeView32"));
           
NativeButton buttonOK = new NativeButton(folderDialog.IdentifyFromWindowText("OK"));

            tree.EmulateExpand(tree.FindNode(
@"デスクトップ", @"PC"), true);
            tree.EmulateExpand(tree.FindNode(
@"デスクトップ", @"PC", @"Windows (C:)"), true);
            tree.EmulateSelectItem(tree.FindNode(
@"デスクトップ", @"PC", @"Windows (C:)", @"Program Files"));

            buttonOK.EmulateClick();

            async.WaitForCompletion();


           
WindowsAppExpander.LoadAssembly(_app, GetType().Assembly);
           
string path = _app.Type(GetType()).GetCurrentPathEx();
           
Assert.AreEqual(@"C:\Program Files".ToLower(), path.ToLower());
        }

        [
TestMethod]
       
public void TestSkipFolderDialog()
        {
           
WindowsAppExpander.LoadAssembly(_app, GetType().Assembly);

           
string myPath = GetType().Assembly.Location;
            _app.Type(GetType()).ShowFolderDialogCoreLogic(myPath);

           
string path = _app.Type(GetType()).GetCurrentPathEx();
           
Assert.AreEqual(myPath, path);
        }

        [
DllImport("user32.dll")]
        [
return: MarshalAs(UnmanagedType.Bool)]
       
public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);

        [
DllImport("Win32Target.exe", CallingConvention = CallingConvention.Cdecl)]
       
static extern void ShowFolderDialogCoreLogic(string path);

        [
DllImport("Win32Target.exe", CallingConvention = CallingConvention.Cdecl)]
        
static extern void GetCurrentPath(StringBuilder buf, int size);

       
static string GetCurrentPathEx()
        {
           
StringBuilder buf = new StringBuilder(1024);
            GetCurrentPath(buf, 1024);
           
return buf.ToString();
        }
    }
}