TestStack White doesn't find TextBox in WPF Application

落花浮王杯 提交于 2019-12-07 15:08:30

Let me know if that works

TextBox = (TextBox)CreateBranch
         .Get(SearchCriteria.ByAutomationId("Title").AndOfFramework(WindowsFramework.Wpf));

Edited after new source added

You have to create a specific AutomationPeer for your custom control and return it via the override of the method OnCreateAutomationPeer().

Your control is a subclass of the TextBox control so you can just return a new TextBoxAutomationPeer instance or create your custom AutomationPeer from it.

public class XTextBox : TextBox
{
...
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new XTextBoxAutomationPeer(this);
        // or just use the TextBoxAutomationPeer
        // return new TextBoxAutomationPeer(this);
    }
...
}

The custom automation peer

public class XTextBoxAutomationPeer : TextBoxAutomationPeer
{
    public XTextBoxAutomationPeer(XTextBox owner)
        : base(owner)
    {
    }

    protected override string GetClassNameCore()
    {
        return "XTextBox";
    }
}
[SetUpFixture]
    public class SETUP_THAT_WILL_GET_CALL_LATER
    {
        [OneTimeSetUp]
        public void OneTimeSetUp()
        {
            var applicationDirectory = TestContext.CurrentContext.TestDirectory;
            var applicationPath = Path.Combine(applicationDirectory, @"..\..\..\, "your debug folder path here", "your application.exe here");
            Application = Application.Launch(applicationPath);

            Thread.Sleep(2000);

            Window = Application.GetWindow("Title of your application", InitializeOption.WithCache);
        }

        [OneTimeTearDown()]
        public void OneTimeTearDown()
        {
            Window.Dispose();
            Application.Dispose();
        }

        public static Application Application;
        public static Window Window;
    }

Then in your test

[Test]
public void yourtest()
{
   var textBox = SETUP_THAT_WILL_GET_CALL_LATER.**Window.Get<TextBox>("Your textbox name here");**

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!