Professional Documents
Culture Documents
1.Preface
2.Requirements
2.1JavaSDK
2.2AndroidSDK
2.3AndroidVirtualDevices
2.4PhysicalAndroidDevices
3.PreparingYourDevice
4.Selendroid
4.1WhatisSelendroid?
4.2StartingSelendroid
4.3WritingaSimpleTestforSelendroid
4.4RunningaTest
4.5StartingDevicesAtRuntime
4.5.1SelectingAVDsbyProperties
4.5.2SelectingPhysicalDevicesByProperties
4.5.3PerformingaTestOnMultipleEmulators
4.6SelendroidandSiteGenesis
4.8Advantages
5.Appium
5.1WhatisAppium?
5.2InstallingtheAppiumServer
5.3CreatingYourFirstAppiumTestCase
5.3.1SetupofaSimpleAppiumProject
5.3.2UsingAppiumandMaven
5.3.3WritingYourFirstAppiumTestCase
5.4TestExecution
5.5Pitfalls
5.6CreatingAnAppiumTestCaseForSiteGenesis
6.LocalMobileEmulators
6.1AndroidVirtualDevice(AVD)Manager
6.2Genymotion
6.3OtherAlternatives
7.MobileDeviceClouds
7.1AWSDeviceFarm
7.2Keynote
7.3Xamarin
7.4Saucelabs
7.5BrowserStack
7.6Perfectomobile
8.SiteGenesisCommunityTestsuiteWrappert
8.1BrowserStackStandalone
8.2AppiumStandalone
8.3AppiumWrapper
8.4ChromeWebDriverWrapper
9.KnownProblems
9.1TroubleCreatingScreenshotWithAppium
9.2ClickingOnElementsDoesNotWork
9.3AppiumBuildpathOrder
9.4WindowsDriver
10.Sources
1. Preface
Testingnativeappsandthemobilewebonsmartphonesbecomesmoreandmoreimportantthesedays.
Togetridofallthedifferentplatformsandapplicationtypeswhichexistsitishighlyrecommendedtousea
testautomationframeworklike
Selendroid1 Appium2.Bothframeworksareavailableunderthe
or Apache
LicenseVersion2.0 .Thesourcecodecanbeobtainedfrom GitHub.
ThefollowingtextshallguidethereaderthroughtheprocessofinstallingSelendroid/Appiumwithall
dependenciesunderalinuxenvironmentandfinallycreatesometestcaseswiththe XltScriptDeveloper
.
2. Requirements
BeforeyoucanstartwritingmobiletestcaseswithSelendroidorAppiumitisnecessarytoinstallcertain
dependencies.Firstandforemostyouneedtohavesome SoftwareDevelopmentKits(SDK), whichallows
youtowritetestsandexecutesthem.Additionallyyouneedtoprovidesometestdevices.Thesecanbe
eithervirtualdevicesorphysicalhardwaredevices.Makesureyoufulfillallthoserequirementsbelow
beforeyouproceed.
JavaSDK3
A (v.1.7andabove)needstobeinstalledandconfigured.Makesurethattheenvironment
JAVA_HOME
variable ispointingtotherightdirectory,i.e.,donotletitpointtothe
JavaRuntime
Environment only.Ifyouneedhelpinstallingjavaconsulttheoracleonlinehelpat
https://www.java.com/en/download/help/download_options.xml ,whichprovidesfurtherinformation.
1
http://selendroid.io/
2
http://appium.io/
3
http://www.oracle.com/technetwork/java/javase/downloads/index.html
1
2.2 Android SDK
AndroidSDK4
ToruntestsagainstAndroiddevicesrequiresyoutoinstallthe .Followthestepsbelowto
setupyourAndroidenvironment:
2. extractthecontentofthedownloadedarchivetoaanylocationofyourchoosingtherootofyour
userhomedirectoryisarecommendedplace
3. addANDROID_HOME toyour
PATH
exportANDROID_HOME=/install_dir/androidsdklinux
exportPATH=${PATH}:$ANDROID_HOME/tools
exportPATH=${PATH}:$ANDROID_HOME/platformtools
5. inthefollowingwindowyoucanselectwhichplatformsandtoolsyouwanttoinstallstartby
selectingtheTools
checkbox,todownloadthemall
6. selectacheckboxbeneathanAndroidVersionmakesurethatthetestautomationframeworkyou
wanttousesupportsthisAPI,forexampleSelendroidsupportsAPI1019
7. selectthecheckbox AndroidSupportLibrary
fromtheExtrasfolder
8. clickon
Installpackages
9. acceptthelicenseandclickonInstall
Thecheckedpackageswillbedownloaded,whichmaytakeawhiledependingonyourconnection.After
thedownloadiscompletedyoucanstartaddingvirtualdevicestoyoursystemoruserealphysical
hardwaretorunyourtests.
4
http://developer.android.com/sdk/index.html
2
2.3 Android Virtual Devices
Insteadofrunningmobiletestsonrealhardwareyoucanalsouseanemulator.Emulatorsofferssome
advantagescomparedtophysicaldevices.Youcancreateasmanyemulatorsasyoulike,eachofthem
emulatesadifferentAndroiddevicewithdifferentsettings.AnemulatorforAndroidisoftencalled Android
VirtualDevice(AVD) .IfyouhaveinstalledtheAndroidSDKwithittoolsyoucancreateyourownAVDinno
time.
TocreateanewAVDrunthecommand android inaconsoletostarttheAndroidSDKManager.Nextgo
tothetoolbarandclickTools>ManageAVDs .TheAndroidVirtualDevice(AVD)Manager shouldopenin
anotherwindow.HereyoucancreatedifferentAVDsandevenspecifyyourownDeviceDefinitions.This
allowsyoutocreatevirtualdeviceswithhardwarespecsthatdoesnotexistinreallife.Whencreatingan
AVDpayattentiontoselectasupportedAPIversionunder Target,tomakesureyourtestautomation
frameworkwillrecognizethedevice.AlldevicesyoucreatewillbedisplayedintheAVDManager.Ifyou
ratherwanttoworkonconsolelevelyoucanalsousethecommand androidlistavd togetalistofall
installedemulators.
IMPORTANT: doNOTactivatetheUseHostGPUoptionitmightcausesometroublewhileusingthe
frameworks.
Ifyoudontwanttorelyonemulatorsyoucanalsouserealdevices.Usingphysicalhardwarerequiresyou
toenableUSBdebugging onyourphoneinthe
DeveloperOptions dialog.Ifyoudontknowwherethis
optionsarefollowthislink
http://developer.android.com/tools/device.html#settingup.
Connectthedevicetoyourcomputerthroughusb.Itmaybenecessary(dependingonyourAndroid
version)toacceptthefingerprintforthecomputeryouconnectto.Inthiscaseanewdialogpopsupasking
3
youwhethertotrustthecomputerordeclinetheconnection.JustclickOkandyouarefine.Alsomake
surethatnoscreenlockisactivated.
Youcanverifythatyourdeviceiscorrectlyattachedtoyourcomputertypingthecommand adbdevices
inaconsole.Theoutputpresentsalistofallexternaldevicesconnectedtothecomputer.
AfterinstallingandconfiguringtheJavaandAndroidSDK,youarereadytostartwritingtestswith
SelendroidorAppium.Asalreadymentionedyoucandecidewhetheryouwanttoexecuteyourtestson
realhardwareoranemulator.Doyouwanttoseehowyourtestcasebehavesonarealdevicethenjust
followthestepsabove .
Ordoyouratherpreferanemulator?Allyouhavetodoisitstartemulationby
executingthecommand emulatoravdNexus442 (replaceNexus442withtheactualnameofyour
previouslycreateddevice).AnewwindowshouldappearwhichactslikeanAndroiddevice.
IMPORTANT: startallyourAVDsatleastonceanddeactivatetheAndroidscreenlock.Otherwisethere
couldbeproblemslaterwhiletesting.
4. Selendroid
Selendroidisatestautomationframework,whichcanbeusedtotest nativeandhybrid
apps,andthe
mobileweb aswell(mobilewebreferstoaccessingtheinternetthroughamobilebrowser).Theframework
AndroidInstrumentationFramework5
reliesonthe andisthereforelimitedtotestandroiddevicesonly.
Selendroidconsistsoffourmajorcomponents:
1. SelendroidClient javaclientlibrarybasedonSeleniumWebDrivers
2. SelendroidServer whichisrunningontheandroiddevice
3. AndroidDriverApp specialapptotestthemobileweb(notinthepicturebelow)
4. SelendroidStandalone proxybetweenSelendroidClient
andSelendroidServer
5
http://developer.android.com/tools/testing/testing_android.html
5
CommunicationbetweentheWebDriverClientandSelendroidStandaloneisbasedonthe JSONwire
protocol
6
.ThemainfunctionoftheSelendroidStandaloneistohandlerequestsfromclientsandforward
themtothedevices.Thisalsoincludestransferringappsfromthecomputertotheandroiddevice.Itdoes
notmatterifanemulatororarealdeviceistested.
6
https://code.google.com/p/selenium/wiki/JsonWireProtocol
6
4.2 Starting Selendroid
RunningSelendroidisstraightforward:justdownloadthe SelendroidStandalone.jar
andyouaregoodto
go.Navigatetothefolderwiththejarfileandrunfollowingcommand:
javajarselendroidstandalone0.15.0withdependencies.jar
Executingthecommandwillstartahttpserverlisteningonport4444.Thisserverwillscancontinuously
yourcomputerformobiledeviceconnections.ThereforeitchecksallyourcreatedAVDswhilealso
watchingfornewpluggedindevices.
Everyactiontheserverperforms(likedetectingnewdevices,runningtestsetc.)willbeloggedtothe
consoleandtodisk.Youcanalsotakealookat http://localhost:4444/wd/hub/status
toseeseeall
connecteddevicesandsomeotherimportantinformation.Watchout:ifyourdeviceisnotlistedunder
supportedDevicesitmaybepossiblethatitusesanotsupportedAndroidAPIversion.Inthatcaseyou
cannotuseitfortestingpurpose.
IfyouprefertorunaSelendroidStandalonefromyourcodeallyouhavetodoiscreateanewdefault
SelendroidConfiguration andlaunchaserverwithit.Takealookatthecodesnippetbelow,which
showsyouhowitworks:
AsyoucanseeitisreallysimpletostartSelendroidStandalonefromyourcode.Ofcourseyoucanchange
theserverconfigurationtoyourneeds,likespecifyingonwhichporttheislistening,theloglevelorsome
generalbehaviour,e.g.,emulatorandsessiontimeout.
YoucanwriteSelendroidtestcasesineveryprogramminglanguageforwhichSeleniumoffersclient
bindings.ThefollowingtestcodeiswritteninJavaanddemonstratesthesimpleusageofSelendroid.First,
createanewJavaProjectintheIDEofyourchoice.AddanewclasswiththenameSelendroidTest tothe
projectwhichcontainsthefollowingcode:
8
Theclasscontainsthreemethodsthatareexecutedbefore,duringandafterthetest.In begin() anew
webdriveriscreatedwiththecapabilitytocommunicatewithAndroiddevices.Themethod test()
providesthecodewhichwillbetested.Inthiscaseitopensamobilewebbrowser,visitthewebsite
http://m.ebay.de,getahandletothesearchfieldandsearchesforthetextNexus5.Afterthepayloadof
end()
thetest
iscalledandthebrowserisclosed.
BeforeyoucanrunthecodewithSelendroidyouneedtoadd selendroidclient0.15.0.jar
and
selendroidstandalone0.15.0withdependencies.jar toyourprojectbuildpath.
Torunthecodeaboveyouneedtoa.)startanAVDasexplainedin 3.Preparingyourdevice
orb.)connectyoursmartphonetothecomputerasexplainedin 2.4PhysicalAndroidDevices .
Takecare:ifyouhaverealhardwareconnectedandanAVDrunning,Selendroidwillpickuponeobject
randomlytoperformthetest.MakesureSelendroidStandaloneisstarted(asdescribedin 4.2Starting
Selendroid)andrecognizesyourdevice.Allthatislefttodoistocompileandrunthecodeandwatchyour
deviceperformingthetest.
Testingyourcodeinanemulatoraspresentedin 4.4Runningatest
hasonemajordrawback:youneedto
start/stopyourAVDmanuallybeforethetest,everytimeyouwanttouseanotherdevice.Imagineyouhave
atestcaseandaroundtendeviceswhichyouallwantedtotest.Itwouldbeareallytediousjobtotype
always emulatoravddevice#xx justtostartanewdeviceandafterwardsexecutetheactualtest.
Selendroidoffersaconvenientwaytosolvethisspecificproblemandsavesyoualotoftyping.Insteadof
startingemulatorsbyhandyoucanletSelendroidstart/stopthemforyoudynamically(donotconfuse
9
startinganAVDwithcreatingone!).Ifyouhavejustoneemulator/deviceoritdoesnotmatteronwhich
deviceyouareperformingatestyoucanstickwiththecodefrom 4.3WritingasimpletestforSelendroid.
ButifyouwanttotestcertaindevicesyouneedtosupplyadditionalinformationtoSelendroid.These
informationareprovidedthrough propertiesduringthecreationofthewebdriver.SelendroidStandalonewill
thentrytoselectadevicewhichmatchesalltherequirements.Takealookatthetablebelowtoseeallthe
propertiesyoucansetformobilewebtesting.
Therearealsosomepitfallsbyselectingdeviceswhichyouneedtotakecareof.Adevicemustsupport
ALLthespecifiedpropertiestoreturnamatch,i.e.,ifyourdevicehastheAndroidtargetAPI19butthe
propertyistellingSelendroidStandalonetolookfordeviceswithAPI18,nomatchwillbefoundandthe
testwillcrash.Ontheotherhand,ifyourpropertiesmatchvariousresults,Selendroidwillpickthefirstone
returned.
SadlythereisnowaytoselectanemulatorbynameorID.Onlyphysicaldevicescanbeaccessedbya
serialnumber.
Property Description
EMULATOR booleanvalue,indicatingwhethertousean
emulatororarealdevice
PLATFORM_VERSION theAndroidtargetAPIversion
LOCALE whichlocalizationshouldbeused
SCREEN_SIZE thedisplayresolution
MODEL specifythemodelofanemulator
API_TARGET_TYPE letyouspecifytheAPIlibraryneededonadevice
DISPLAY onlyforLinux:specifythedisplayonwhichthe
emulatorwindowshouldappear
PRE_SESSION_ADB_COMMANDS allowstheusertoprovidealistofadbcommands
(executedbeforethetest)
LettingSelendroidpickanemulatorforyourdesiredpropertiesisstraightforward.Theexamplebelow
showsyouhowtoselectadevicebyusingsomeofthepropertiesfromthetableabove.Allyouhavetodo
isslightlymodifythecodefrom
4.3WritingasimpletestforSelendroid begin()
inthe method:
ThissamplecodesnippetwillselectasingleNexus5emulator(ifavailable).Ifmultipledevicesarefound
thefirstreturnedtoSelendroidwillbeused.
10
BesideslettingyouspecifywhichAVDtouseyoucanalsotellSelendroidwhichrealdevice(s)shouldbe
tested.Togetalistofalldevicesconnectedtoyourcomputeryoucanrunthecommand adbdevices .
Theoutputmaylooklikethis:
Listofdevicesattached
00199148057dae device
0149bd8205670019 device
Everyentryhasauniqueserialnumberwhichidentifiesthedevice.TotellSelendroidwhichdeviceto
chooseyoucanusethefollowingcode:
NowthatyouknowhowtoselectdeviceswithSelendroiditshouldbenoproblemtowritemorecomplex
code,whichselectsdifferentdevicesconsecutivelyandruntestsonthem(seetheexamplebelow).
11
SiteGenesisCommunityTestSuite7allowsuserstowritetestcasesforDemandware8based
The
ecommercestores.EverytestcaseinSiteGenesisisasubclassof AbstractScriptTestCase .Ifyou
wanttoextendthetestsuitebyofferingthepossibilitytoperformmobiletestsyouneedawrapperclass.
Thisclassisresponsibleforstarting/shuttingdowntheSelendroidserverandthecreationofthewebdriver.
Runningamobiletestjustrequiresyoutoextendtestcasesfromthenewlycreatedwrapperclass.
MobileWrapper
Inthesampleabovetheclass actsasawrapperclassformobiletestcases.Apossible
implementationofthatclassmaylooklikethis:
7
https://github.com/Xceptance/SiteGenesisCommunityTestSuite
8
http://www.demandware.com/
12
13
Thepurposeofthisclassistostart/stopaSelendroidServerandcreate/deleteanAndroidDriver.Tosave
timeandresourcestheserveranddriverareonlycreated/deletedonce.Thecreationwilltakeplaceina
staticinitializationblockwhichgetsexecutedwhentheJavaClassloaderloadstheclass.Inthisblockwe
alsoaddashutdownhandlerthatwillbecalledwhenalltestsarefinishedandthe JavaVirtualMachine
(JVM) quits.
Becauseitisnecessarytotellthesuperclasswhichdrivershouldbeused,weneedtocall
setWebDriver() intheconstructorandpassthedriverforeachtestweliketorun.
Atthetimeofwritingselendroidhassomeissueswhichhaveanegativeimpactonperformingtestsand
mayalsocrashthem.Thefollowingproblemswerefoundbytheauthorduringwritingthisdocumentation:
Issue#1:ItisnotpossibletooverridecertainJavaScriptfunctionslikewindow.alertor
window.confirm
ThereasonforthatisthatSelendroidusesitownmessagingsystemwhichalreadyoverridesthose
functions.AsresulttheSelendroidServermaycrash.Selendroiddevsknowaboutthisissuebutitwill
probablynotbefixedsoon( https://github.com/selendroid/selendroid/issues/361 ).
ThisproblemespeciallyaffectstheXLTFrameworkmethod assumeOkOnAlertOrConfirm intheclass
WebDriverUtils .
Issue#2:Themethodclear()cannotbeused
TherearesomeproblemswhentheselendroidwebdriveriscreatedandtheSeleniumfunctionclear()is
called.Itisnotquiteclearhowandwhentheerrorhappens(maybeitdependsonacertainAndroid
version).Forfurtherreadingvisit https://github.com/appium/appium/issues/3186 or
https://github.com/selendroid/selendroid/issues/492 .
Issue#3:Thewebviewsgotsopenedbutthenclosedimmediately
IfyouwanttouseSelendroidinconjunctionwithSiteGenesisyouneedtopayattentiontotheorderofthe
buildpath.SiteGenesisusessomelibrarieswhicharealsorequiredbySelendroid.Thiscanleadtoversion
conflictsorotherunexpectedbehaviour.Forexample,takealookatthegraphicbelow:
14
ThegraphicshowsthebuildclasspathorderfortheauthorsSiteGenesisproject.Itusesthelibraries
requiredforSelendroidandlinkstothesourcesoftheXLTFramework(intheprojectxlt4.6mobile).Ifyou
changetheorderandputthefoldernamedxlt4.6.mobileabovetheentry
selendroidstandalone0.15.0withdependencies.jarthecreationofawebdriverfails.Thiskindoferroris
hardtodetectbecausetheconsolelogdoesnottellyouexactlywhattheproblemis.Inthisexampleallthe
errorlogsaysisthattherearesomeproblemspopulatingthewebdriverwithJSONObjects.
Soiftherearesomestrangeerrorsinyourprojecttrytochangethebuildorderfirst.
4.8 Advantages
Theframeworkoffersmanyadvantages,mostofthemarerelatedtothefunctionalitytotestapps.Togetan
overviewtakeaquickglimpseatthefollowinglist:
youcantestappswithoutmodifyingthem
supportofmultipledevices/simulatorssimultaneously
gesturesupport(touch,swipe,drag)
canhandlehotplughardware
15
supportsAndroidAPI10(2.3.3GingerBread)to19(4.4KitKat)
providessomebuiltinUIinspectortools
16
5. Appium
Appiumisanotheropensourcetestautomationframeworkfortestingthemobileweb,nativeandhybrid
apps.Theframeworkwasdesignedtosupportcrossplatformtests.Thatmeansausercanwritetestsfor
Android,iOSorevenFirefoxOSusingthesameAPI.ThisworksbecauseAppiumisusingdifferent
vendorprovidedframeworksfordifferentplatforms:
iOS:Apple's
UIAutomation
Android4.2+:Google's
UiAutomator
Android2.3+:Google's
Instrumentation
ThosevendorprovidedframeworksarewrappedintheWebDriverAPIsothattheuserisnotboundto
certainframeworkrestrictions.Forexample,iftheuserwantstouseUIAutomationheisforcedtowritehis
testsinJavaScript.Byencapsulatingtheframeworkstheuserwritehistestsineverylanguagewhichis
WebDrivercompatible.
ThewaythatAppiumworksisverysimilartoSelendroid.AclientsendscommandstoanAppiumServer,
whichtranslatesthesecommandsintovendorspecificinstructionsandforwardsthemtoanemulatororreal
device.Thedeviceexecutestheinstructionsandrepliestotheserverwiththeresultoftheperformed
actions.Theservertakestheresult,printitonscreenandsenditbacktotheoriginalclient.
OneimportantthingtomentionisthatAppiumonlyrunsforAndroidDevicesusingAPI17and
above.AlldevicesconnectedusingalowerAPIversionwillberunnedusingSelendroid.
BeforeyouinstallAppiummakesurethatyoufulfillallrequirementsfrom
2.Requirements
andprepared
yourAVDslikedescribedin
3.Preparingyourdevice .
Appiumcanbeobtainedas Node.js9
packagethroughthe Nodes.jspackagemanager(npm)10.Justenter
thefollowingcommandsinyourconsole:
9
https://nodejs.org/
10
https://www.npmjs.com/
17
npminstallgappium
npminstallwd
sudo
Itisimportantthatyoudonotuse toinstallorstartAppiumotherwiseAppiumwillcomplainthatit
cannotrun.Ifyouhavealreadyinstallednode.jsandarenotallowedtoinstall/startsomethingwithout
enteringsudoyouneedtouninstallnode.jsfirst.Afterwardsdownloadthenode.jsarchiveforyouroperating
systemfrom https://nodejs.org/download/,extractitandconfigureyourPATH.Nowyoushouldbeableto
install/startnodepackageswithoutsudo .
Aftertheinstallationyoucanusethecommand appiumdoctort overifythatAppiumsdependenciesare
installedcorrectly.
AtthetimeofwritingAppium1.4isthelatestversion.YoucancheckwhichAppiumversionisinstalledby
enteringappiumversion .
YouarealmostreadytocreateyourfirstAppiumTestCase.Allthatismissingnowistheclientcodefor
settingupanAppiumDriver.Dependingonwhetheryouwanttocreateasmallandsimpleprojectorrather
createaprojectwiththebuildmanagerMaven11youcancontinuereadingat 5.3.1SetupofaSimple
AppiumProject
or
5.3.2UsingAppiumandMaven .
WritinganAppiumApplicationwithoutabuildmanagerisstraightforward.AtfirstcreateanewJavaProject
letscallitAppium.DownloadthelatestversionofAppiumsClientJarwhichisavailableunderAppiums
Mavenrepository12 .AlsomakesuretodownloadtherecommendedSeleniumversionfortheAppiumclient.
InsteadofdownloadingSeleniumandallitsdependenciesmanuallyyoucanalsojustdownloadthewhole
package SeleniumStandaloneServer13.That.jarfilecontainsalladditionaldependencieswhichyouneed
toaddmanuallyotherwise.AddtheAppiumsClientJarandtheSeleniumServerJarfilestoyourbuild
path.Nowyourprojectissetupandyoucancontinuereading 5.3.3
.
11
https://maven.apache.org/
12
http://mvnrepository.com/artifact/io.appium/javaclient
13
http://seleniumrelease.storage.googleapis.com/2.46/seleniumserverstandalone2.46.0.jar
18
19
5.3.2 Using Appium and Maven
Insteadofcreatingasimplesingleprojectanddownloadingandaddingalldependenciesmanuallytothe
buildpathyoucanalsouseMavenassoftwareprojectmanager.Infactallsourcesandjarsareavailablein
theMavenrepositoryfromAppium http://mvnrepository.com/artifact/io.appium
.Allyouneedtodoisto
pom.xml
createafile thatcontainsalldependenciesrequiredbyAppium.
<
projectxmlns
="http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://maven.apache.org/POM/
4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd "
>
<
modelVersion >
4.0.0
</modelVersion >
<
groupId >
com.xceptance </groupId>
<
artifactId
>appium-test
</
artifactId
>
<
version>0.0.1-SNAPSHOT </version
>
<dependencies
>
<dependency >
<
groupId >
io.appium </ groupId >
<
artifactId
>java-client</artifactId>
<
version>3.0.0</
version >
</dependency >
<dependency >
<
groupId >
org.seleniumhq.selenium </groupId
>
<
artifactId
>selenium -java</ artifactId
>
<
version>2.46.0</version >
</dependency >
<dependency >
<
groupId >
com.google.code.gson </groupId>
<
artifactId
>gson</artifactId>
<
version>2.2.4</
version >
</dependency >
<dependency >
<
groupId >
junit</
groupId >
<
artifactId
>junit
</artifactId>
<
version>4.11</
version >
<
scope >
test
</scope>
</dependency >
<dependency >
<
groupId >
org.apache.httpcomponents </groupId
>
<
artifactId
>httpclient</artifactId>
<
version>4.3.3</
version >
</dependency >
<dependency >
<
groupId >
com.google.guava </groupId >
<
artifactId
>guava </
artifactId >
<
version>17.0</
version >
</dependency >
<dependency >
<
groupId >
cglib</
groupId >
<
artifactId
>cglib
</artifactId>
<
version>3.1</
version >
</dependency >
</dependencies
>
</
project
>
Ifyourprojectsetupiscompletedyoucanstarttowriteyourfirsttestcase.Itdoesnotmatterifyouwantto
useJUnit14
fortestingorjustwriteafewlinesofcodebecauseAppiumisdecoupledfromJUnitsince
version3.0.ThecodebelowisaquickanddirtyexampleofhowtouseAppiumtotestthemobileweb.
14
http://junit.org/
20
import
io.appium.java_client.android.AndroidDriver;
import
io.appium.java_client.remote.MobileBrowserType;
import
io.appium.java_client.remote.MobileCapabilityType;
import
io.appium.java_client.remote.MobilePlatform;
import
java.net.MalformedURLException;
import
java.net.URL;
import
org.openqa.selenium.By;
import
org.openqa.selenium.JavascriptExecutor;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.remote.DesiredCapabilities;
publicclass
Appium
{
public
static
void
main(String[]
args
)throws
MalformedURLException
{
DesiredCapabilities
caps=new DesiredCapabilities();
caps
= DesiredCapabilities.
android
();
caps
.setCapability(MobileCapabilityType.
DEVICE_NAME
,
"Nexus5");
caps
.setCapability(MobileCapabilityType.
PLATFORM_NAME, MobilePlatform.
ANDROID
);
caps
.setCapability(MobileCapabilityType.
BROWSER_NAME, MobileBrowserType.
BROWSER);
URL
appiumUrl
=new
URL(
"http://127.0.0.1:4723/wd/hub"
);
WebDriver driver
=new AndroidDriver<WebElement>(
appiumUrl
,
caps
);
driver
.get(
"http://m.ebay.de"
);
final
WebElement
element
=driver
.findElement(By.
id
(
"kw"
));
lement
e .sendKeys("Nexus 5"
);
element
.submit();
if
(
driver!=
null)
{
driver
.quit();
}
}
}
appium
BeforeyoucanstartyourtestyouneedtostarttheAppiumserverbytyping inyourconsole.The
Appiumserverisstartedandbeginstolisten:
NowyoucaneitherconnectyourphoneviaUSBtoyourcomputerORstartanAVD(forexample,typing
emulatoravdNexus442 willstarttheVirtualDevicewiththenameNexus442).Payattention:One
Appiumservercannothandlemultipledevicesatthesametime.Itispossibletotestagainstmultiple
devicesatthesametimebutthisrequiresrunningmultipleinstancesofAppiumservers(eachlisteningto
21
anotherport).ExecutingthefollowingtwolineswillstarttwoseparatedAppiumservers,eachonadifferent
port,usingthedevicesspecifiedbytheparameterpassedinwithinU.
appiump4727bp4728U"1af519b642187c"chromedriverport9516
appiump4725bp4731U"98531da52fc10e"chromedriverport9518
WaituntiltheAVDisstartedoryourconnectdevicegetsrecognized.Youcanverifythatyourdeviceis
adbdevices
readyfortestingbyentering
inyourconsole.Again,ifyouonlyhaveasingleAppiumserver
startedmakesurethatonlyonedeviceislistedasoutput.Whenanythingispreparedyoucanstartyour
testcaseandseehowAppiumexecutesthecommandsonyourphone.
22
5.5 Pitfalls
Appiumalsocontainssomepitfallswhichmaygiveyousomeheadache.Ifsomethingdoesnotworktakea
lookatthelistbelow.
Issue#1:Phonegetsunlockedbutnothinghappensafterwards
Appiummessage:
FailedtostartanAppiumsession,errwas:Error:Anoperationdidnotcompletebeforeitstimeout
expired.(Originalerror:timeout:Adbcommandtimedoutafter30seconds
YouhavestartedAppium,bootedyourAVD(orconnectedyourphone)andarereadytoproceedtesting.
Afterstartingyourtestyouseethatthedeviceisunlockedbutnothinghappensafterwards.Maybeyousee
awhitewindowwhichopensupbutclosesimmediatelyoryouseehowthebrowserisopenedbutthen
again,nothinghappens.Appiumtriestoperformfurthercommands(likeopeningawebsite)butitcannot
proceed.ThiserrorisprobablyrelatedtoawellknownproblemdiscussedintheAppiumdeveloperforum15.
AvoidingthisproblemrequiresyoutosetyourphonesettingScreenlocktoNone.Thatmaynotbethe
bestsolutionregardingthattheinitialproblemstillexists(Appiumcannothandlephoneunlockingproperly)
butitwillhelpyoutocontinuewithmobiletesting.
SiteGenesisCommunityTestSuite16(SGCTS)
Ifyouarefamiliarwiththe thenyoucanofcoursecreateyour
ownownmobiletestcaseswhichareexecutedbyAppiumandtestSiteGenesissites.IntegratingAppium
intotheSGCTSrequiresyoutowriteanadditionalwrapperthatcreatestheAppiumdriver.
UsuallySGCTStestcasesextends AbstractScriptTestCase.Sowhatyougonnaneedtodoiswritea
furtherwrapperthatextends AbstractScriptTestCase
.Thefollowingclasscouldbeusedformobile
testing:
15
https://github.com/appium/appium/issues/2908
16
https://github.com/Xceptance/SiteGenesisCommunityTestSuite
23
public
class
AppiumWrapper extendsAbstractScriptTestCase
{
privatestatic
final
AppiumDriver<WebElement> driver
;
static
{
Runtime.
getRuntime
().addShutdownHook
(
new
Thread(){
@Override
public
void
run()
{
AppiumWrapper.quitDriver
();
}
}
);
DesiredCapabilities
caps=new DesiredCapabilities();
caps= DesiredCapabilities.
android();
caps
.setCapability(MobileCapabilityType.
DEVICE_NAME ,
"Nexus5");
caps
.setCapability(MobileCapabilityType.
PLATFORM_NAME , MobilePlatform.
ANDROID
);
caps
.setCapability(MobileCapabilityType.
BROWSER_NAME , MobileBrowserType.
BROWSER);
URL
appiumUrl
=null
;
try
{
appiumUrl =new URL(
"http://127.0.0.1:4723/wd/hub"
);
}
catch
(MalformedURLException e
)
{
e
.printStackTrace();
}
driver
=new
AndroidDriver<>(
appiumUrl
,
caps
);
public
AppiumWrapper()
{
setWebDriver(
driver
);
}
publicstatic
void
quitDriver()
{
System. out
.println(
"Quit Driver"
);
if
(
driver!=
null)
{
driver.quit();
}
}
}
AllthatislefttodonowisletyourcurrenttestcaseextendsthenewlycreatedAppiumWrapper:
@ScriptName
(
"tests.checkout.phone.TCheckoutOrder_AsGuest" )
//public class TCheckoutOrder_AsGuest extends AbstractScriptTestCase
public class
TCheckoutOrder_AsGuest
extends AppiumWrapper
{
24
6. Local Mobile Emulators
Thefollowingsectionshowsyouwhichtoolsyoucanuseforcreatinglocalemulators.
Asalreadymentionedin
2.3AndroidVirtualDevicesyoucancreateemulatorsusingtheAndroidVirtual
DeviceManagerthatcomeswiththeAndroidSDK.Creatingdevicesisstraightforward:justclickonthe
buttonCreateandselectwhichdevice,APIversionandskinyouwanttouse.Keepinmindthatbefore
youcanactuallyuseanemulator,youneedtoinstalltherequiredAPIversionforthespecificdevice.
Pros Cons
itisfree notabletoprocessmultipletestcasesina
rowonthesamesimulator
createasmanydevicesasyouwant
youneedtodownload/setuptheAPIsby
containedintheAndroidSDK yourself
youcancreateyourowndevices nopreconfigureddevices
ifyoudonotwantcreateyourown
imaginaryhardware,youarelimitedtojust
afewstandarddevices(Nexus)
VMsmaytookahugeamountofspace
25
6.2 Genymotion
GenymotionisanAndroidemulatorthatisavailableat
https://www.genymotion.com/#!/ andletsyoucreate
severalandroiddevices.Asaprivateuseryoucanownacopyforpersonaluseonly.Thatkindofcopyhas
somelimitations(missingscreencast,nocloneandreset).Ifyoudonotwanttheselimitationsandare
willingtopay24.99permonthyoucanbuyabusinesslicenseforGenymotionwithallfeatures.
Pros Cons
canrunmultipletestcasesinarowonthe monthlyfee(businesslicense)
samedevicewithoutcrashing(incontrastto
AVD)
itisInsanelyfastandfullyfeatured17
(comparedtophysicaldevices)
bringsitownADBtools
ThereareafewmorealternativesyoucanusetorunanAndroidemulatorlocallybuttheyallseemtohave
certainlimitationsorflaws(operatingsystem,androidversion,stillinabetaversion),sotheyare
probablynotsuitablefortesting.Anyway,ifyouwanttochecksomeofthemouttakealookatthefollowing
links:
YouWave ,
JarOfBeansand
Andyroid.
17
Quotefromthegenymotionwebsite
26
7. Mobile Device Clouds
Somedrawbacksofusinglocalemulatorsarethatyouneedtomanagealldevicesbyyourself,installthe
requiredAPIsandtheextrastressforyourCPU.Ifyoudonotwanttosetupemulatorsorrealdevices
yourselfyoucouldalsorentthemonline.Alotofvendorsoffersuchdevicesforexecutingmobiletests.Of
course,allthesesolutionsareSystemasaServicesolutions,sononeofthemisfree.Thefollowingsection
giveyouanoverviewaboutsomecloudmobilerentingservicesfromwhichyoumaychoose.
27
7.1 AWS Device Farm
AmazonoffersyoutotestyourAndroidandFireOSappsintheircloud.Alltestsareexecutedonreal
devicesinparallel.Youcaneitherchoosetoperformyourthetestonasmartphoneoronatablet.
Website http://aws.amazon.com/de/devicefarm/
Suitablefor nativeapps,hybridapps
Physical yes(>200)
Dev.
VirtualDev. no
Support Appium,Calabash
Pricing 0,17USDperdevice/min.oramonthlyflatfor250USD
Information CanIuseAWStotestthemobilewebaswell?
Amazon:
Hi,thanksforthequestion.Today,AWSDeviceFarmsupportstestingofnativeand
hybridapps.Forwebapplications,thismeansyoucantesttheappwithinathin
container(WebVieworsimilar)orinaninstrumentedbuildofabrowser.(17thJuly
2015)
AdditionalLinks:
http://docs.aws.amazon.com/devicefarm/latest/developerguide/welcome.html
https://aws.amazon.com/de/blogs/aws/awsdevicefarmtestmobileappsonrealdevices/
https://forums.aws.amazon.com/thread.jspa?messageID=648896
7.2 Keynote
Keynoteisacompanywhichisspecialisedinperformancemonitoring.Theyarethe(selfclaimed)business
leaderincloudbasedtestingandoffercustomersrealdevicesinacloudfortesting.
Website http://www.keynote.com/solutions/testing/mobiletesting
Suitablefor nativeapps,hybridapps,mobileweb
Physical yes(~300)
Dev.
VirtualDev. no
Support Appium
Pricing between0750$
28
Information DeviceAnywhereCloudwhichcontainsthelargestmobiledevicelibraryavailable.
EasytocombinewithAppium.Justcreateadeviceandtheserversendsyoubackan
AppiumIDtouseduringtesting.
Requiresavalidcreditcardforsignup
AdditionalLinks:
http://www.heise.de/developer/meldung/KeynoteMobileTestingmitAppiumFrameworkgekoppelt.html
http://www.keynote.com/solutions/testing/appiumintegration
7.3 Xamarin
Website http://xamarin.com/testcloud
Suitablefor nativeapps,hybridapps,mobileweb
Physical yes(>1000)
Dev.
VirtualDev. no
Support Calabash
Pricing between1,00012,000$
Information Probablytooexpensiveforcustomers
7.4 Saucelabs
Website https://saucelabs.com/mobile/
Suitablefor nativeapps,hybridapps,mobileweb
Physical no
Dev.
VirtualDev. yes(numberunknown)
Support Appium,Selendroid
Pricing between12149$
Information Worksveryreliable
Superslow(~10minutes)
bloated
29
7.5 BrowserStack
Website https://www.browserstack.com/automate
Suitablefor (hybridapps),mobileweb
Physical yes(iOS)
Dev.
VirtualDev. yes(>50)
Support Appium
Pricing between2999$(1user/1vm)
Information CapabilityConfigurator
iOSandMACsupport
iOShassometimesproblemstostart
AndroidusesthedeprecatedAndroidDriver
7.6 Perfectomobile
Website http://www.perfectomobile.com/
Perfectomobileoffersyoutorentrealmobiledeviceshostedbyorganisationsand/orpeople.Youcan
choosefromvariousvendorsandmodelsandifyourdesireddeviceisavailableaconnectionis
established.Youevengetthephonenumberforthedevicesothatyoucandophonecallsandwriteor
readSMS.Thatisprettyawesomebecauseyoucangetinstantlyhundredsofnewphonenumbersto
registeryourselfsomewhere.
ThedownsideofthePerfectomobileserviceisthatcustomerisresponsibleforclearinghisowndata
aftereachsession.Thatmeanswhenyouredoneusingthephoneanddisconnect,anotherusermay
reuseyourpreviouslyusedphoneandhasaccesstoallthedatayoustoredonwhichwasnotproperly
deleted.Soitisuptoyoutowipealldataafterthephoneusage.
30
8. SiteGenesis Community Testsuite Wrappert
Belowyouwillfinddifferentcodesnippets,thatdemonstrateshowtoruntestcasesagainstmobile
emulators/realdevices.SomesnippetsarewrapperclassesthatallowyoutorunyourSiteGenesis
CommunityTestsuitetestscenariosasmobiletestcases.
Requires:
seleniumserverstandalone2.46.0.jar
public
class
BrowserStack
{
publicstatic
final
String
USER="your-username"
;
publicstatic
final
String
KEY
="your-password"
;
publicstatic
final
String
URL
="http://"
+USER +":"
+KEY
+"@hub.browserstack.com/wd/hub"
;
public
static
void
main(String args[])
throws Exception
{
DesiredCapabilities
caps=new DesiredCapabilities();
caps
.setCapability(
"browserName" ,"iPhone"
);
caps
.setCapability(
"platform"
,"MAC" );
caps
.setCapability(
"device"
,
"iPhone 5" );
WebDriver driver
=new RemoteWebDriver( new URL(
URL ),
caps
);
driver
.get(
"http://www.google.com" );
WebElement searchField
= driver
.findElement(By.
name(
"q"
));
searchField
.sendKeys("Testing on BrowserStack"
);
searchField
.submit();
System.
out
.println(
driver
.getTitle());
driver
.quit();
}
}
Requires:
runningemulator(devicenameNexus5)
runningAppiumservice
javaclient3.0.0.jar
seleniumserverstandalone2.46.0.jar
public
class
Appium30
{
publicstatic
void
main(String[]
args)throwsMalformedURLException
{
caps=new DesiredCapabilities();
caps= DesiredCapabilities.
android();
caps
.setCapability(MobileCapabilityType.
DEVICE_NAME ,"Nexus5");
caps
.setCapability(MobileCapabilityType.
PLATFORM_NAME , MobilePlatform.
ANDROID
);
caps
.setCapability(MobileCapabilityType.
BROWSER_NAME , MobileBrowserType.
BROWSER);
31
final
WebElement
element
=driver
.findElement(By.
id
(
"kw"
));
element
.sendKeys("Nexus 5"
);
element
.submit();
if
(
driver!=
null
)
{
driver
.quit();
}
}
}
Requires:
runningemulator(devicenameNexus5)
runningAppiumservice
SGCTStestcasewhichextendsAppiumWrapper
javaclient3.0.0.jar
seleniumserverstandalone2.46.0.jar
public
class
AppiumWrapper
extends AbstractScriptTestCase
{
privatestatic
final
WebDriver
driver
;
static
{
Runtime.
getRuntime
().addShutdownHook
(
new Thread()
{
@Override
publicvoid
run()
{
AppiumWrapper.
quitDriver
();
}
}
);
DesiredCapabilities
caps=new DesiredCapabilities();
caps= DesiredCapabilities.
android
();
caps
.setCapability(MobileCapabilityType.
DEVICE_NAME
,
"Nexus5");
caps
.setCapability(MobileCapabilityType.
PLATFORM_NAME, MobilePlatform.
ANDROID
);
caps
.setCapability(MobileCapabilityType.
BROWSER_NAME, MobileBrowserType.
BROWSER);
URL
appiumUrl
=null
;
try
{
appiumUrl =new URL(
"http://127.0.0.1:4723/wd/hub"
);
}
catch(MalformedURLException e)
{
e
.printStackTrace();
}
driver =new RemoteWebDriver( appiumUrl
,
caps
);
}
public
AppiumWrapper()
{
setWebDriver(
driver
);
}
public
static
void
quitDriver()
{
32
System.out
.println(
"Quit Driver"
);
if
(
driver
!=
null)
{
driver
.quit();
}
}
}
Requires:
Chrome(Browser)
ChromeDriver18
SGCTStestcasewhichextendsChromeWebDriver
seleniumserverstandalone2.46.0.jar
public
class
ChromeWebDriverWrapper extends
AbstractScriptTestCase
{
private
static
WebDriver
driver
;
static
{
Runtime.
getRuntime
().addShutdownHook(
new Thread(){
@Override
public
void
run() {
ChromeWebDriverWrapper.
quit
();
}
});
System.
setProperty
(
"webdriver.chrome.driver"
,
"/home/Appium/Downloads/chromedriver"
);
Map<String, String>
phone=
new HashMap<>();
phone
.put(
"deviceName",
"Google Nexus 5"
);
Map<String, Object>
chromeOptions=new HashMap<>();
chromeOptions
.put(
"mobileEmulation"
,
phone
);
DesiredCapabilities
caps
= DesiredCapabilities.
chrome();
caps
.setCapability(ChromeOptions.
CAPABILITY ,
chromeOptions
);
driver
=new
ChromeDriver(
caps
);
}
public
ChromeWebDriverWrapper()
{
if
(
driver!=
null
)
{
setWebDriver(
driver
);
}
}
public
static
void
quit()
{
if
(
driver!=
null)
{
driver
.quit();
}
}
}
18
https://sites.google.com/a/chromium.org/chromedriver/downloads
33
9. Known Problems
SomedriversdoesntsupporttheXLTscreenshotimplementationandthereforecrashwhiletakinga
screenshot.ToavoidthatproblemmakesuretoexplicitsetthedriverdevicecontexttoNATIVE_APP.
Makesurethatyousavethepreviouslyassignedvalueandresetthecontexttoit,otherwisestrange
behaviormayoccur.
URL
url
=newURL(
"http://127.0.0.1:4723/wd/hub");
AppiumDriver<WebElement> driver
=new AndroidDriver<>(
url
,
caps
);
driver
.get(
"https://www.browserstack.com/" );
WebElement element =
driver
.findElement(By.id
(
"pricing-upgrade-link"
));
new Actions(driver
).moveToElement( element
).perform();
driver
.context(
context
);
driver
.quit();
Sometimesclickingonelementsdoesnotseemtowork.Ofcoursethereareseveralreasonswhyitmay
notworkasexpected,soyoushouldstartyourinvestigationbydebuggingyourcodeandtakealookatthe
returnvaluesofsomefunctions(istheelementlocatorcorrect,wastheelementfound).Ifeverythingis
correct,theremightbeanother,hardertotrack,error,thatscausingthisbehavior:invisiblescrollbars.
Letsclarifywhatthiserrorisbyanexample:imagineyouwanttocheckacheckboxonawebsite.Thatbox
ispartofaformandisnotdisplayedinthecurrentviewport(therectanglewhichcoversthemobiledisplay).
Takealookat screenshot9A .
Screenshot9A Screenshot9B
34
Mostwebdriverimplementationsexpectthatelementstointeractwith,mustliewithinthecurrentviewport.If
anelementliesnotwithintheviewport,thewebdrivertriestoscrolltotheelement.In
screenshot9Bthe
webdriverscrollsasfarasneededtofullydisplaythecheckboxintheviewport.Assoonasthecheckboxis
visibletheclickcommandisfired.Butnothinghappens?Now,howdidthatcome?
Somewebpagesoffershorizontal/verticalscrollbarsfornavigation.However,mobiledevicesaremostly
configuredtouseasmuchspaceaspossibletodisplayinformationintheviewport.Sounnecessary
elements,likescrollbars,arefadedout.Theyareonlydisplayedwhentheyarerequired:duringscrolling.
Sowhathappenedinthisscenarioisthis:thewebdriverstartsscrollingtothecheckbox.Ascrollbaris
displayedforafractionofasecond.Thescrollbarisdrawnoverourcheckboxandcoverstheelement
belowwhichthewebdriverdoesnotrecognize.Insteadofcheckingthecheckboxbyaclick,anotherUI
element,thescrollbar,issimplyclicked.
Screenshot9C demonstratestheexplainedsituation.
Asworkaroundyoucouldscrolltoeachelementbeforeyouexecuteaclickandwaituntilacertain
thresholdisreached(forexample,scrollandwait100ms).Itmaynotbethebestsolution
Screenshot9C
35
9.3 Appium Buildpath Order
PayattentiononorganizingyourimportswhenusingSGCTSandAppium.Itisrecommendedthatyour
includedlibrarieshaveacertainorder,otherwisesomestrangeruntimebehaviormaybedetected.
Unfortunately,errorsrelatedtothebuildpathorderarehardtotrack,becauseerrormessagesthatmight
appearoftenseemsalittlevague.IncaseyouhavetroubletorunyourAppiumprojectforunknown
reasonsyoumightwanttotrytoadaptthebuildpathorder.Thescreenshotbelowshowsyouapossible
configurationforusingAppiuminconjunctionwithSGCTS.
WhenusingMicrosoftWindows19 asoperatingsystemitmaybepossiblethattheperformanceofyourlocal
emulatorsisreallychoppy,ifyoudonthaveinstalledthelatestIntel20 HardwareAcceleratedExecution
Manager(HAXM).HAXMisahypervisorwhichallowsyoutotakeadvantageofIntelsVirtualization
Technology(IntelVT)andthismaygiveyourlocalemulatorsahugeperformanceboost.Thatsthereason
whytheAndroidSDKManageroffersyouthe Intelx86EmulatorAccelerator(HAXMinstaller) package.So
ifyousufferunderthelowperformanceyoumaygivethatpackageatry.
19
http://www.microsoft.com/enus/windows
20
http://www.intel.de/content/www/de/de/homepage.html
36
10. Sources
http://selendroid.io/faq.html
http://selendroid.io/architecture.html
http://spring.io/guides/gs/android/
http://www.guru99.com/introductiontoselendroid.html
https://docs.saucelabs.com/tutorials/appium/
http://appium.io/
http://appium.io/slate/en/master/?java#appium
http://appium.io/introduction.html
http://nishantverma.gitbooks.io/appiumforandroid/content/appium/why_appium.html
37