docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Add styles during testing

    To add styles to elements during testing, call AddTestComponent<StylesApplicator>() to add a StylesApplicator component to the test fixture. The styles are automatically cleaned up at the end of the test.

    The following example shows how to add styles using the StylesApplicator:

    public class StylesApplicatorExampleClass : UITestFixture
    {
        StylesApplicator m_StylesApplicator;
    
        [OneTimeSetUp]
        public void OneTimeSetUp()
        {
            // Add the test component and keep its reference
            // to add styles to elements later.
            m_StylesApplicator = AddTestComponent<StylesApplicator>();
        }
    
        [Test]
        public void StylesApplicatorExample()
        {
            Button button = rootVisualElement.Q<Button>("MyButton");
    
            Assume.That(button.resolvedStyle.backgroundColor,
                Is.Not.EqualTo(Color.red));
    
            // Add a style to the button.
            m_StylesApplicator.AddStylesToElement(
                button, ".unity-button { background-color: rgb(255, 0, 0); }");
            simulate.FrameUpdate();
    
            Assert.That(button.resolvedStyle.backgroundColor,
                Is.EqualTo(Color.red));
    
            // Add a style to the root.
            m_StylesApplicator.AddStylesToRoot(
                "Button:hover { color: rgb(0, 0, 255); }");
            simulate.FrameUpdate();
    
            Assume.That(button.resolvedStyle.color,
                Is.Not.EqualTo(Color.blue));
    
            // Mouse over the button to activate the :hover
            // style that was added to the root.
            simulate.MouseMove(button.worldBound.center);
            simulate.FrameUpdate();
    
            Assert.That(button.resolvedStyle.color,
                Is.EqualTo(Color.blue));
        }
    
        [SetUp]
        public void SetUp()
        {
            VisualTreeAsset uxml = Resources.Load<VisualTreeAsset>("UITestFrameworkDocSample");
            uxml.CloneTree(rootVisualElement);
    
            simulate.FrameUpdate();
        }
    }
    

    Additional resources

    • UITestComponent
    • AddTestComponent<T>
    In This Article
    Back to top
    Copyright © 2026 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)