gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:09:46 37 4
gpt4 key购买 nike

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

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 上按运行,对于更新版本的 nunit,您可以连接到 nunit gui。

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

还有一件事:在 nunit gui 中,在工具 -> 设置 -> 树显示中禁用“保存每个项目的视觉状态”,否则 nunit gui 会记住上次运行的测试,如果那不是你的测试,那会很痛苦想调试。

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

最佳答案

试试这个:

<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)

我测试了这个,它工作正常。

关于c# - 如何在不同的进程上按下按钮 (nunit.exe),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8853801/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com