Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Press a Button on a Different Process (nunit.exe)
    text
    copied!<p>I have recorded this macro that attaches to a process (nunit.exe) for me. Now I'd like to press a button (named "Run") on the window of the process I am attaching to, but how? I have recorded this macro:</p> <pre class="lang-vb prettyprint-override"><code>Sub DebugNUnit() DTE.ExecuteCommand("Tools.ExternalCommand6") 'need a short delay for nunit process to appear in process list: Threading.Thread.Sleep(200) Try Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default") Dim dbgeng(1) As EnvDTE80.Engine dbgeng(0) = trans.Engines.Item("Managed (v4.0)") Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "myPC").Item("nunit.exe") proc2.Attach2(dbgeng) Catch ex As System.Exception MsgBox(ex.Message) End Try End Sub </code></pre> <p><strong>UPDATE:</strong> I have adjusted the script based on the answer like this</p> <pre class="lang-vb prettyprint-override"><code>Sub DebugNUnit() DTE.ExecuteCommand("Tools.ExternalCommand6") 'need a short delay for nunit process to appear in process list: Threading.Thread.Sleep(200) Try Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default") Dim dbgeng(1) As EnvDTE80.Engine dbgeng(0) = trans.Engines.Item("Managed (v4.0)") Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "myPC").Item("nunit.exe") proc2.Attach2(dbgeng) 'added this because we may need a short delay for nunit gui to load '(would prefer to replace it with a wait on some loaded event or something) Threading.Thread.Sleep(5000) 'use Process.Handle or Process.MainWindowHandle? anyway, both do not seem to work.. are the parameters incorrect? Dim ButtonHandle As IntPtr = FindWindowEx(System.Diagnostics.Process.GetProcessById(proc2.ProcessID).Handle, 0, "Button", "Run") 'Dim ButtonHandle As IntPtr = FindWindowEx(System.Diagnostics.Process.GetProcessById(proc2.ProcessID).MainWindowHandle, 0, "Button", "Run") SendMessage(ButtonHandle, &amp;HF5, 0, 0) Catch ex As System.Exception MsgBox(ex.Message) End Try End Sub </code></pre> <p>but the "Run" button on the nunit user interface that was launched (by Tools.ExternalCommand6) still isn't being pressed. that makes sense because the button handle appears to be always 0.. why? is there anyone out there with a solution?</p> <p><strong>UPDATE:</strong> I have adjusted the script based on the edited answer like this</p> <pre class="lang-vb prettyprint-override"><code>Option Strict Off Option Explicit Off Imports System Imports EnvDTE Imports EnvDTE80 Imports EnvDTE90 Imports EnvDTE90a Imports EnvDTE100 Imports System.Diagnostics Imports System.Runtime.InteropServices Imports System.Windows.Forms Public Module RecordingModule &lt;DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)&gt; _ Private Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean End Function Sub DebugNUnit() DTE.ExecuteCommand("Tools.ExternalCommand6") 'need a short delay for nunit processes to appear in process list: Threading.Thread.Sleep(1000) Try Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default") Dim dbgeng(1) As EnvDTE80.Engine dbgeng(0) = trans.Engines.Item("Managed (v4.0)") Dim proc2nunitagent As EnvDTE80.Process2 = dbg2.GetProcesses(trans, Environment.MachineName).Item("nunit-agent.exe") Dim proc2nunit As EnvDTE80.Process2 = dbg2.GetProcesses(trans, Environment.MachineName).Item("nunit.exe") proc2nunitagent.Attach2(dbgeng) PostMessage(System.Diagnostics.Process.GetProcessById(proc2nunit.ProcessID).MainWindowHandle, &amp;H100, Keys.Alt Or Keys.R, IntPtr.Zero) Catch ex As System.Exception MsgBox(ex.Message + Environment.NewLine + ex.StackTrace) End Try End Sub End Module </code></pre> <p>I am using nunit 2.5.7 and for attaching and debugging to work you need to attach to the nunit agent and press run on the nunit gui, for more recent versions of nunit you can attach to the nunit gui.</p> <p>the above script enables nunit debugging from the Visual Studio IDE with just visual Studio and the nunit binaries. all you need to do is set up nunit as an external tool (with the current project as a parameter), create a custom toolbar, and add a button to that toolbar that uses the above script. this will start nunit, attach to it, start running the tests, and break at your breakpoint.</p> <p>one more thing: in the nunit gui disable "Save Visual state of each Project" in Tools -> Settings -> Tree Display or the nunit gui will remember the last run test, which is a pain if that's not the one you wanted to debug.</p> <p>possible improvements: build before starting nunit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload