@@ -230,3 +230,211 @@ Zoom in on an element, doing a pinch out operation.
230230#### ` driver.pinch `
231231
232232Zoom out on an element, doing a pinch in operation.
233+
234+
235+
236+ ### Application management methods
237+
238+ There are times when you want, in your tests, to manage the running application,
239+ such as installing or removing an application, etc.
240+
241+
242+ #### Backgrounding an application
243+
244+ The method ` driver.background_app ` sends the running application to the background
245+ for the specified amount of time, in seconds. After that time, the application is
246+ brought back to the foreground.
247+
248+ ``` python
249+ driver.background_app(1 )
250+ sleep(2 )
251+ el = driver.find_element_by_name(' Animation' )
252+ assertIsNotNone(el)
253+ ```
254+
255+
256+ #### Checking if an application is installed
257+
258+ To check if an application is currently installed on the device, use the ` device.is_app_installed `
259+ method. This method takes the bundle id of the application and return ` True ` or
260+ ` False ` .
261+
262+ ``` python
263+ assertFalse(self .driver.is_app_installed(' sdfsdf' ))
264+ assertTrue(self .driver.is_app_installed(' com.example.android.apis' ))
265+ ```
266+
267+
268+ #### Installing an application
269+
270+ To install an uninstalled application on the device, use ` device.install_app ` ,
271+ sending in the path to the application file or archive.
272+
273+ ``` python
274+ assertFalse(driver.is_app_installed(' io.selendroid.testapp' ))
275+ driver.install_app(' /Users/isaac/code/python-client/test/apps/selendroid-test-app.apk' )
276+ assertTrue(driver.is_app_installed(' io.selendroid.testapp' ))
277+ ```
278+
279+
280+ #### Removing an application
281+
282+ If you need to remove an application from the device, use ` device.remove_app ` ,
283+ passing in the application id.
284+
285+ ``` python
286+ assertTrue(driver.is_app_installed(' com.example.android.apis' ))
287+ driver.remove_app(' com.example.android.apis' )
288+ assertFalse(driver.is_app_installed(' com.example.android.apis' ))
289+ ```
290+
291+
292+ #### Closing and Launching an application
293+
294+ To launch the application specified in the desired capabilities, call ` driver.launch_app ` .
295+ Closing that application is initiated by ` driver.close_app `
296+
297+ ``` python
298+ el = driver.find_element_by_name(' Animation' )
299+ assertIsNotNone(el)
300+ driver.close_app();
301+
302+ try :
303+ driver.find_element_by_name(' Animation' )
304+ except Exception as e:
305+ pass # should not exist
306+
307+ driver.launch_app()
308+ el = driver.find_element_by_name(' Animation' )
309+ assertIsNotNone(el)
310+ ```
311+
312+
313+ ### Other methods
314+
315+
316+ #### Retrieving application strings
317+
318+ The property method ` driver.app_strings ` returns the application strings from
319+ the application on the device.
320+
321+ ``` python
322+ strings = driver.app_strings
323+ ```
324+
325+
326+ #### Sending a key event to an Android device
327+
328+ The ` driver.keyevent ` method sends a keycode to the device. The keycodes can be
329+ found [ here] ( http://developer.android.com/reference/android/view/KeyEvent.html ) .
330+ Android only.
331+
332+ ``` python
333+ # sending 'Home' key event
334+ driver.keyevent(3 )
335+ ```
336+
337+
338+ #### Retrieving the current running activity
339+
340+ The property method ` driver.current_activity ` returns the name of the current
341+ activity running on the device.
342+
343+ ``` python
344+ activity = driver.current_activity
345+ assertEquals(' .ApiDemos' , activity)
346+ ```
347+
348+
349+ #### Set a value directly on an element
350+
351+ Sometimes one needs to directly set the value of an element on the device. To do
352+ this, the method ` driver.set_value ` or ` element.set_value ` is invoked.
353+
354+ ``` python
355+ el = driver.find_element_by_class_name(' android.widget.EditText' )
356+ driver.set_value(el, ' Testing' )
357+
358+ text = el.get_attribute(' text' )
359+ assertEqual(' Testing' , text)
360+
361+ el.set_value(' More testing' )
362+ text = el.get_attribute(' text' )
363+ assertEqual(' More testing' , text)
364+ ```
365+
366+
367+ #### Retrieve a file from the device
368+
369+ To retrieve the contents of a file from the device, use ` driver.pull_file ` , which
370+ returns the contents of the specified file encoded in [ Base64] ( https://docs.python.org/2/library/base64.html ) .
371+
372+ ``` python
373+ # pulling the strings file for our application
374+ data = driver.pull_file(' data/local/tmp/strings.json' )
375+ strings = json.loads(data.decode(' base64' , ' strict' ))
376+ assertEqual(' You can\' t wipe my data, you are a monkey!' , strings[u ' monkey_wipe_data' ])
377+ ```
378+
379+
380+ #### Place a file on the device
381+
382+ To put a file onto the device at a particular place, use the ` driver.push_file `
383+ method, which takes the path and the data, encoded as [ Base64] ( https://docs.python.org/2/library/base64.html ) , to be written to the file.
384+
385+ ``` python
386+ path = ' data/local/tmp/test_push_file.txt'
387+ data = ' This is the contents of the file to push to the device.'
388+ driver.push_file(path, data.encode(' base64' ))
389+ data_ret = driver.pull_file(' data/local/tmp/test_push_file.txt' ).decode(' base64' )
390+ self .assertEqual(data, data_ret)
391+ ```
392+
393+
394+ #### Complex find in Android
395+
396+ Appium supports a way to do complex searches for elements on an Android device.
397+ This is accessed through ` driver.complex_find ` . The arguments and use case,
398+ to borrow from Winston Churchill, remain a riddle, wrapped in a mystery, inside
399+ an enigma.
400+
401+ ``` python
402+ el = self .driver.complex_find([[[2 , ' Ani' ]]])
403+ self .assertIsNotNone(el)
404+ ```
405+
406+
407+ def end_test_coverage(self, intent, path):
408+ """Ends the coverage collection and pull the coverage.ec file from the device.
409+ Android only.
410+
411+ See https://github.com/appium/appium/blob/master/docs/en/android_coverage.md
412+
413+ :Args:
414+ - intent - description of operation to be performed
415+ - path - path to coverage.ec file to be pulled from the device
416+ """
417+ data = {
418+ 'intent': intent,
419+ 'path': path
420+ }
421+ self.execute(Command.END_TEST_COVERAGE, data)
422+ return self
423+
424+ def lock(self, seconds):
425+ """Lock the device for a certain period of time. iOS only.
426+
427+ :Args:
428+ - the duration to lock the device, in seconds
429+ """
430+ data = {
431+ 'seconds': seconds
432+ }
433+ self.execute(Command.LOCK, data)
434+ return self
435+
436+ def shake(self):
437+ """Shake the device.
438+ """
439+ self.execute(Command.SHAKE)
440+ return self
0 commit comments