Skip to content

Instantly share code, notes, and snippets.

View xuhaibahmad's full-sized avatar
💭
¯\_(ツ)_/¯

Zuhaib Ahmad xuhaibahmad

💭
¯\_(ツ)_/¯
View GitHub Profile
class GradeCalculatorActivity : AppCompatActivity() {
companion object {
private const val TOTAL_MARKS = 100
}
private val gradeCalculator = GradeCalculator()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
class GradeCalculator {
var totalMarks = 0
fun getGrade(obtainedMarks: Int, totalMarks: Int): String {
val percentage = getPercentage(obtainedMarks, totalMarks)
return when {
percentage >= 90 -> "A"
percentage in 80..89 -> "B"
percentage in 70..79 -> "C"
class GradeCalculatorSpec : BehaviorSpec({
Given("a grade calculator") {
val calculator = spyk(GradeCalculator())
every { calculator.totalMarks } returns 100
val total = calculator.totalMarks
When("obtained marks are 90 or above") {
val grade = calculator.getGrade(93, total)
import com.agoda.kakao.edit.KEditText
import com.agoda.kakao.screen.Screen
import com.agoda.kakao.text.KButton
import com.agoda.kakao.text.KTextView
import com.zuhaibahmad.kaspressotestingdemo.R
class GradeCalculatorScreen: Screen<GradeCalculatorScreen>() {
val inputMarks = KEditText { withId(R.id.etMarks) }
val buttonSubmit = KButton { withId(R.id.btSubmit) }
val labelGrade = KTextView { withId(R.id.tvGrade) }
import androidx.test.rule.ActivityTestRule
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
import com.zuhaibahmad.kaspressotestingdemo.screens.GradeCalculatorScreen
import org.junit.Rule
import org.junit.Test
class GradeCalculatorUiTest : TestCase() {
@Rule
@JvmField
testImplementation 'junit:junit:4.13'
implementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.1.7'
testImplementation 'io.mockk:mockk:1.9.3'
androidTestImplementation 'com.kaspersky.android-components:kaspresso:1.1.0'
onScreen<FormScreen> {
phone {
hasText("971201771")
}
button {
click()
}
}
class FormScreen : Screen<FormScreen>() {
val phone = KView { withId(R.id.phone) }
val email = KEditText { withId(R.id.email) }
val submit = KButton { withId(R.id.submit) }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etMarks"
@xuhaibahmad
xuhaibahmad / FormScreen.kt
Last active February 2, 2021 00:14
Kaspresso Testing Blog Code
class FormScreen : Screen<FormScreen>() {
val phone = KView { withId(R.id.phone) }
val email = KEditText { withId(R.id.email) }
val submit = KButton { withId(R.id.submit) }
}