Nicola Gallazzi
he/him
Android developer
Pragmatic UI testing with
Compose Semantics
Agenda
1. Introduction to UI testing
2. Terminology
3. Why semantics?
4. Setup Compose UI testing
5. Build and Run!
Why automated
UI testing?
● Testing UI interactions ensure
users to do not fall into
unexpected results and poor user
experience
● Manual approach is
time-consuming and error prone
● Automatic approach guarantees
quickness and reliability
The old way -
Espresso UI testing
import org.junit.Test
onView(withId(R.id.tvName)).perform
(typeText("Steve"))
onView(withId(R.id.btGreet)).perfor
m(click())
The new way -
Compose testing API
● Compose provides a set of
testing APIs to find elements
● A semantic tree is generated
alongside the UI hierarchy
● onView “withId” matcher no
longer available
Terminology
1. Rules
2. Finders
3. Assertions
4. Actions
5. Matchers
Rules
● useful to set up the necessary
environment for a Compose UI test
● common Rule factory methods are
createComposeRule() and
createAndroidComposeRule<YourA
ctivity>()
Finders
● Convenient for the most common
searches
● Useful to select one or multiple
nodes of the semantic tree
● Can be used on a single node or on
a group of nodes
● Most common are
onNodeWithText,
onNodeWithContentDescription,
onNodeWithTag
Assertions
● Used to check that our
composable tree has the expected
elements with consistent content
and behavior
● If the condition(s) is verified the
test passes, otherwise it fails
● Most common are assertExists,
assertIsDisplayed,
assertTextEquals
Actions
● Used to perform a user action on a
UI component and change the
state of the UI
● Most common are performClick(),
performScrollTo(),
performTextInput()
Matchers
● Let you go up and down the
semantic tree
● Can be hierarchical or selectors
● Most common are hasParent,
hasAnySibling, hasTestTag
Accessibility and testing: contentDescription semantic
1) Accessibility: some people use
our app in a different way from
what we typically do. Semantic
content description tree helps
them to describe the app to users
with impaired vision
2) Testing: We have a convenient and
reliable way to find our “views” in
the semantic tree
Testing only alternative: testTag
1) Accessibility: no accessibility info
“out of the box”, still needs a
contentDescription
2) Testing: easy way to find the tag
by using onNodeWithTag finder
Setup Compose UI Testing - On/off Switch playground app
● Describe our UI elements with
semantics modifier
● Test ui state “OFF”
● Test ui state “ON”
Setup Compose UI Testing - Dependencies
Compose UI dependencies Testing dependencies
Handling Switch Status
● Keep switch status within an enum
variable, surviving to
recompositions
● Use semantics modifier to make
our layout accessible and test -
friendly
Handling User actions
● Enable / disable ON and OFF
buttons according to switch status
● Change status on user clicks
● Describe each button with
semantics modifier
Setup UI testing
● Set up our testing scenario by
using createAndroidComposeRule
● Use setContent method to inject
our layout in the UI test
● Perform ON and OFF actions
● Test that our “status description”
composable reacts accordingly
Setup UI testing
● createAndroidComposeRule allows you
to start an activity displaying any
composable
● We need “android” test rule, it will
come in handy to access string
resources
● setContent method inherited from
ComponentActivity
Perform click action (ON)
● A semantic node is needed to
perform the click action
● We can’t use findviewbyId
(Espresso-like)
● How do we find our semantic
node???
interface ComposeTestRule : TestRule,
SemanticsNodeInteractionsProvider
Test click action (ON)
● Find the “on button” semantic
node with
onNodeWithContentDescript
ion finder
● Use .performClick() action
to perform the click
● Find the “status” semantic
node
● assert “status” label has
changed accordingly
Test click action (OFF)
● Find the “off button” semantic
node with
onNodeWithContentDescripti
on finder
● Use .performClick()action to
perform the click
● Find the “status” semantic
node
● assert status composable text
has changed accordingly
Demo time!
Bonus track 1: custom semantic properties
● Useful to expose additional
semantics information to tests
● Definition by
SemanticsPropertyKey class in a
type safe way
● Property is made available with
SemanticsPropertyReceiver
Matching custom property in tests
● Use expectValue predicate to
check that a semantic property
exists with an expected value
Bonus track 2: testing navigation
Semantics modifier can be useful also to
describe each screen of our app.
This modifier will come in handy in our
UI test to check what is the current
displayed screen.
Bonus track 2: testing navigation - Setup
Before running the test we need to use
a special navController for tests:
TestNavHostController
Bonus track 2: testing navigation
Once the test is set up with our
TestNavHostController we can:
● Assert the first visible screen is “Switch
Screen”
● Perform click on “Credits” button
● Verify that our app has switched to
“credits” screen
● Enjoy the greens!!
Merged vs Unmerged tree
Sometimes is convenient to merge some
sub-trees of nodes and treat them as one
single node. We can use
mergeDescendants property for this
purpose
Questions?
THANK YOU!
Nicola Gallazzi
Email: nicola.gallazzi.dev@gmail.com
LinkedIn: nicola-gallazzi-dev
GitHub: ngallazzi
Project repo: Semantic-Testing-Playground
Useful links: Testing cheatsheet, Accessibility
and testing in Compose

More Related Content

PPTX
Compose In Practice
PDF
Никита Галкин "Testing in Frontend World"
PPTX
Xam expertday
PDF
Justin Ison
PDF
Testing in FrontEnd World by Nikita Galkin
PPTX
Xamarin.UITest. From "Zero" to "Hero"
PDF
Automated Exploratory Testing
PPTX
Suparna - XCUITest
Compose In Practice
Никита Галкин "Testing in Frontend World"
Xam expertday
Justin Ison
Testing in FrontEnd World by Nikita Galkin
Xamarin.UITest. From "Zero" to "Hero"
Automated Exploratory Testing
Suparna - XCUITest

Similar to Pragmatic UI testing with Compose Semantics.pdf (20)

PPTX
Mobile testing. Xamarin.UITest approach
POT
Fast deterministic screenshot tests for Android
PPTX
PPT from Geekle QA Global Summit 2023 conference
PPTX
Cross Platform Appium Tests: How To
PDF
Appium Native Application Crawler
PPTX
PDF
Android Testing Support Library: The Nitty Gritty - Zan Markan - Codemotion M...
PPTX
UI Testing for Your Xamarin.Forms Apps
PDF
Android UI Testing with Espresso - How to Get Started.pdf
PDF
UI Testing with Earl Grey
PDF
Uncovering breaking changes behind UI on mobile applications
PDF
Building Maintainable Android Apps (DroidCon NYC 2014)
PDF
A Semantics-based User Interface Model for Content Annotation, Authoring and ...
PDF
Workshop 23: ReactJS, React & Redux testing
PDF
iOS UI Testing in Xcode
PDF
JUnit for android
PDF
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
PDF
Advanced Appium Tips & Tricks with Jonathan Lipps
PDF
An existential guide to testing React UIs
PDF
Functional Testing
Mobile testing. Xamarin.UITest approach
Fast deterministic screenshot tests for Android
PPT from Geekle QA Global Summit 2023 conference
Cross Platform Appium Tests: How To
Appium Native Application Crawler
Android Testing Support Library: The Nitty Gritty - Zan Markan - Codemotion M...
UI Testing for Your Xamarin.Forms Apps
Android UI Testing with Espresso - How to Get Started.pdf
UI Testing with Earl Grey
Uncovering breaking changes behind UI on mobile applications
Building Maintainable Android Apps (DroidCon NYC 2014)
A Semantics-based User Interface Model for Content Annotation, Authoring and ...
Workshop 23: ReactJS, React & Redux testing
iOS UI Testing in Xcode
JUnit for android
[Srijan Wednesday Webinar] Mastering Mobile Test Automation with Appium
Advanced Appium Tips & Tricks with Jonathan Lipps
An existential guide to testing React UIs
Functional Testing
Ad

More from infogdgmi (7)

PDF
Ktor - Definizioni di Path, Integrazioni, Plugin e build fino al rilascio
PDF
Let's Build a House Price Predictor with Google Cloud!.pdf
PDF
Public Speaking - Il Potere delle Voce.pdf
PDF
ScrapeGraphAI: a new way to scrape context with AI
PDF
Videogame localization & technology_ how to enhance the power of translation.pdf
PDF
Da Arduino ad Android_ illumina il Natale con il BLE
PDF
Droids on wheels
Ktor - Definizioni di Path, Integrazioni, Plugin e build fino al rilascio
Let's Build a House Price Predictor with Google Cloud!.pdf
Public Speaking - Il Potere delle Voce.pdf
ScrapeGraphAI: a new way to scrape context with AI
Videogame localization & technology_ how to enhance the power of translation.pdf
Da Arduino ad Android_ illumina il Natale con il BLE
Droids on wheels
Ad

Recently uploaded (20)

DOCX
search engine optimization ppt fir known well about this
PPTX
TEXTILE technology diploma scope and career opportunities
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Comparative analysis of machine learning models for fake news detection in so...
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
UiPath Agentic Automation session 1: RPA to Agents
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPTX
2018-HIPAA-Renewal-Training for executives
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
STKI Israel Market Study 2025 version august
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
search engine optimization ppt fir known well about this
TEXTILE technology diploma scope and career opportunities
Consumable AI The What, Why & How for Small Teams.pdf
Benefits of Physical activity for teenagers.pptx
Comparative analysis of machine learning models for fake news detection in so...
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Convolutional neural network based encoder-decoder for efficient real-time ob...
A proposed approach for plagiarism detection in Myanmar Unicode text
UiPath Agentic Automation session 1: RPA to Agents
Microsoft Excel 365/2024 Beginner's training
OpenACC and Open Hackathons Monthly Highlights July 2025
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
2018-HIPAA-Renewal-Training for executives
A review of recent deep learning applications in wood surface defect identifi...
1 - Historical Antecedents, Social Consideration.pdf
STKI Israel Market Study 2025 version august
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf

Pragmatic UI testing with Compose Semantics.pdf

  • 1. Nicola Gallazzi he/him Android developer Pragmatic UI testing with Compose Semantics
  • 2. Agenda 1. Introduction to UI testing 2. Terminology 3. Why semantics? 4. Setup Compose UI testing 5. Build and Run!
  • 3. Why automated UI testing? ● Testing UI interactions ensure users to do not fall into unexpected results and poor user experience ● Manual approach is time-consuming and error prone ● Automatic approach guarantees quickness and reliability
  • 4. The old way - Espresso UI testing import org.junit.Test onView(withId(R.id.tvName)).perform (typeText("Steve")) onView(withId(R.id.btGreet)).perfor m(click())
  • 5. The new way - Compose testing API ● Compose provides a set of testing APIs to find elements ● A semantic tree is generated alongside the UI hierarchy ● onView “withId” matcher no longer available
  • 6. Terminology 1. Rules 2. Finders 3. Assertions 4. Actions 5. Matchers
  • 7. Rules ● useful to set up the necessary environment for a Compose UI test ● common Rule factory methods are createComposeRule() and createAndroidComposeRule<YourA ctivity>()
  • 8. Finders ● Convenient for the most common searches ● Useful to select one or multiple nodes of the semantic tree ● Can be used on a single node or on a group of nodes ● Most common are onNodeWithText, onNodeWithContentDescription, onNodeWithTag
  • 9. Assertions ● Used to check that our composable tree has the expected elements with consistent content and behavior ● If the condition(s) is verified the test passes, otherwise it fails ● Most common are assertExists, assertIsDisplayed, assertTextEquals
  • 10. Actions ● Used to perform a user action on a UI component and change the state of the UI ● Most common are performClick(), performScrollTo(), performTextInput()
  • 11. Matchers ● Let you go up and down the semantic tree ● Can be hierarchical or selectors ● Most common are hasParent, hasAnySibling, hasTestTag
  • 12. Accessibility and testing: contentDescription semantic 1) Accessibility: some people use our app in a different way from what we typically do. Semantic content description tree helps them to describe the app to users with impaired vision 2) Testing: We have a convenient and reliable way to find our “views” in the semantic tree
  • 13. Testing only alternative: testTag 1) Accessibility: no accessibility info “out of the box”, still needs a contentDescription 2) Testing: easy way to find the tag by using onNodeWithTag finder
  • 14. Setup Compose UI Testing - On/off Switch playground app ● Describe our UI elements with semantics modifier ● Test ui state “OFF” ● Test ui state “ON”
  • 15. Setup Compose UI Testing - Dependencies Compose UI dependencies Testing dependencies
  • 16. Handling Switch Status ● Keep switch status within an enum variable, surviving to recompositions ● Use semantics modifier to make our layout accessible and test - friendly
  • 17. Handling User actions ● Enable / disable ON and OFF buttons according to switch status ● Change status on user clicks ● Describe each button with semantics modifier
  • 18. Setup UI testing ● Set up our testing scenario by using createAndroidComposeRule ● Use setContent method to inject our layout in the UI test ● Perform ON and OFF actions ● Test that our “status description” composable reacts accordingly
  • 19. Setup UI testing ● createAndroidComposeRule allows you to start an activity displaying any composable ● We need “android” test rule, it will come in handy to access string resources ● setContent method inherited from ComponentActivity
  • 20. Perform click action (ON) ● A semantic node is needed to perform the click action ● We can’t use findviewbyId (Espresso-like) ● How do we find our semantic node??? interface ComposeTestRule : TestRule, SemanticsNodeInteractionsProvider
  • 21. Test click action (ON) ● Find the “on button” semantic node with onNodeWithContentDescript ion finder ● Use .performClick() action to perform the click ● Find the “status” semantic node ● assert “status” label has changed accordingly
  • 22. Test click action (OFF) ● Find the “off button” semantic node with onNodeWithContentDescripti on finder ● Use .performClick()action to perform the click ● Find the “status” semantic node ● assert status composable text has changed accordingly
  • 24. Bonus track 1: custom semantic properties ● Useful to expose additional semantics information to tests ● Definition by SemanticsPropertyKey class in a type safe way ● Property is made available with SemanticsPropertyReceiver
  • 25. Matching custom property in tests ● Use expectValue predicate to check that a semantic property exists with an expected value
  • 26. Bonus track 2: testing navigation Semantics modifier can be useful also to describe each screen of our app. This modifier will come in handy in our UI test to check what is the current displayed screen.
  • 27. Bonus track 2: testing navigation - Setup Before running the test we need to use a special navController for tests: TestNavHostController
  • 28. Bonus track 2: testing navigation Once the test is set up with our TestNavHostController we can: ● Assert the first visible screen is “Switch Screen” ● Perform click on “Credits” button ● Verify that our app has switched to “credits” screen ● Enjoy the greens!!
  • 29. Merged vs Unmerged tree Sometimes is convenient to merge some sub-trees of nodes and treat them as one single node. We can use mergeDescendants property for this purpose
  • 31. THANK YOU! Nicola Gallazzi Email: nicola.gallazzi.dev@gmail.com LinkedIn: nicola-gallazzi-dev GitHub: ngallazzi Project repo: Semantic-Testing-Playground Useful links: Testing cheatsheet, Accessibility and testing in Compose