SlideShare a Scribd company logo
1 of 31
Download to read offline
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

Similar to Pragmatic UI testing with Compose Semantics.pdf

Espresso workshop
Espresso workshopEspresso workshop
Espresso workshopKetan Soni
 
QTP Training by INFOTECH
QTP Training by INFOTECHQTP Training by INFOTECH
QTP Training by INFOTECHPravinsinh
 
Hp Quick Test Professional
Hp Quick Test ProfessionalHp Quick Test Professional
Hp Quick Test Professionalsunny.deb
 
Slides for Automation Testing or End to End testing
Slides for Automation Testing or End to End testingSlides for Automation Testing or End to End testing
Slides for Automation Testing or End to End testingSwapnilNarayan
 
Testing Android Application, Droidcon Torino
Testing Android Application, Droidcon TorinoTesting Android Application, Droidcon Torino
Testing Android Application, Droidcon TorinoPietro Alberto Rossi
 
Top100summit 谷歌-scott-improve your automated web application testing
Top100summit  谷歌-scott-improve your automated web application testingTop100summit  谷歌-scott-improve your automated web application testing
Top100summit 谷歌-scott-improve your automated web application testingdrewz lin
 
Automated Testing on iOS
Automated Testing on iOSAutomated Testing on iOS
Automated Testing on iOSMake School
 
Dive into Angular, part 5: Experience
Dive into Angular, part 5: ExperienceDive into Angular, part 5: Experience
Dive into Angular, part 5: ExperienceOleksii Prohonnyi
 
Engineering Student MuleSoft Meetup#4 - API Testing With MuleSoft
Engineering Student MuleSoft Meetup#4 - API Testing With MuleSoftEngineering Student MuleSoft Meetup#4 - API Testing With MuleSoft
Engineering Student MuleSoft Meetup#4 - API Testing With MuleSoftJitendra Bafna
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosFlutter Agency
 
STAREAST 2011 - 7 Steps To Improving Software Quality using Microsoft Test Ma...
STAREAST 2011 - 7 Steps To Improving Software Quality using Microsoft Test Ma...STAREAST 2011 - 7 Steps To Improving Software Quality using Microsoft Test Ma...
STAREAST 2011 - 7 Steps To Improving Software Quality using Microsoft Test Ma...Anna Russo
 
Automated Acceptance Testing Example
Automated Acceptance Testing ExampleAutomated Acceptance Testing Example
Automated Acceptance Testing ExampleHani Massoud
 
Selenium Testing Training in Bangalore
Selenium Testing Training in BangaloreSelenium Testing Training in Bangalore
Selenium Testing Training in Bangalorerajkamal560066
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applicationsBabak Naffas
 
Gabriel carabat a healthy approach for test automation
Gabriel carabat   a healthy approach for test automationGabriel carabat   a healthy approach for test automation
Gabriel carabat a healthy approach for test automationRomania Testing
 
Visual Automation Framework via Screenshot Comparison
Visual Automation Framework via Screenshot ComparisonVisual Automation Framework via Screenshot Comparison
Visual Automation Framework via Screenshot ComparisonMek Srunyu Stittri
 
Good practices for debugging Selenium and Appium tests
Good practices for debugging Selenium and Appium testsGood practices for debugging Selenium and Appium tests
Good practices for debugging Selenium and Appium testsAbhijeet Vaikar
 
Advance UIAutomator : Documentaion
Advance UIAutomator : DocumentaionAdvance UIAutomator : Documentaion
Advance UIAutomator : DocumentaionRaman Gowda Hullur
 

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

Espresso workshop
Espresso workshopEspresso workshop
Espresso workshop
 
QTP Training by INFOTECH
QTP Training by INFOTECHQTP Training by INFOTECH
QTP Training by INFOTECH
 
Hp Quick Test Professional
Hp Quick Test ProfessionalHp Quick Test Professional
Hp Quick Test Professional
 
Slides for Automation Testing or End to End testing
Slides for Automation Testing or End to End testingSlides for Automation Testing or End to End testing
Slides for Automation Testing or End to End testing
 
Testing Android Application, Droidcon Torino
Testing Android Application, Droidcon TorinoTesting Android Application, Droidcon Torino
Testing Android Application, Droidcon Torino
 
Top100summit 谷歌-scott-improve your automated web application testing
Top100summit  谷歌-scott-improve your automated web application testingTop100summit  谷歌-scott-improve your automated web application testing
Top100summit 谷歌-scott-improve your automated web application testing
 
Automated Testing on iOS
Automated Testing on iOSAutomated Testing on iOS
Automated Testing on iOS
 
Coded ui test
Coded ui testCoded ui test
Coded ui test
 
Dive into Angular, part 5: Experience
Dive into Angular, part 5: ExperienceDive into Angular, part 5: Experience
Dive into Angular, part 5: Experience
 
Engineering Student MuleSoft Meetup#4 - API Testing With MuleSoft
Engineering Student MuleSoft Meetup#4 - API Testing With MuleSoftEngineering Student MuleSoft Meetup#4 - API Testing With MuleSoft
Engineering Student MuleSoft Meetup#4 - API Testing With MuleSoft
 
Rft courseware
Rft coursewareRft courseware
Rft courseware
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
 
STAREAST 2011 - 7 Steps To Improving Software Quality using Microsoft Test Ma...
STAREAST 2011 - 7 Steps To Improving Software Quality using Microsoft Test Ma...STAREAST 2011 - 7 Steps To Improving Software Quality using Microsoft Test Ma...
STAREAST 2011 - 7 Steps To Improving Software Quality using Microsoft Test Ma...
 
Automated Acceptance Testing Example
Automated Acceptance Testing ExampleAutomated Acceptance Testing Example
Automated Acceptance Testing Example
 
Selenium Testing Training in Bangalore
Selenium Testing Training in BangaloreSelenium Testing Training in Bangalore
Selenium Testing Training in Bangalore
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applications
 
Gabriel carabat a healthy approach for test automation
Gabriel carabat   a healthy approach for test automationGabriel carabat   a healthy approach for test automation
Gabriel carabat a healthy approach for test automation
 
Visual Automation Framework via Screenshot Comparison
Visual Automation Framework via Screenshot ComparisonVisual Automation Framework via Screenshot Comparison
Visual Automation Framework via Screenshot Comparison
 
Good practices for debugging Selenium and Appium tests
Good practices for debugging Selenium and Appium testsGood practices for debugging Selenium and Appium tests
Good practices for debugging Selenium and Appium tests
 
Advance UIAutomator : Documentaion
Advance UIAutomator : DocumentaionAdvance UIAutomator : Documentaion
Advance UIAutomator : Documentaion
 

Recently uploaded

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Recently uploaded (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

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