Site icon C1CTech

Android Junit and Espresso Testing using CalculatorApp example

<p>This article is designed to get a basic understanding of how to add JUnit and espresso testing in the android application with the help of a simple <span style&equals;"color&colon; &num;008000&semi;"><b>calculator app<&sol;b><&sol;span>&period;<&sol;p>&NewLine;<p>CalculatorApp will perform operations like addition&comma; subtraction&comma; multiplication&comma; division&comma; percentage only on two numbers&period; In CalculatorApp we will write Unit and UI test cases on all the operations&period;<&sol;p>&NewLine;<p>When you run the application <strong><span style&equals;"color&colon; &num;0000ff&semi;">CalculatorApp<&sol;span><&sol;strong> it will give you the output as shown below&colon;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"z8s6IcMrfVM" title&equals;"Android Junit and Espresso Testing using CalculatorApp example"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;z8s6IcMrfVM"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;z8s6IcMrfVM&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Junit and Espresso Testing using CalculatorApp example"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">Get Complete GITHUB code from<&sol;span> <span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;CalculatorApp">here<&sol;a><&sol;span>&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Testing<&sol;span><&sol;strong><&sol;h3>&NewLine;<p class&equals;"p1">By running tests against your app consistently&comma; you can verify your app&&num;8217&semi;s correctness&comma; functional behavior&comma; and usability before you release it publicly&period;<&sol;p>&NewLine;<p class&equals;"p1"><span class&equals;"s1">A typical project in Android Studio contains two directories in which you place tests&period; <&sol;span><&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li2"><span class&equals;"s2">The <&sol;span><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"s3">androidTest<&sol;span><&sol;strong><&sol;span><span class&equals;"s2"> directory should contain the tests that run on real or virtual devices&period; Such tests include integration tests&comma; end-to-end tests&comma; and other tests where the JVM alone cannot validate your app&&num;8217&semi;s functionality&period;<&sol;span><&sol;li>&NewLine;<li class&equals;"li2"><span class&equals;"s2">The <&sol;span><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"s3">test<&sol;span><&sol;strong><&sol;span><span class&equals;"s2"> directory should contain the tests that run on your local machine&comma; such as unit tests&period;<&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Testing Tools in android<&sol;span><&sol;strong><&sol;h3>&NewLine;<p class&equals;"p1">There are many tools that can be used for testing android applications&period; Some are official like Junit&comma; Espresso and some are third-party tools that can be used to test android applications&period; In this article&comma; we will talk about these two tools to test android applications&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li2">Junit<&sol;li>&NewLine;<li class&equals;"li2">Espresso<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">JUnit<&sol;span><&sol;strong><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Junit<&sol;span><&sol;strong> is a &OpenCurlyDoubleQuote;Unit Testing” framework for Java Applications&period;<&sol;p>&NewLine;<p>Unit testing is a way of testing the smallest piece of code referred to as a <strong>unit<&sol;strong> &lpar;i&period;e method&comma; class&comma; component&rpar; that can be logically isolated in a system&period;<&sol;p>&NewLine;<p>It is mainly focused on the functional correctness of standalone modules&period;<&sol;p>&NewLine;<p>Unit testing consists of test cases that are used to check the business logic of your code&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Espresso<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;008000&semi;"><b>Espresso<&sol;b><&sol;span> is an open-source UI Testing framework made available by Google&period;<&sol;p>&NewLine;<p class&equals;"p1">It is used to automate manual test cases for Android applications&period;<&sol;p>&NewLine;<p class&equals;"p1">Espresso automatically <span style&equals;"color&colon; &num;008000&semi;"><b>synchronizes<&sol;b><&sol;span> your test actions with the UI of your application&period; The framework also ensures that your activity is started before the tests run and also lets the test wait until all observed background activities have finished&period;<&sol;p>&NewLine;<p>If you are new to Espresso then refer to my previous article <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;android-espresso-testing-basics&sol;">Android Espresso Testing Basics<&sol;a><&sol;span><&sol;strong> &period;<&sol;p>&NewLine;<p class&equals;"p1">The main components of Espresso include the following&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Espresso<&sol;b><&sol;span> – Entry point to interactions with views &lpar;via <span style&equals;"color&colon; &num;008000&semi;"><b>onView&lpar;&rpar; and onData&lpar;&rpar;<&sol;b><&sol;span>&rpar;&period;<&sol;li>&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>ViewMatchers<&sol;b><&sol;span> –<span style&equals;"color&colon; &num;008000&semi;"><b>&lpar;Match a view&rpar;<&sol;b><&sol;span> Allows you to find  view in the current view hierarchy&period;<&sol;li>&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>ViewActions<&sol;b><&sol;span> – <span style&equals;"color&colon; &num;008000&semi;"><b>&lpar;Perform an action&rpar;<&sol;b><&sol;span>Allows you to perform an action on a View&period;<&sol;li>&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>ViewAssertions<&sol;b><&sol;span> – <span style&equals;"color&colon; &num;008000&semi;"><b>&lpar;Assert and verify the result&rpar;<&sol;b><&sol;span>Check the state of the View to see if it reflects the expected state or behavior defined by the assertion&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Now let&&num;8217&semi;s start creating our CalculatorApp example&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating CalculatorApp<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>1 &period; In our CalculatorApp inside<span style&equals;"color&colon; &num;0000ff&semi;"><strong> main&gt&semi;java<&sol;strong><&sol;span> we will create one java file <span style&equals;"color&colon; &num;008000&semi;"><strong>Calculation&period;Java<&sol;strong><&sol;span> in which we will define all the operations &lpar;ie&period; addition &comma; subtraction&comma; multiplication &comma; division&comma; percentage&rpar; which we want to perform on two numbers&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Calculation&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;calculatorapp&semi;&NewLine;&NewLine;public class Calculation &lbrace;&NewLine;&NewLine; public double addTwoNumbers&lpar;double a&comma; double b&rpar; &lbrace;&NewLine; return a &plus; b&semi;&NewLine; &rcub;&NewLine;&NewLine; public double subtractTwoNumbers&lpar;double a&comma; double b&rpar; &lbrace;&NewLine; return a - b&semi;&NewLine; &rcub;&NewLine;&NewLine; public double multiplyTwoNumbers&lpar;double a&comma; double b&rpar; &lbrace;&NewLine; return a &ast; b&semi;&NewLine; &rcub;&NewLine;&NewLine; public double divideTwoNumbers&lpar;double a&comma; double b&rpar; &lbrace;&NewLine; return a &sol; b&semi;&NewLine; &rcub;&NewLine;&NewLine; public double calculatePercentage&lpar;double a&rpar; &lbrace;&NewLine; return a &sol; 100&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<p>2 &period;To create Unit and UI tests cases for the operations defined in Calculation class we will create two classes&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>CalculationUnitTest<&sol;strong><&sol;span> under <span style&equals;"color&colon; &num;008000&semi;"><strong>test<&sol;strong><&sol;span> folder for writing Unit tests&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>CalculationTest<&sol;strong><&sol;span> under <strong><span style&equals;"color&colon; &num;008000&semi;">androidTest<&sol;span><&sol;strong> folder for writing UI tests&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>3 &period;Now right-click on Calculation class and select <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Generate&period;&period;&gt&semi;Tests&period;&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1452" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;01&sol;Screenshot-2020-01-21-15&period;02&period;20&period;png" alt&equals;"Screenshot 2020-01-21 15&period;02&period;20" width&equals;"549" height&equals;"313" &sol;><&sol;p>&NewLine;<p>4 &period;A new <span style&equals;"color&colon; &num;008000&semi;"><strong>Create Test<&sol;strong><&sol;span> window will appear as shown below&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1454" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;01&sol;Screenshot-2020-01-21-15&period;03&period;43&period;png" alt&equals;"Screenshot 2020-01-21 15&period;03&period;43" width&equals;"509" height&equals;"536" &sol;><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p>5 &period;In above window provide Test class name and check all the methods which you want to use in your class and then click <strong><span style&equals;"color&colon; &num;008000&semi;">OK<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>6 &period;Now choose the destination directory ie&period; <strong><span style&equals;"color&colon; &num;008000&semi;">androidTest<&sol;span><&sol;strong> or <strong><span style&equals;"color&colon; &num;008000&semi;">test<&sol;span><&sol;strong> in which you will add the above-defined class and then click <span style&equals;"color&colon; &num;008000&semi;"><strong>OK<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1455" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;01&sol;Screenshot-2020-01-21-15&period;05&period;03&period;png" alt&equals;"Screenshot 2020-01-21 15&period;05&period;03" width&equals;"523" height&equals;"546" &sol;><&sol;p>&NewLine;<p>After creating the <span style&equals;"color&colon; &num;008000&semi;"><strong>TestClass<&sol;strong><&sol;span> you are ready to start writing your test cases&period;<&sol;p>&NewLine;<p>7 &period;<span style&equals;"color&colon; &num;000080&semi;"><strong>CalculationUnitTest<&sol;strong><&sol;span> consist of unit test cases for each operation defined with <strong><span style&equals;"color&colon; &num;008000&semi;">&commat;Test<&sol;span><&sol;strong> notation&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">CalculationUnitTest&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;calculatorapp&semi;&NewLine;&NewLine;import org&period;junit&period;Test&semi;&NewLine;&NewLine;import static org&period;junit&period;Assert&period;&ast;&semi;&NewLine;&NewLine;&NewLine;public class CalculationUnitTest &lbrace;&NewLine;&NewLine; &commat;Test&NewLine; public void addition&lowbar;isCorrect&lpar;&rpar; &lbrace;&NewLine; assertEquals&lpar;5&comma; new Calculation&lpar;&rpar;&period;addTwoNumbers&lpar;2&comma;3&rpar;&comma;0&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;Test&NewLine; public void subtraction&lowbar;isCorrect&lpar;&rpar; &lbrace;&NewLine; assertEquals&lpar;5&comma; new Calculation&lpar;&rpar;&period;subtractTwoNumbers&lpar;8&comma;3&rpar;&comma;0&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Test&NewLine; public void multiplication&lowbar;isCorrect&lpar;&rpar; &lbrace;&NewLine; assertEquals&lpar;24&comma; new Calculation&lpar;&rpar;&period;multiplyTwoNumbers&lpar;8&comma;3&rpar;&comma;0&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Test&NewLine; public void division&lowbar;isCorrect&lpar;&rpar; &lbrace;&NewLine; assertEquals&lpar;3&comma; new Calculation&lpar;&rpar;&period;divideTwoNumbers&lpar;9&comma;3&rpar;&comma;0&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Test&NewLine; public void percentage&lowbar;isCorrect&lpar;&rpar; &lbrace;&NewLine; assertEquals&lpar;0&period;03&comma; new Calculation&lpar;&rpar;&period;calculatePercentage&lpar;3&rpar;&comma;0&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;&NewLine;<&sol;pre>&NewLine;<p>Each test consist of <strong><span style&equals;"color&colon; &num;008000&semi;">assertEquals&lpar;&rpar;<&sol;span><&sol;strong> which takes three inputs&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>expected value&period;<&sol;li>&NewLine;<li>actual value to check against expected value&period;<&sol;li>&NewLine;<li>delta ie&period; the maximum delta between expected and<br &sol;>&NewLine;actual value for which both numbers are still<br &sol;>&NewLine;considered equal&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>8 &period;To run all unit test cases right-click on <strong><span style&equals;"color&colon; &num;008000&semi;">CalculationUnitTest<&sol;span><&sol;strong> class and then select <strong><span style&equals;"color&colon; &num;0000ff&semi;">Run &&num;8216&semi;CalculationUnitTest&&num;8217&semi;<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1457" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;01&sol;Screenshot-2020-01-21-17&period;40&period;34&period;png" alt&equals;"Screenshot 2020-01-21 17&period;40&period;34" width&equals;"1158" height&equals;"414" &sol;><&sol;p>&NewLine;<p>9 &period;<strong><span style&equals;"color&colon; &num;000080&semi;">CalculationTest<&sol;span><&sol;strong> consist of UI test cases for all the operations&period;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;RunWith<&sol;span> <&sol;strong>&colon; specifies the behavior for our TestClass&period;To create an instrumented JUnit 4 test class&comma; add the <strong><span style&equals;"color&colon; &num;008000&semi;">&commat;RunWith&lpar;AndroidJUnit4&period;class&rpar;<&sol;span> <&sol;strong>annotation at the beginning of your test class definition&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;Rule<&sol;span> <&sol;strong>&colon; Provides functional testing of a single activity&period; UI can access the activity using the rule and thereby can access the resources etc&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&commat;Test <&sol;strong><&sol;span>&colon; The &commat;Test annotation tells <span style&equals;"color&colon; &num;008000&semi;"><strong>JUnit<&sol;strong><&sol;span> that the <span style&equals;"color&colon; &num;008000&semi;"><strong>public void<&sol;strong><&sol;span> method to which it is attached can be run as a test case&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">CalculationTest&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;calculatorapp&semi;&NewLine;&NewLine;import org&period;junit&period;Rule&semi;&NewLine;import org&period;junit&period;Test&semi;&NewLine;import org&period;junit&period;runner&period;RunWith&semi;&NewLine;&NewLine;import androidx&period;test&period;ext&period;junit&period;runners&period;AndroidJUnit4&semi;&NewLine;import androidx&period;test&period;rule&period;ActivityTestRule&semi;&NewLine;&NewLine;import static androidx&period;test&period;espresso&period;Espresso&period;onView&semi;&NewLine;import static androidx&period;test&period;espresso&period;action&period;ViewActions&period;click&semi;&NewLine;import static androidx&period;test&period;espresso&period;assertion&period;ViewAssertions&period;matches&semi;&NewLine;import static androidx&period;test&period;espresso&period;matcher&period;ViewMatchers&period;withId&semi;&NewLine;import static androidx&period;test&period;espresso&period;matcher&period;ViewMatchers&period;withText&semi;&NewLine;&NewLine;&NewLine;&commat;RunWith&lpar;AndroidJUnit4&period;class&rpar;&NewLine;public class CalculationTest &lbrace;&NewLine;&NewLine; &commat;Rule&NewLine; public ActivityTestRule&lt&semi;MainActivity&gt&semi; activityScenarioRule&NewLine; &equals; new ActivityTestRule&lt&semi;&gt&semi;&lpar;MainActivity&period;class&rpar;&semi;&NewLine;&NewLine; &commat;Test&NewLine; public void addTwoNumbers&lpar;&rpar; &lbrace;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;two&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;plus&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;three&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;equalsto&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;edt&lowbar;calculation&rpar;&rpar;&period;check&lpar;matches&lpar;withText&lpar;"5"&rpar;&rpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Test&NewLine; public void subtractTwoNumbers&lpar;&rpar; &lbrace;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;eight&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;minus&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;three&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;equalsto&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;edt&lowbar;calculation&rpar;&rpar;&period;check&lpar;matches&lpar;withText&lpar;"5&period;0"&rpar;&rpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Test&NewLine; public void multiplyTwoNumbers&lpar;&rpar; &lbrace;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;eight&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;multiply&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;three&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;equalsto&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;edt&lowbar;calculation&rpar;&rpar;&period;check&lpar;matches&lpar;withText&lpar;"24&period;0"&rpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Test&NewLine; public void divideTwoNumbers&lpar;&rpar; &lbrace;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;nine&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;divide&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;three&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;equalsto&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;edt&lowbar;calculation&rpar;&rpar;&period;check&lpar;matches&lpar;withText&lpar;"3&period;0"&rpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Test&NewLine; public void calculatePercentage&lpar;&rpar; &lbrace;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;three&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;btn&lowbar;percentage&rpar;&rpar;&period;perform&lpar;click&lpar;&rpar;&rpar;&semi;&NewLine; onView&lpar;withId&lpar;R&period;id&period;edt&lowbar;calculation&rpar;&rpar;&period;check&lpar;matches&lpar;withText&lpar;"0&period;03"&rpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>onView&lpar;&rpar;<&sol;strong><&sol;span> is used to select a view for testing&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>withId&lpar;&rpar;<&sol;strong><&sol;span> is used to locate the UI element&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>perform&lpar;click&lpar;&rpar;&rpar;<&sol;strong><&sol;span> is used to perform click operation&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>check&lpar;matches&lpar;withText&lpar;&rpar;&rpar;&rpar;<&sol;strong><&sol;span> is used to check whether the element is displayed with particular text or not&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>10 &period;To run all UI test cases right-click on <strong><span style&equals;"color&colon; &num;008000&semi;">CalculationTest<&sol;span><&sol;strong> class and then select <strong><span style&equals;"color&colon; &num;0000ff&semi;">Run &&num;8216&semi;CalculationTest&&num;8217&semi;<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1458" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;01&sol;calculatorApp&lowbar;UI&lowbar;testing&period;gif" alt&equals;"calculatorApp&lowbar;UI&lowbar;testing" width&equals;"394" height&equals;"700" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version