Skip to content

Instantly share code, notes, and snippets.

View carolinemusyoka's full-sized avatar
:octocat:
so?.let { it -> be (it) }

Carol carolinemusyoka

:octocat:
so?.let { it -> be (it) }
View GitHub Profile
import africastalking
username = "africa username" # use 'sandbox' for development in the test environment
# use your sandbox app API key for development in the test environment
api_key = "africa's talking api key"
africastalking.initialize(username, api_key)
# # Use the service synchronously
@carolinemusyoka
carolinemusyoka / Robot Pattern.md
Created March 26, 2020 11:38 — forked from bharatdodeja/Robot Pattern.md
Android UI testing using Espresso and Robot pattern

Android UI Testing using Espresso, Rotton Pattern and Kotlin DSL

Espresso allow UI tests to have stable interactions with your app, but without discipline these tests can become hard to manage and require frequent updating. Robot pattern allows you to create stable, readable, and maintainable tests with the aid of Kotlin’s language features.

Test Excecution

Let's say there is a login screen and user needs to login access dashboard by entering email and password and clicking on the login button. This is what we want the test to validate. But there’s also the how. Really, what we are looking for is the what.

Traditional Testing (Problem)

A way that you might write the test is something like this:

implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'com.google.android.material:material:1.2.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
//LifeCycle
implementation 'androidx.lifecycle:lifecycle-common:2.2.0'
implementation 'androidx.lifecycle:lifecycle-runtime:2.2.0'
implementation 'android.arch.lifecycle:extensions:2.2.0'
<uses-permission android:name="android.permission.INTERNET" />
import com.google.gson.annotations.SerializedName
data class Hero(
@SerializedName("id")
val id: Long,
@SerializedName("name")
val name: String,
@SerializedName("slug")
interface ApiService {
@GET("all.json")
suspend fun getSuperHeroes(): List<Hero>
}
class ApiHelper(private val apiService: ApiService) {
suspend fun getSuperHeroes() = apiService.getSuperHeroes()
}
object RetrofitBuilder {
private const val BASE_URL = "https://akabab.github.io/superhero-api/api/"
private fun getRetrofit(): Retrofit{
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
class MainRepository(private val apiHelper: ApiHelper) {
suspend fun getSuperHeroes() = apiHelper.getSuperHeroes()
}
data class Resource<out T>(val status: Status, val data: T?, val message: String?) {
companion object {
fun <T> success(data: T): Resource<T> = Resource(status = Status.SUCCESS, data = data, message = null)
fun <T> error(data: T?, message: String): Resource<T> =
Resource(status = Status.ERROR, data = data, message = message)
fun <T> loading(data: T?): Resource<T> = Resource(status = Status.LOADING, data = data, message = null)
}
}