如何在不同进程上按下按钮(nunit.exe)

时间:2012-01-13 16:26:45

标签: c# .net vb.net nunit

我已经录制了这个附加到进程(nunit.exe)的宏。现在我想在我附加的过程的窗口上按下一个按钮(名为“Run”),但是怎么样?我录制了这个宏:

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

更新:我根据这样的答案调整了脚本

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, &HF5, 0, 0)

    Catch ex As System.Exception

        MsgBox(ex.Message)

    End Try

End Sub

但是仍然没有按下由(由Tools.ExternalCommand6)启动的nunit用户界面上的“运行”按钮。这是有道理的,因为按钮手柄似乎总是0 ..为什么?那里有人有解决方案吗?

更新:我根据编辑后的答案调整了脚本

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

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    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, &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

我正在使用nunit 2.5.7并附加和调试你需要附加到nunit代理的工作并按下nunit gui上的run,对于nunit的更新版本你可以附加到nunit gui。

上面的脚本只使用visual Studio和nunit二进制文件从Visual Studio IDE启用nunit调试。您需要做的就是将nunit设置为外部工具(将当前项目作为参数),创建自定义工具栏,并向使用上述脚本的工具栏添加按钮。这将启动nunit,附加到它,开始运行测试,并在断点处中断。

还有一件事:在nunit gui中禁用“工具中保存每个项目的可视状态” - &gt;设置 - &gt; Tree Display或nunit gui会记住最后一次运行测试,如果那不是你想要调试的那个,那就太痛苦了。

可能的改进:在启动nunit之前构建。

1 个答案:

答案 0 :(得分:4)

试试这个:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
End Function

Dim ButtonHandle As IntPtr = FindWindowEx(YOURPROCESS.HANDLE, 0, "Button", "&Run")
SendMessage(ButtonHandle, &HF5, 0, 0)

编辑:似乎NUint是一个.NET应用程序,这意味着您无法轻松获得按钮句柄。你可以在Spy ++的帮助下为自己看到这一点。

我注意到按钮Run的键盘快捷键为ALT + R,因此您可以使用此代码:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
End Function
PostMessage(MAIN_WINDOW_HANDLE, &H100, Keys.Alt Or Keys.R, IntPtr.Zero)

我对此进行了测试,效果很好。