docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Simulate popup menu actions

    To validate and simulate popup menu actions in your tests, use the AddTestComponent<T> method to create a PopupMenuSimulator test component. The PopupMenuSimulator doesn't spawn popup menus, so you must activate them in the test.

    The following example shows how to use the PopupMenuSimulator to validate and interact with a popup menu:

    public class PopupMenuSimulatorExampleClass : UITestFixture
    {
        PopupMenuSimulator m_PopupMenuSimulator;
    
        [OneTimeSetUp]
        public void OneTimeSetUp()
        {
            // Add the test component and keep its reference
            // to interact with popup menus later.
            m_PopupMenuSimulator = AddTestComponent<PopupMenuSimulator>();
        }
    
        [Test]
        public void PopupMenuSimulatorExample()
        {
            // Fetch the DropdownMenu to interact with.
            DropdownField dropdownField = rootVisualElement.Q<DropdownField>("MyDropdownField");
    
            VisualElement dropdown = dropdownField.Q<VisualElement>(className: "unity-base-popup-field__input");
    
            Assume.That(dropdownField.value, Is.Null);
    
            // Click on the dropdown.
            simulate.Click(dropdown);
            simulate.FrameUpdate();
    
            // Check that the menu is displayed.
            Assert.That(m_PopupMenuSimulator.menuIsDisplayed,
                Is.True, "PopupMenu was not displayed.");
    
            // Check that the menu contains the item we want to select.
            m_PopupMenuSimulator.AssertContainsAction("Option1");
    
            // Select Option1 by its name.
            m_PopupMenuSimulator.SimulateMenuSelection("Option1");
            Assert.That(dropdownField.value, Is.EqualTo("Option1"));
    
            // Discard the menu.
            // PopupMenuSimulator does not currently automatically discard the menu,
            // so it needs to be manually discarded before opening another menu.
            m_PopupMenuSimulator.DiscardMenu();
    
            // Click on the dropdown again.
            simulate.Click(dropdown);
            simulate.FrameUpdate();
    
            Assert.That(m_PopupMenuSimulator.menuIsDisplayed,
                Is.True, "PopupMenu was not displayed.");
    
            m_PopupMenuSimulator.AssertContainsAction("Option2");
    
            // Simulate item selection by its index.
            var itemIndex = m_PopupMenuSimulator.FindActionIndex("Option2");
            m_PopupMenuSimulator.SimulateItemSelection(itemIndex);
    
            Assert.That(dropdownField.value, Is.EqualTo("Option2"));
        }
    
        [SetUp]
        public void SetUp()
        {
            VisualTreeAsset uxml = Resources.Load<VisualTreeAsset>("UITestFrameworkDocSample");
            uxml.CloneTree(rootVisualElement);
    
            simulate.FrameUpdate();
        }
    }
    

    Additional resources

    • Simulate context menu actions
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)