11package info.appdev.chartexample
22
33import android.graphics.Bitmap
4+ import androidx.compose.ui.test.assertIsDisplayed
5+ import androidx.compose.ui.test.junit4.createEmptyComposeRule
6+ import androidx.compose.ui.test.onNodeWithTag
7+ import androidx.compose.ui.test.onNodeWithText
8+ import androidx.compose.ui.test.performClick
9+ import androidx.compose.ui.test.performScrollToIndex
410import androidx.test.core.graphics.writeToTestStorage
511import androidx.test.espresso.Espresso
6- import androidx.test.espresso.Espresso.onData
712import androidx.test.espresso.Espresso.onView
813import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
914import androidx.test.espresso.action.ViewActions.captureToBitmap
1015import androidx.test.espresso.action.ViewActions.click
1116import androidx.test.espresso.intent.Intents
1217import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
1318import androidx.test.espresso.matcher.ViewMatchers
14- import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed
15- import androidx.test.espresso.matcher.ViewMatchers.withId
1619import androidx.test.espresso.matcher.ViewMatchers.withText
1720import androidx.test.ext.junit.rules.activityScenarioRule
1821import androidx.test.ext.junit.runners.AndroidJUnit4
1922import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
2023import info.appdev.chartexample.notimportant.DemoBase.Companion.optionMenus
2124import info.appdev.chartexample.notimportant.MainActivity
2225import info.hannes.timber.DebugFormatTree
23- import org.hamcrest.CoreMatchers.allOf
24- import org.hamcrest.CoreMatchers.anything
2526import org.junit.After
2627import org.junit.Before
2728import org.junit.Rule
@@ -37,6 +38,9 @@ class StartTest {
3738 @get:Rule
3839 val activityScenarioRule = activityScenarioRule<MainActivity >()
3940
41+ @get:Rule
42+ val composeTestRule = createEmptyComposeRule()
43+
4044 @get:Rule
4145 var nameRule = TestName ()
4246
@@ -53,19 +57,47 @@ class StartTest {
5357
5458 @Test
5559 fun smokeTestStart () {
60+ // Wait for Compose to be ready
61+ composeTestRule.waitForIdle()
62+
5663 onView(ViewMatchers .isRoot())
5764 .perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} " ) })
5865
5966 var optionMenu = " "
60- // iterate samples
67+ // iterate samples - only items with classes (not section headers)
6168 MainActivity .menuItems.forEachIndexed { index, contentItem ->
6269 contentItem.clazz?.let {
63- Timber .d(" Intended ${index} -${it.simpleName} " )
70+ Timber .d(" Intended ${index} -${it.simpleName} : ${contentItem.name} " )
6471
6572 try {
66- onData(anything())
67- .inAdapterView(allOf(withId(R .id.listViewMain), isCompletelyDisplayed()))
68- .atPosition(index).perform(click())
73+ // Use description to uniquely identify items since names can be duplicated
74+ // If description exists, use it; otherwise fall back to name
75+ val searchText = if (contentItem.desc.isNotEmpty()) {
76+ contentItem.desc
77+ } else {
78+ contentItem.name
79+ }
80+
81+ Timber .d(" Searching for index $index : $searchText " )
82+
83+ // Scroll to the item in the LazyColumn by index
84+ // This ensures the item is composed and visible
85+ try {
86+ composeTestRule.onNodeWithTag(" menuList" )
87+ .performScrollToIndex(index)
88+ composeTestRule.waitForIdle()
89+ } catch (e: Exception ) {
90+ Timber .w(" Could not scroll to index $index : ${e.message} " )
91+ }
92+
93+ // Now click the item using its test tag
94+ composeTestRule.onNodeWithTag(" menuItem_$index " )
95+ .assertExists(" Could not find menu item at index $index " )
96+ .performClick()
97+
98+ // Wait for the new activity to start
99+ composeTestRule.waitForIdle()
100+ Thread .sleep(300 ) // Increased delay for activity transition
69101
70102 Intents .intended(hasComponent(it.name))
71103 onView(ViewMatchers .isRoot())
@@ -80,8 +112,12 @@ class StartTest {
80112
81113 // Thread.sleep(100)
82114 Espresso .pressBack()
115+
116+ // Wait for MainActivity to be visible again
117+ composeTestRule.waitForIdle()
118+ Thread .sleep(200 ) // Small delay for back navigation
83119 } catch (e: Exception ) {
84- Timber .e(optionMenu + e.message!! )
120+ Timber .e(" Error at index $index : $ optionMenu - ${ e.message} " , e )
85121 onView(ViewMatchers .isRoot())
86122 .perform(captureToBitmap { bitmap: Bitmap -> bitmap.writeToTestStorage(" ${javaClass.simpleName} _${nameRule.methodName} -${index} -${it.simpleName} -Error" ) })
87123 }
0 commit comments