Last active
February 2, 2021 00:14
-
-
Save xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 to your computer and use it in GitHub Desktop.
Kaspresso Testing Blog Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
android:padding="16dp" | |
tools:context=".GradeCalculatorActivity"> | |
<androidx.appcompat.widget.AppCompatEditText | |
android:id="@+id/etMarks" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:hint="@string/hint_enter_your_marks" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/tvGrade" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:textAppearance="@style/TextAppearance.AppCompat.Large" | |
android:textColor="@color/colorAccent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@id/etMarks" | |
tools:text="Your Grade Is: A" /> | |
<androidx.appcompat.widget.AppCompatButton | |
android:id="@+id/btSubmit" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:text="@string/submit" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onScreen<FormScreen> { | |
phone { | |
hasText("971201771") | |
} | |
button { | |
click() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
percentage in 60..69 -> "D" | |
else -> "F" | |
} | |
} | |
private fun getPercentage(obtainedMarks: Int, totalMarks: Int): Int { | |
return (obtainedMarks / totalMarks.toFloat() * 100).roundToInt() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GradeCalculatorActivity : AppCompatActivity() { | |
companion object { | |
private const val TOTAL_MARKS = 100 | |
} | |
private val gradeCalculator = GradeCalculator() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_grade_calculator) | |
btSubmit.setOnClickListener { | |
val inputMarks = etMarks.text.toString().toInt() | |
val grade = gradeCalculator.getGrade(inputMarks, TOTAL_MARKS) | |
tvGrade.text = "Your Grade is: $grade" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
Then("grade is A") { | |
grade.shouldBe("A") | |
} | |
} | |
When("obtained marks are between 80 and 89") { | |
val grade = calculator.getGrade(88, total) | |
Then("grade is B") { | |
grade.shouldBe("B") | |
} | |
} | |
When("obtained marks are between 70 and 79") { | |
val grade = calculator.getGrade(78, total) | |
Then("grade is C") { | |
grade.shouldBe("C") | |
} | |
} | |
When("obtained marks are between 60 and 69") { | |
val grade = calculator.getGrade(68, total) | |
Then("grade is D") { | |
grade.shouldBe("D") | |
} | |
} | |
When("obtained marks are below 60") { | |
val grade = calculator.getGrade(59, total) | |
Then("grade is F") { | |
grade.shouldBe("F") | |
} | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
var rule = ActivityTestRule(GradeCalculatorActivity::class.java, false, false) | |
val screen = GradeCalculatorScreen() | |
@Test | |
fun onCorrectScoreSubmit_shouldDisplayCorrectGrade() = before { | |
// No-op | |
}.after { | |
// No-op | |
}.run { | |
screen { | |
step("Open grade calculator screen") { | |
rule.launchActivity(null) | |
} | |
step("Submit obtained marks") { | |
inputMarks.replaceText("90") | |
buttonSubmit.click() | |
} | |
step("Verify the displayed grade is correct") { | |
labelGrade.hasText("Your Grade is: A") | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KASPRESSO I --------------------------------------------------------------------------- | |
I BEFORE TEST SECTION | |
I --------------------------------------------------------------------------- | |
I --------------------------------------------------------------------------- | |
I TEST SECTION | |
I --------------------------------------------------------------------------- | |
I ___________________________________________________________________________ | |
I TEST STEP: "1. Open grade calculator screen" in GradeCalculatorUiTest | |
ActivityTestRule W getActivityIntent() returned null using default: Intent(Intent.ACTION_MAIN) | |
ActivityThread W handleWindowVisibility: no activity for token android.os.BinderProxy@efc119d | |
LifecycleMonitor D Lifecycle status change: com.zuhaibahmad.kaspressotestingdemo.GradeCalculatorActivity@76bf15e in: PRE_ON_CREATE | |
D running callback: com.kaspersky.kaspresso.device.languages.LanguageImpl$lifecycleCallback$1@934993f | |
D callback completes: com.kaspersky.kaspresso.device.languages.LanguageImpl$lifecycleCallback$1@934993f | |
essotestingdem W Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking) | |
W Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking) | |
W Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking) | |
W Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking) | |
W Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking) | |
W Accessing hidden method Landroid/view/View;->getAccessibilityDelegate()Landroid/view/View$AccessibilityDelegate; (light greylist, linking) | |
W Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflect | |
ion) | |
W Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection) | |
W Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking) | |
LifecycleMonitor D Lifecycle status change: com.zuhaibahmad.kaspressotestingdemo.GradeCalculatorActivity@76bf15e in: CREATED | |
D running callback: com.kaspersky.kaspresso.device.languages.LanguageImpl$lifecycleCallback$1@934993f | |
D callback completes: com.kaspersky.kaspresso.device.languages.LanguageImpl$lifecycleCallback$1@934993f | |
D Lifecycle status change: com.zuhaibahmad.kaspressotestingdemo.GradeCalculatorActivity@76bf15e in: STARTED | |
D running callback: com.kaspersky.kaspresso.device.languages.LanguageImpl$lifecycleCallback$1@934993f | |
D callback completes: com.kaspersky.kaspresso.device.languages.LanguageImpl$lifecycleCallback$1@934993f | |
D Lifecycle status change: com.zuhaibahmad.kaspressotestingdemo.GradeCalculatorActivity@76bf15e in: RESUMED | |
D running callback: com.kaspersky.kaspresso.device.languages.LanguageImpl$lifecycleCallback$1@934993f | |
D callback completes: com.kaspersky.kaspresso.device.languages.LanguageImpl$lifecycleCallback$1@934993f | |
OpenGLRenderer D HWUI GL Pipeline | |
HostConnection D HostConnection::get() New Host Connection established 0xe8b21370, tid 6867 | |
D HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_u | |
nmap_buffer GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 | |
ConfigStore I android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0 | |
I android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0 | |
OpenGLRenderer I Initialized EGL, version 1.4 | |
D Swap behavior 1 | |
W Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... | |
D Swap behavior 0 | |
eglCodecCommon D setVertexArrayObject: set vao to 0 (0) 0 0 | |
EGL_emulation D eglCreateContext: 0xe8b05420: maj 2 min 0 rcv 2 | |
D eglMakeCurrent: 0xe8b05420: ver 2 0 (tinfo 0xe8b03790) | |
HostConnection D createUnique: call | |
D HostConnection::get() New Host Connection established 0xe8b215f0, tid 6867 | |
D HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_u | |
nmap_buffer GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 | |
eglCodecCommon E GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1 | |
EGL_emulation D eglMakeCurrent: 0xe8b05420: ver 2 0 (tinfo 0xe8b03790) | |
AssistStructure I Flattened final assist data: 2688 bytes, containing 1 windows, 9 views | |
KASPRESSO I TEST STEP: "1. Open grade calculator screen" in GradeCalculatorUiTest SUCCEED. It took 0 minutes, 0 seconds and 339 millis. | |
I ___________________________________________________________________________ | |
KASPRESSO I ___________________________________________________________________________ | |
I TEST STEP: "2. Submit obtained marks" in GradeCalculatorUiTest | |
essotestingdem W Accessing hidden method Landroid/os/MessageQueue;->next()Landroid/os/Message; (light greylist, reflection) | |
W Accessing hidden field Landroid/os/MessageQueue;->mMessages:Landroid/os/Message; (light greylist, reflection) | |
W Accessing hidden method Landroid/os/Message;->recycleUnchecked()V (light greylist, reflection) | |
W Accessing hidden method Landroid/view/WindowManagerGlobal;->getInstance()Landroid/view/WindowManagerGlobal; (light greylist, reflection) | |
W Accessing hidden field Landroid/view/WindowManagerGlobal;->mViews:Ljava/util/ArrayList; (light greylist, reflection) | |
W Accessing hidden field Landroid/view/WindowManagerGlobal;->mParams:Ljava/util/ArrayList; (light greylist, reflection) | |
ViewInteraction I Performing 'replace text(90)' action on view (with id: com.zuhaibahmad.kaspressotestingdemo:id/etMarks) | |
KASPRESSO I replace text(90) on AppCompatEditText(id=etMarks;hint=Enter Your Marks;) | |
essotestingdem W Accessing hidden method Landroid/view/ViewConfiguration;->getDoubleTapMinTime()I (light greylist, reflection) | |
ViewInteraction I Performing 'single click' action on view (with id: com.zuhaibahmad.kaspressotestingdemo:id/btSubmit) | |
KASPRESSO I single click on AppCompatButton(id=btSubmit;text=Submit;) | |
I TEST STEP: "2. Submit obtained marks" in GradeCalculatorUiTest SUCCEED. It took 0 minutes, 0 seconds and 315 millis. | |
I ___________________________________________________________________________ | |
KASPRESSO I ___________________________________________________________________________ | |
I TEST STEP: "3. Verify the displayed grade is correct" in GradeCalculatorUiTest | |
ViewInteraction I Checking 'com.kaspersky.kaspresso.proxy.ViewAssertionProxy@d4df345' assertion on view (with id: com.zuhaibahmad.kaspressotestingdemo:id/tvGrad | |
e) | |
KASPRESSO I Check with text: is "Your Grade is: A" on AppCompatTextView(id=tvGrade;text=Your Grade is: A;) | |
I TEST STEP: "3. Verify the displayed grade is correct" in GradeCalculatorUiTest SUCCEED. It took 0 minutes, 0 seconds and 5 millis. | |
I ___________________________________________________________________________ | |
KASPRESSO I --------------------------------------------------------------------------- | |
I AFTER TEST SECTION | |
I --------------------------------------------------------------------------- | |
I --------------------------------------------------------------------------- | |
I TEST PASSED | |
I --------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment