Templates for remote Webdriver
Open port 4723 to accept Webdriver requests by default
Default on 4724 for Android communication
#Coding:utf-8#Import the Common packageImportOSImportUnitTest fromAppiumImportWebdriver fromTimeImportSleep#Set PATH informationPATH =LambdaP:os.path.abspath (Os.path.join (Os.path.dirname (__file__), p))classloginandroidtests (unittest. TestCase):defsetUp (self):#Initialize test PlatformDesired_caps ={} desired_caps['PlatformName'] ='Android'desired_caps['platformversion'] ='4.4.2' #Android版本desired_caps['devicename'] ='127.0.0.1:62001' #连接到的设备名称, this is the name of the night God Simulator. #desired_caps[' app ' = ' D:\apk\ love wallpaper. apk ' #app的路径desired_caps['Apppackage'] ='Com.lovebizhi.wallpaper' #包名desired_caps['appactivity'] ='com.lovebizhi.wallpaper.WelcomeActivity' #activity名称Self.driver= Webdriver. Remote ('Http://127.0.0.1:4723/wd/hub', Desired_caps)defTearDown (self): Self.driver.quit ()defTest (self):#Test Navigation page Print("Start test ...") #Determine if love Wallpapers app is installedWallpaper = self.driver.is_app_installed ("Com.lovebizhi.wallpaper") ifWallpaper:#Self.driver.remove_app ("Com.lovebizhi.wallpaper")Sleep (8) #Click on a wallpaper imageSELF.DRIVER.FIND_ELEMENTS_BY_ID ("Com.lovebizhi.wallpaper:id/image1") [4].click () Sleep (4) #Click Set WallpaperSELF.DRIVER.FIND_ELEMENT_BY_ID ("Com.lovebizhi.wallpaper:id/btsetup"). Click () Sleep (5) Else: Self.driver.install_app ("D:\apk\ Love wallpaper. apk") Sleep (30)if __name__=='__main__': Suite=UnitTest. Testloader (). Loadtestsfromtestcase (loginandroidtests) unittest. Texttestrunner (verbosity=2). Run (Suite)
How do I check the app's Apppackage and appactivity?
1, download the Apktool-new package, and unzip
2. Double-click Decode.bat to run
3. Drag the apk into the Decode.bat and enter it to get the corresponding apk folder
4. Open the Androidmanifest.xml in the APK folder with Notepad or notepad++ to see Apppackage and appactivity names
At the time of Romote Webdriver initialization
desired_caps['apppackage'com.lovebizhi.wallpaper ' desired_caps['appactivity ' Com.lovebizhi.wallpaper.WelcomeActivity
It has been determined which app and which interface in the app to test. When you run the Python script directly, you jump to the corresponding interface, and in test you cannot manipulate elements that do not exist in this interface. Otherwise it will be an error
If you want to uninstall the app or other app in the main section, you'll get an error.
#Coding:utf-8#Import the Common packageImportOSImportUnitTest fromAppiumImportWebdriver fromTimeImportSleep#Set PATH informationPATH =LambdaP:os.path.abspath (Os.path.join (Os.path.dirname (__file__), p))classloginandroidtests (unittest. TestCase):defsetUp (self):#Initialize test PlatformDesired_caps ={} desired_caps['PlatformName'] ='Android'desired_caps['platformversion'] ='4.4.2'desired_caps['devicename'] ='127.0.0.1:62001' #desired_caps[' app ' = ' D:\apk\ love wallpaper. apk 'desired_caps['Apppackage'] ='Com.lovebizhi.wallpaper'desired_caps['appactivity'] ='com.lovebizhi.wallpaper.WelcomeActivity'Self.driver= Webdriver. Remote ('Http://127.0.0.1:4723/wd/hub', Desired_caps)defTearDown (self): Self.driver.quit ()deftest_1 (self):#Test Navigation page Print("start Test1 ...") #Determine if love Wallpapers app is installedWallpaper = self.driver.is_app_installed ("Com.lovebizhi.wallpaper") ifWallpaper: self.driver.remove_app ( "Com.lovebizhi.wallpaper" ) sleep (8) Else: Self.driver.install_app ( "D:\apk\ love wallpaper. apk" ) sleep (30) if __name__=='__main__': Suite=UnitTest. Testloader (). Loadtestsfromtestcase (loginandroidtests) unittest. Texttestrunner (verbosity=2). Run (Suite)
The following error is reported:
So do not use uninstall or install the program after initialization.
Appium Remote Webdriver Call