TestStack | How to find custom controls and invoke

2019-03-05 04:51发布

问题:

I have a custom Control HtButton which inherits from Control.

I made a custom FrameworkElementAutomationPeer and override the OnCreateAutomationPeer() in HtButton.

public class HtButtonAutomationPeer : FrameworkElementAutomationPeer
{
    public HtButtonAutomationPeer([NotNull] FrameworkElement owner) : base(owner)
    {
    }

    protected override string GetClassNameCore()
    {
        return "HtButton";
    }
}

public abstract class HtButtonBase : Control
{
}

public class HtButton : HtButtonBase
{
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new HtButtonAutomationPeer(this);
    }
}

To get all of them, I use the following call:

var myButtons = Window.GetMultiple(SearchCriteria.ByClassName("HtButton"));

I receive a list of CustomUIItem:

The following call throws an ArgumentException: At least two conditions must be specified.:

myButtons = Window.GetMultiple(SearchCriteria.ByControlType(typeof(HtButton), WindowsFramework.Wpf));
foreach (IUIItem myControl in myButtons)
{
    if (myControl is HtButton b)
    {

    }
}

Die Testmethode "HtFramework.WhiteUnitTest.HtPlcFrameworkTest.NavigationTest" hat eine Ausnahme ausgelöst: 
System.ArgumentException: Es müssen mindestens zwei Bedingungen angegeben werden.
    bei System.Windows.Automation.OrCondition..ctor(Condition[] conditions)
   bei TestStack.White.UIItems.Finders.OrSearchCondition.get_AutomationCondition() in c:\TeamCity\buildAgent\work\89a20b30302799e\src\TestStack.White\UIItems\Finders\OrSearchCondition.cs:Zeile 27.
   bei TestStack.White.UIItems.Finders.SearchCriteria.get_AutomationSearchCondition() in c:\TeamCity\buildAgent\work\89a20b30302799e\src\TestStack.White\UIItems\Finders\SearchCriteria.cs:Zeile 140.
   bei TestStack.White.Factory.PrimaryUIItemFactory.CreateAll(SearchCriteria searchCriteria, ActionListener actionListener) in c:\TeamCity\buildAgent\work\89a20b30302799e\src\TestStack.White\Factory\PrimaryUIItemFactory.cs:Zeile 80.
   bei TestStack.White.UIItems.Container.NonCachedContainerItemFactory.GetAll(SearchCriteria searchCriteria) in c:\TeamCity\buildAgent\work\89a20b30302799e\src\TestStack.White\UIItems\Container\NonCachedContainerItemFactory.cs:Zeile 30.
   bei TestStack.White.UIItems.Container.CurrentContainerItemFactory.FindAll(SearchCriteria criteria) in c:\TeamCity\buildAgent\work\89a20b30302799e\src\TestStack.White\UIItems\Container\CurrentContainerItemFactory.cs:Zeile 78.
   bei TestStack.White.UIItems.UIItemContainer.GetMultiple(SearchCriteria criteria) in c:\TeamCity\buildAgent\work\89a20b30302799e\src\TestStack.White\UIItems\UIItemContainer.cs:Zeile 205.
   bei HtFramework.WhiteUnitTest.MainWindow.NavigateTo() in ...hiddenPath...\HtFramework.WhiteUnitTest\UnitTest1.cs:Zeile 34.
   bei HtFramework.WhiteUnitTest.HtPlcFrameworkTest.<NavigationTest>d__6.MoveNext() in ...hiddenPath...\HtFramework.WhiteUnitTest\UnitTest1.cs:Zeile 196.
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
   bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   bei Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)

I have read the following posts and articles without success


  • TestStack White doesn't find TextBox in WPF Application
  • https://teststackwhite.readthedocs.io/en/latest/AdvancedTopics/CustomUIItems/
  • http://putridparrot.com/blog/teststack-white-gotchatips/

Maybe someone could give me a primitive example how to trigger method Foo() or fire event FooEvent of HtButton.

回答1:

There are other solutions I guess, some of which are a bit involved in my opionion (i.e. creating a custom UIItem subclass and so on).

What I finally went for is this:

var myButtons = Window.GetMultiple(SearchCriteria.ByClassName("HtButton"));
foreach (var button in myButtons )
{
    dynamic t = button;
    t.Foo();
}

It does definitely work, it's short and just requires you to reference Microsoft.CSharp.