Skip to content

Commit

Permalink
Click option menu in Espresso (#274)
Browse files Browse the repository at this point in the history
* Click option menu in Espresso
  • Loading branch information
hannesa2 authored Aug 4, 2024
1 parent e7371b5 commit 9418940
Show file tree
Hide file tree
Showing 236 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import androidx.test.espresso.Espresso.onData
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.screenshot.captureToBitmap
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase.Companion.optionMenus
import com.xxmassdeveloper.mpchartexample.notimportant.MainActivity
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.CoreMatchers.anything
Expand All @@ -23,9 +28,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestName
import org.junit.runner.RunWith
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation


@RunWith(AndroidJUnit4::class)
class StartTest {
Expand All @@ -52,27 +55,45 @@ class StartTest {
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")

var optionMenu = ""
// iterate samples
MainActivity.menuItems.forEachIndexed { index, contentItem ->
contentItem.clazz?.let {
Log.d(nameRule.methodName, "Intended ${index}-${it.simpleName}")

onData(anything())
.inAdapterView(allOf(withId(R.id.listViewMain), isCompletelyDisplayed()))
.atPosition(index).perform(click())
try {
onData(anything())
.inAdapterView(allOf(withId(R.id.listViewMain), isCompletelyDisplayed()))
.atPosition(index).perform(click())

Intents.intended(hasComponent(it.name))
takeScreenshot()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-${index}-${it.simpleName}-${contentItem.name}-1SampleClick")

Intents.intended(hasComponent(it.name))
takeScreenshot()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-${index}-${it.simpleName}-click")
optionMenu = ""
optionMenus.filter { plain -> Character.isDigit(plain.first()) }.forEach { filteredTitle ->
optionMenu = "$index->$filteredTitle"
openActionBarOverflowOrOptionsMenu(getInstrumentation().targetContext)
screenshotOfOptionMenu("${javaClass.simpleName}_${nameRule.methodName}-${index}-${it.simpleName}-${contentItem.name}", filteredTitle)
}

openActionBarOverflowOrOptionsMenu(getInstrumentation().targetContext)
Thread.sleep(100)
takeScreenshot()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-${index}-${it.simpleName}-menu")
Espresso.pressBack()
Thread.sleep(100)
Espresso.pressBack()
// Espresso.pressBack()
//Thread.sleep(100)
Espresso.pressBack()
} catch (e: Exception) {
Log.e("smokeTestStart", optionMenu + e.message!!)
takeScreenshot()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}-${index}-${it.simpleName}-Error")
}
}
}
}

private fun screenshotOfOptionMenu(simpleName: String, menuTitle: String) {
onView(withText(menuTitle)).perform(click())
Log.d(nameRule.methodName, "screenshotOfOptionMenu ${menuTitle}-${simpleName}")
takeScreenshot()
.writeToTestStorage("${simpleName}-2menu-click-$menuTitle")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.Manifest
import android.content.pm.PackageManager
import android.graphics.Typeface
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -31,11 +33,22 @@ abstract class DemoBase : AppCompatActivity(), ActivityCompat.OnRequestPermissio

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
optionMenus.clear()

tfRegular = Typeface.createFromAsset(assets, "OpenSans-Regular.ttf")
tfLight = Typeface.createFromAsset(assets, "OpenSans-Light.ttf")
}

override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
menu?.let {
for (i in 0 until menu.size()) {
val menuItem: MenuItem = menu.getItem(i)
optionMenus.add(menuItem.title.toString())
}
}
return super.onPrepareOptionsMenu(menu)
}

override fun onBackPressed() {
super.onBackPressed()
overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity)
Expand Down Expand Up @@ -84,5 +97,6 @@ abstract class DemoBase : AppCompatActivity(), ActivityCompat.OnRequestPermissio
private const val PERMISSION_STORAGE = 0
// Jan, Feb,... Dec
val months = DateFormatSymbols().months.toList().map { it.take(3) }
val optionMenus: MutableList<String> = mutableListOf()
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.

0 comments on commit 9418940

Please sign in to comment.