Uluvavanya njani usetyenziso lwakho lwe-Android usebenzisa i-Espresso

Ngu-Michael Burton

Njengomphuhlisi, sele uyazi ukubaluleka kokubhala iimvavanyo zekhowudi yakho. Oku kuyinyani kuphuhliso lwe-Android njengoko kunjalo kulo naluphi na olunye uhlobo lophuhliso. Ngethamsanqa, i-Android yenza kube lula ukongeza iimvavanyo kwi-app yakho.



Olu lwazi lulandelayo luya kukusa kwiziseko zokongeza iimvavanyo kwi-app yakho ye-Android usebenzisa i-Espresso. I-Espresso yithala leencwadi elithumela nge-Android SDK ukwenza uvavanyo lomdibaniso womsebenzisi we-Android lube lula.



Qiniseka nje ukuba usetyenziso lwakho lisebenzisa i-gradle 2.1.1 okanye kamva ngezixhobo ze-Android zokwakha 21.1.2 okanye kamva.

Ukongeza uvavanyo lwe-Espresso kwi-app yakho, kuya kufuneka:



ikhuselekile kwi-mucinex ngexesha lokukhulelwa
  1. Guqula isikripthi sakho sokwakha

  2. Bhala uvavanyo

  3. Qalisa uvavanyo



Ungayiguqula njani ifayile yakho yokwakha

Vula ifayile ye-bubungakananiifayile yeapp yakho. Ukuba iapp yakho ineemodyuli ezininzi, uya kufuna ukuyivulaukwakhaifayile kwimodyuli yenkqubo yakho, hayi kwimodyuli yengcambu. Ke ngenxa yeNkqubo yokuThula ngokuSebenza, uya kuvulaSilentModeToggle / build.gradle.

Yongeza ukuxhomekeka okulandelayo kwicandelo lokuxhomekeka lakhoukwakhaifayile:

dependencies { …      //       Test       dependencies       androidTestCompile           '   com.android.support.test.espresso:espresso-core:2.0   '  }

Oku kuxelela Android Studio ukuba ibandakanye ithala leencwadi le-Espresso kuxhomekeko lwakho lovavanyo. Ithala leencwadi le-Espresso aliyi kwakhiwa kwi-apk oyithumelayo, kodwa iya kubandakanywa kwi-apk yakho yovavanyo ukuze iimvavanyo zakho zikwazi ukuyisebenzisa.

Okulandelayo, yongeza oku kulandelayookungagqibekangaConfig:

defaultConfig { …      //       The       test       runner       to       use       when       running       the       tests.       testInstrumentationRunner       '   android.support.test.runner.AndroidJUnitRunner   '  }

Oku kuxelela i-Android ukuba yeyiphi isixhobo oza kuyisebenzisa ukwenza iimvavanyo zakho. Awudingi ukwazi okuninzi ngesiXhobo sezixhobo, ngaphandle kokuba le yeyona nto kufuneka uyisebenzise nanini na ufuna ukuqhuba iimvavanyo ze-Espresso.

i-hydrocodone-acetaminophen 5-325

Yiyo yonke into ekufuneka uyenzile ukongeza inkxaso ye-Espresso. Okulandelayo, kuya kufuneka ubhale olona vavanyo.

Ulujonga njani uvavanyo

Ukongeza uvavanyo kwi-Android, kuya kufuneka uqiniseke ukuba uyibeka kwindawo efanelekileyo. Vula eyakhosrcisikhombisi kwi Android Studio, kwaye wenze isikhombisi esitsha esinegamaAndroidTestukuba ayisekho. Ngaphakathi kwesikhombisi, yenza isikhombisi esinegamaJava.

Ngaphakathi kulawulo olwenzileyo nje, yenza ifayile entsha enegamaCima iModeToggleEspressoTest.java. Ungayibiza ngegama nantoni na oyifunayo, qiniseka ukuba iyaphelaUvavanyo. Java.

Beka oku kulandelayo kwifayile yakho yovavanyo:

public class SilentModeToggleEspressoTest extends ActivityInstrumentationTestCase2 { public SilentModeToggleEspressoTest() { super(MainActivity.class); } }

Oku kudala iklasi entsha enegamaCima iModeToggleEspressoTest. IyaqhubekaUmsebenziInstationationTestCase2, oya kuthi uyisebenzise ukubhala uninzi lweemvavanyo zakho.UmsebenziInstationationTestCase2yiparameter kuhlobo lomsebenzi ofuna ukuwuvavanya. Kwi-SilentModeToggle app, owona msebenzi uphambili ubizwa, ngokufanelekileyo ngokwaneleyo,Umsebenzi ophambili.

Udidi lwakho lovavanyo kufuneka lube nomakhi osilelayo, kwaye kulomakhi osilelayo uya kubiza umakhi wodidi oluphezulu kunye nodidi lomsebenzi ovavanyiweyo. Into oyibhalileyo ukuza kuthi ga ngoku iya kukhutshelwa-ikhutshelweyo malunga nalo lonke uvavanyo lwe-Android oke ubhale.

Ngaphambi kokuba ubhale uvavanyo lwakho lokwenyani, kuyakufuneka usete. Ukwenza oko, yongeza le ndlela ilandelayo kwiklasi yakho yovavanyo:

@Override public void setUp() throws Exception { super.setUp(); getActivity(); }

Ukuvavanya umsebenzi usebenzisa i-Espresso, kufuneka uqale wenze umsebenzi ovavanyiweyo. Oku kwenziwa ngokufowunaUmsebenzi (), Eyila kwaye ikwazise umsebenzi ukuba awukho. Ukwenza oku ngasentla kwimisela()indlela yeklasi, eqhutywa phambi kovavanyo lwakho ngalunye.

Ngoku, ekugqibeleni, ungabhala uvavanyo lwakho. Yongeza le ndlela elandelayo kwiklasi yakho:

public void testPhoneIconIsDisplayed() { // When the phone_icon view is available, // check that it is displayed. onView(withId(R.id.phone_icon)) .check(matches(isDisplayed())); }

Uya kudinga ukungenisaJonga,kunyeId,khangela,imatshisi, kunyeIsDisplayediindlela. Isitudiyo se-Android sibonelela ukukunceda ngokungenisa izinto eziyimfuneko.

Uvavanyo lwe-Espresso lwenzelwe ukuba kube lula ukufunda. Njengoko ubona apha, olu vavanyo lulinde ukujonga kunye ne-IDRid.phone_iconiyafumaneka, kwaye xa ikhona, iyajonga ukuba umbono uyaboniswa na.

Inyathelo elilandelayo kukuqhuba uvavanyo.

Iziphumo ebezingalindelekanga zyrtec ixesha elide

Uqhuba njani uvavanyo

Okokuqala, qalisa i-emulator okanye uqhagamshele ifowuni yakho kwikhompyuter yakho nge-USB.

Xa isixhobo sakho sixhunyiwe kwaye sisebenza, cofa ekunene kwiJavaulawulo phantsiAndroidTest, kwaye emva koko ukhethe ukuqhuba lonke uvavanyo (kunye ne icon ye-Android):

umfanekiso0.png

Ukuba uvavanyo luqhuba ngempumelelo, kuya kufuneka ubone into elandelayo:

i-prednisone iyeza lokubulala iintsholongwane

umfanekiso1.png

Kutheni le nto uvavanyo lubalulekile?

Kutheni uhamba ngawo wonke lo mzamo ukongeza nje uvavanyo olulula? Unokucinga ukuba kubonakala ngathi akufuneki kuvavanyo olusebenzayo.

Inkqubo yokuThula kweNkqubo yokuTshintsha ilula ngokwaneleyo ukuba mhlawumbi umdwelisi wenkqubo owonqenayo unokuthethelela ukutsiba iimvavanyo. Ingayiyo.

Ukuba wongeza iimvavanyo ezimbalwa zokongeza, ukususa, kunye nokuhlela imisebenzi kule app, unoxolo lwengqondo lokwazi ukuba xa usenza utshintsho, iimvavanyo zakho ezizenzekelayo ziya kubamba naziphi na iimpazamo ezibalulekileyo ozazisayo.

Ngaba uyafuna nyani ukuvavanya konke ukusebenza kweapp yakho kwiifowuni, iitafile, iiwotshi, kunye neeTV, emva koko wenze konke kwakhona kwizixhobo ezindala ukuvavanya ukuhambelana ngasemva? Ukongeza iimvavanyo kunokukhathalela obo bunzima kuwe.

Malunga neemvavanyo ze-Espresso

Uvavanyo lwe-Espresso lubhalwa kusetyenziswa le fomathi ilandelayo:

onView( Matcher ) .perform( ViewAction ) .check( ViewAssertion )

Olu phepha lulandelayo lokukopela, ngoncedo lweprojekthi ye-Espresso 2.0, sisixhobo esiluncedo ekubhaleni iimvavanyo ze-Espresso. Uluhlu luluhlu lwes syntax ngokubanzi yovavanyo lwe-Espresso, kwaye ikwadwelisa ezona ntlobo zixhaphakileyo zabalingisi:Jonga iActionkwayeJonga ukuHlolwa.

umfanekiso2.png

Indlela ekhawulezayo yokubhala iimvavanyo ze-Espresso kukusebenzisa ithuba Android Studio's Autocomplete functionality. Ukuba uyazi ukuba uninzi lokulinganisa lukuJonga abaPhawuliiklasi, uninzi lwezenzo zokujonga zikwiJongaIzenzo, kwaye uninzi lwezimvo zokujonga zikhonaJonga iiAssertions, emva koko unokusebenzisa ukugqibezela ukufumana umlinganiso ofanelekileyo:

umfanekiso3.png

Masidibanise enye

Ke ngaloo nto engqondweni, masongeze olunye uvavanyo. Yongeza imigca ebhalwe ngqindilili kudidi lwakho lovavanyo:

public class SilentModeToggleEspressoTest extends ActivityInstrumentationTestCase2 {      AudioManager       audioManager;  … @Override public void setUp() throws Exception { super.setUp();      Activity       activity       =       getActivity();           //       Retrieve       an       AudioManager       from       the       activity       audioManager       =       (AudioManager)       activity.getSystemService(Context.AUDIO_SERVICE);       //       Make       sure       the       ringer       mode       is       reset       to       normal       audioManager.setRingerMode(       AudioManager.RINGER_MODE_NORMAL);  } …      public       void       testCanToggleIcon()       {       //       When       the       phone_icon       view       is       available,       click       it       onView(withId(R.id.phone_icon)).perform(click());       //       Then       assert       that       the       phone       is       now       in       silent       mode.       assertTrue(RingerHelper.isPhoneSilent(audioManager));       }  }

Olu vavanyo lufumana iqhosha lokutshintsha kwaye ucofe kulo, emva koko uqinisekise ukuba inkqubo ethe cwaka yeapp ivuliwe. Inkqubo ye-misela()ikhowudi iqinisekisa ukuba imo ye-ringer isetwa njengesiqhelo ngaphambi kokuba uvavanyo luqhubeke. Ngoku, ngalo lonke ixesha usenza utshintsho kwi-app yakho, kufuneka usebenze nje kwezi meko zovavanyo ukuze ube nokuzithemba ukuba awophulanga nantoni na ebalulekileyo.

Ukuba uneserver yokudibanisa eqhubekayo, konke okufuneka ukwenze kukuqhubauqhagamsheloCheckekujolise kuko kuqhubekile ukuqhuba amatyala akho ovavanyo ngayo yonke into oyenzayo. Ukuba awunaso isiseko sokudityaniswa okuqhubekayo okwangoku, mhlawumbi ngoku lixesha elifanelekileyo lokujonga kwenye! Phuma travis-ci.org ngokhetho olulungileyo.

Ukuvavanya ii -apps kwi-Android sisihloko esinzulu nesibandakanyekayo, kodwa ngoku kuya kufuneka ube nayo yonke into oyifunayo ukuze uqalise ukuvavanya iiapps zakho!

Ukuba ungathanda ukuqhubela phambili, unokuba nomdla kwezinye zezixhobo zilandelayo:

Ipilisi emhlophe eyi-m366
  • Sebenzisa Ipuni ukuvavanya kwizixhobo ezininzi.

  • Zama Ifolokhwe ukuqhuba iimvavanyo ngokukhawuleza kwizixhobo ezininzi.

  • Zama Ndiyabulela okanye Jenkins yomncedisi womdibaniso oqhubekayo.

  • Bukela intshayelelo kaMichael Bailey kuvavanyo lwe-Android kwi Youtube .