Appium (seven): Appium API (a) Application Operation

1. The application operation

The method of this chapter listed mainly for operation of the application, such as application installation, uninstallation, OFF, ON and the like.

The boot code in front of this, showing only the portions different from behind.

# coding:utf-8
from appium import webdriver
from time import sleep
# Initialization desired_caps = {} # use a mobile platform which [desired_caps ' PlatformName ' ] = ' the Android ' # the Android version desired_caps [ ' platformVersion ' ] = ' 5.1.1 ' # use adb devices -l query, when more than one device when, declare desired_caps [ ' deviceName ' ] = ' 127.0.0.1:62001 ' # package names desired_caps [ ' appPackage ' ] = ' com.android.settings ' #Interface name desired_caps [ ' appActivity ' ] = ' .Settings ' # Start Service Driver = webdriver.Remote ( ' http://127.0.0.1:4723/wd/hub ' , desired_caps)

1.1 get the package name and the name of the application interface

When we jump from one application to another application, we want to export its package name, user name, or want to show the corresponding information in the report, we can call this property to be acquired.

# Print package name 
Print (driver.current_package)
 # print the current interface name 
Print (driver.current_activity)

1.2 install and uninstall applications and to check whether the installation

Install the application:

driver.Install_app (app_path): application to the device is mounted, needs to route packets apk.

Uninstall the application:

driver.remove_app (app_id): To remove an application from the device.

Check that the application is installed:

driver.is_app_installed (app_id): check whether the application has been installed. We need to pass the application package name. Returns a value of True or False.

Case:

We now come to install an application treasure it, first used a computer to download the apk application package, placed under the F drive.

Then we will be dragged into the simulator apk installation, enter the application package, see the name of the application package.

 

 

This is done can begin our example demonstrates.

if driver.is_app_installed("com.tencent.android.qqdownloader"):
    driver.remove_app("com.tencent.android.qqdownloader")
else:
    driver.install_app("E:\yingyongbao_7422130.apk")

# Exit driver1 
driver.quit ()

If the application treasure is already installed, uninstall applications will treasure. If the application treasure is not installed, the application will be installed treasure.

We only run successfully, you can see the effects.

1.3 launch other applications from within a script

We often encounter the need to jump from one application to another application, just as we use it when hungry, need to jump to Bora payment as payment.

We want to achieve this function, it is necessary to use the following method.

driver.start_activity (appPackage, appActivity): each incoming packet interface name and the name of the application you want to open.

Example:

Open the "Settings", wait three seconds after the jump to "address book."

sleep(3)

# Jump to SMS 
driver.start_activity ( ' com.android.contacts ' , ' .activities.PeopleActivity ' )

sleep(5)

# Exit driver1 
driver.quit ()

1.4 application is closed

Sometimes we need to close an application, and then open a new application. So how close the application?

So far we have learned a method to close the application, and that is driver.quit (). But this close it, the drive is off target, while closing all associated applications, after closing we can not use a script to operate the application.

If we want to close the application of the current operation does not close the drive object, we can use driver.close_app () method.

1.5 Reset Application

driver.reset()

Reset application. Equivalent effect "to restore the factory default value". This method does not require passing parameters.

1.6 will be applied in the background

I believe played in the background of the phone knows that the program is not displayed on the phone screen, but it can run properly, the best example is our popular QQ.

method:

driver.background_app (seconds): app placed into the background after a certain time back to the front desk to simulate hot start. This method requires passing a time, indicate how many seconds remain in the background.

Hot start: that goes into the background back to the foreground. Shut off the power of this behavior to open a can called "cold start.

Example:

Open the "Settings", into the background five seconds, then back to the front desk.

time.sleep(3)
driver.background_app(5)
the time.sleep ( 3 )
 # Exit Driver 
driver.quit ()

Guess you like

Origin www.cnblogs.com/liuhui0308/p/12028898.html