Mobile automation test Appium basic operation API

Appium automated test framework

Learning objectives:

1. able to install Appium desktop client
2. able to install appium Python Library

Appium introduction:

Appium is an automated testing framework for mobile terminal, which can be used to test native applications, mobile web applications and hybrid applications. It is platform broken and can be used for Android and IOS operating systems.
The important thing is that Appium is cross platform and can be scripted with a set of APIs for different platforms

Appium automated test environment setup:

1.Appium desktop client installation method
2. Appium Python library installation

Hello Appium

Learning objectives:

The 1. be able to start any application using appium
The 2. be able to understand the meaning of various parameters in "pre code"

1, Quick experience
Application scenario:

When we do app automation, we must aim at a certain product. To test a certain software, we must first ask the simulator or real machine to open the software for us, so what we need to learn next is how to open a HAOGE application.

Requirements:

Use the following steps to open the settings application in the simulator:

The 1. open the mobile phone simulator
The 2. open Appium
The 3. create a python project named hello_appium
The 4. create a demo Py file
The 5. copy the following code directly and run it

from appium import webdriver
import time

desired_caps = dict()
#The name of the platform, regardless of case, cannot be scrawled
desired_caps['platformName'] = 'Android'
#Platform version (5.4.3 can be written as 5.4.3, 5.4 and 5 can be written)
desired_caps['platformVersion'] = '5.1'
#The name of the device can be written casually for Android, and the IOS should be consistent with the device
desired_caps['deviceName'] = '127.0.0.1:62001'
#Application to open
desired_caps['appPackage'] = 'com.android.settings'
#Interface to open
desired_caps['appActivity'] = '.Settings'

#Connect appium service
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

time.sleep(5)
driver.quit()
2, Detailed explanation of parameters
Application scenario:

Change configuration information

Appium basic operation API

1.1 start other app s in the script
Application scenario:

If an application needs to jump to another application, you can use this api to jump to the application, just as we will jump to the payment application after placing an order through the takeout application

Methods and parameters:
#Start other app s in script
#parameter
#	appPackage: name of the package to open
#	appActivity: interface name of the program to open
driver.start_activity(appPackage,appActivity)
1.2 get the package name and interface name of the app
Application scenario:

When we jump from one application to another, we can call this attribute to obtain the package name, interface name, or the corresponding information in the report

Attribute name:
#Get package name
driver.current_package
#Get interface name
driver.current_activity
Example

Open the settings application to output the current package name and interface name

from appium import webdriver
import time

desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '127.0.0.1:62001'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

time.sleep(5)

#Print current package name
print(driver.current_package)

#Print current interface name
print(driver.current_activity)

driver.quit()
1.3 closing app and driver objects
Application scenario:

Sometimes we need to close an application before opening a new one. How do we close the application?

Method name:
#Close the app of the current operation without closing the driver object
driver.close_app()
#Close the driver object and all associated app s
driver.quit()
Example:

Open settings and use close_ Close the app () method, try to use the quit() method again, and finally print the package name of the current program to observe the difference

from appium import webdriver
import time

desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '127.0.0.1:62001'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

time.sleep(3)

#close applications
driver.close_app()

# Close drive object
driver.quit()
1.4 Android and uninstall and whether to install app
Application scenario:

Some application market software may have a button. If a program has been installed, uninstall it. If not, install it

Method name:
#Install app
#Parameters:
#	app_path:apk path
driver.install_app(app_path)

#Uninstall app
#parameter
#	app_id: application package name
driver.remove_app(app_id)

#Determine whether the app has been installed
#Parameters:
#	app_id: application package name
#Return value:
#Boolean type, True = installed, False = not installed
driver.is_app_installed(app_id)
Example:

If muke.com has been installed, uninstall it; if not, install it

from appium import webdriver
import time

desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '127.0.0.1:62001'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

time.sleep(3)

if driver.is_app_installed('cn.com.open.mooc'):
    driver.remove_app('cn.com.open.mooc')
else:
    driver.install_app("C:\\Users\\admin\\Desktop\\william\\imooc7.3.710102001android.apk")
    time.sleep(5)


1.5 putting applications in the background
Application scenarios;

After a certain period of time in the background, the bank app will re-enter the password if it returns to the foreground page. If you want to test this function, you can use this api for testing

method
#After the app is placed in the background for a certain period of time, it returns to the foreground to simulate the hot start
#Parameters:
#	Seconds: how many seconds does the background stay
driver.background_app(seconds)
Example

Open the settings application, enter the background for 5 seconds, and then return to the foreground

from appium import webdriver
import time

desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '127.0.0.1:62001'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

time.sleep(3)

driver.background_app(5)

time.sleep(3)

Notes:

Hot start: it means entering the background and returning to the front desk, and turning off and then on. This behavior of cutting off the power supply can be called "cold start"

Posted by glc650 on Wed, 01 Jun 2022 05:36:13 +0530