Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to write automated tests for ui elements

Discussion in 'UI Toolkit' started by jshafferman, Oct 6, 2022.

  1. jshafferman

    jshafferman

    Joined:
    Feb 27, 2016
    Posts:
    2
    If I have a document controller like below:

    Code (CSharp):
    1. public class UIDocumentController : MonoBehaviour
    2. {
    3.     private Button button;
    4.  
    5.     private void OnEnable()
    6.     {
    7.         var uiDocument = GetComponent<UIDocument>();
    8.  
    9.         button = uiDocument.rootVisualElement.Q<Button>("SomeButton");
    10.  
    11.         button.RegisterCallback<ClickEvent>(DoSomething);
    12.     }
    13.  
    14.     private void DoSomething(ClickEvent evt)
    15.     {
    16.         Debug.Log("Hello firing click event");
    17.     }
    18. }
    If I wanted to see the console log get fired off from a Unity test. How would I do it? I am trying the following:

    Code (CSharp):
    1. [UnityTest]
    2.     public IEnumerator GivenXWhenYThenZ()
    3.     {
    4.         var ui = GameObject.Find("ElectricityUI");
    5.         var sut = ui.GetComponent<UIDocument>();
    6.  
    7.         var button = sut.rootVisualElement.Q<Button>("SomeButton");
    8.         // Create a UnityEngine.Event to hold initialization data.
    9.         var evt = new ClickEvent()
    10.         {
    11.             target = button
    12.         };
    13.  
    14.         using (ClickEvent clickEvent = ClickEvent.GetPooled(evt))
    15.         {
    16.             sut.rootVisualElement.SendEvent(clickEvent);
    17.         }
    18.  
    19.         yield return null;
    20.  
    21.         // Should see output in console but I don't
    22.     }