.net 是否可以使用;“编码用户界面”;在内存中启动的窗体上,而不是在编译的可执行文件上?

.net 是否可以使用;“编码用户界面”;在内存中启动的窗体上,而不是在编译的可执行文件上?,.net,winforms,coded-ui-tests,.net,Winforms,Coded Ui Tests,我试图针对加载在内存中的表单(在另一个线程上)*而不是编译的可执行文件运行编码的UI框架,它似乎对某些控件正常工作,但对其他控件无效。具体来说,当试图定位作为实际控件(按钮、标签等)直接父对象的(不可见)窗口时,会引发“UITestControlNotFoundException” 一个显示问题的示例: Imports System.Windows.Forms Imports Microsoft.VisualStudio.TestTools.UITesting <CodedUITest(

我试图针对加载在内存中的表单(在另一个线程上)*而不是编译的可执行文件运行编码的UI框架,它似乎对某些控件正常工作,但对其他控件无效。具体来说,当试图定位作为实际控件(按钮、标签等)直接父对象的(不可见)窗口时,会引发“UITestControlNotFoundException”

一个显示问题的示例:

Imports System.Windows.Forms
Imports Microsoft.VisualStudio.TestTools.UITesting

<CodedUITest()>
Public Class CodedUITest2

    <TestMethod()>
    Public Sub CodedUITestMethod1()
        Playback.PlaybackSettings.SearchTimeout = 500
        Playback.PlaybackSettings.MaximumRetryCount = 1

        Dim Thread As New Threading.Thread _
            (Sub()
                 Dim Form = New Form()
                 Form.Text = "Foo"
                 Form.Controls.Add(New Button)
                 Form.Controls(0).Name = "Bar"
                 Form.Visible = True
                 Application.Run(Form)
             End Sub)
        Thread.SetApartmentState(Threading.ApartmentState.STA)
        Thread.Start()

        'Find UI test control
        Dim UIWindow = New WinControls.WinWindow
        UIWindow.SearchProperties.Add(UITestControl.PropertyNames.Name, "Foo")
        UIWindow.WaitForControlExist(5000) '<--This works

        Dim UIButton As New WinControls.WinButton(UIWindow)
        UIButton.Find() '<--This works

        Dim UIButtonWindow As New WinControls.WinWindow(UIWindow)
        UIButtonWindow.SearchProperties.Add(WinControls.WinControl.PropertyNames.ControlName, "Bar")
        UIButtonWindow.Find() '<--This does NOT work
    End Sub
End Class
导入System.Windows.Forms
导入Microsoft.VisualStudio.TestTools.UITesting
公共类代码duitest2
公共子代码duitestmethod1()
Playback.PlaybackSettings.SearchTimeout=500
Playback.PlaybackSettings.MaximumRetryCount=1
将螺纹调暗为新螺纹。螺纹_
(次)
Dim Form=新表单()
Form.Text=“Foo”
Form.Controls.Add(新建按钮)
Form.Controls(0).Name=“Bar”
Form.Visible=True
Application.Run(表单)
末端接头)
SetApartmentState(Threading.ApartmentState.STA)
Thread.Start()
'查找UI测试控件
Dim UIWindow=新WinControl.WinWindow
UIWindow.SearchProperties.Add(UITestControl.PropertyNames.Name,“Foo”)

UIWindow.WaitForControlExist(5000)”如果您想通过其
文本找到类似表单的按钮,则需要设置按钮的文本:
form.Controls(0).Text=“Bar”
。然后使用以下搜索表达式:
UIButtonWindow.SearchProperties.Add(WinControls.WinWindow.PropertyNames.Name,“Bar”)
谢谢,但这没有帮助。正如我在问题中提到的:该场景在针对已编译的exe运行时有效,但在运行时调用表单时无效。没问题,这是一个答案,任何人都知道如何使其工作。我试过这个,它对可执行文件和您的案例都有效。但是它没有描述为什么你的代码不能工作。我不熟悉编码的UI,但是显示的测试代码有两个明显的线程问题(对于有WF经验的人来说)。首先,表单创建(从
Dim form
开始)应该发生在UI线程上,这样块应该在
线程
过程中移动(在
form.Visible=True
行之前)。第二,UI线程应该通过在
thread.Start()之前添加
thread.SetApartmentState(ApartmentState.STA)
使之成为STA。我已经用您的建议更新了示例,但结果是相同的。如果您想通过
文本找到类似表单的按钮,请,您需要设置按钮的文本:
Form.Controls(0)。text=“Bar”
。然后使用以下搜索表达式:
UIButtonWindow.SearchProperties.Add(WinControls.WinWindow.PropertyNames.Name,“Bar”)
谢谢,但这没有帮助。正如我在问题中提到的:该场景在针对已编译的exe运行时有效,但在运行时调用表单时无效。没问题,这是一个答案,任何人都知道如何使其工作。我试过这个,它对可执行文件和您的案例都有效。但是它没有描述为什么你的代码不能工作。我不熟悉编码的UI,但是显示的测试代码有两个明显的线程问题(对于有WF经验的人来说)。首先,表单创建(从
Dim form
开始)应该发生在UI线程上,这样块应该在
线程
过程中移动(在
form.Visible=True
行之前)。其次,UI线程应该通过在
thread.Start()
@IvanStoev之前添加
thread.SetApartmentState(ApartmentState.STA)
使之成为STA。我已经用您的建议更新了示例,但结果是一样的。