On AppUI automation of Appium

1, Appium introduction

Appium is an open source testing automation framework that can be used for native app, hybrid (html) and mobile Web application () testing. It uses the WebDriver protocol to drive iOS, Android and Windows applications.

2, Appium advantages

  • android and ios can be supported across platforms at the same time
  • Support multiple languages, such as java, python, php, Ruby, etc
  • Don't worry about the complex environment
  • Have selenium experience and can start directly

3, Environmental dependence

  • Appium desktop -- > two in one node JS and appium
  • Appium-Python-Client
  • Python
  • JDK
  • Andriod SDK

3.1. Android SDK installation and configuration

Download address: http://tools.android-studio.org/index.php/sdk
Download the corresponding version according to your PC platform
After installation, you need to configure the environment variable: ANDROID_HOME has been configured before the adb operation. Add the following to it

4, Appium Desktop

Appium Desktop is an open source application for Mac, Windows and Linux. It provides you with the powerful functions of appium automation server with a beautiful and flexible user interface. It is a combination of several appium related tools:

  • Graphical interface of Appium Server. You can set options, start / stop the server, view logs, etc... you do not need to use the NPM of the Node to install Appium, because the Node runtime is bundled with the Appium Desktop.
  • You can use the Inspector to view the elements of an application, get basic information about them, and interact with them.
    [note]: Appium Desktop is different from appium. Appium Desktop is the graphical front end of appium with other tools. Appium Desktop is released at its own pace and has its own version control system. Just like many customized Android systems in China have their own version numbers, but they are encapsulated based on an Android system version. The version number is not necessarily the same as the version number of the Android native system.
    For example: Meizu flyme6 The kernel of 0 system is Android 5.1

4.1. Installing appium desktop

Download address: https://github.com/appium/appium-desktop/releases
Appium desktop tool actually encapsulates Appium server and node JS depends on the environment and the graphical tool for obtaining elements, which itself is a separate tool; The appium desktop main interface contains three menus: Simple, Advanced and Presets

4.1.1,Simple

4.1.1.1,host

Set the ip address of Appium server. You can change the ip address to 127.0.0.1 for local debugging

4.1.1.2,port

Set the port number. The default value is 4723. It doesn't need to be modified

4.1.1.3,start server

Start appium server

4.1.2,Advanced

Advanced parameter configuration modification mainly refers to the configuration of some Android and iOS devices, log path and other related information

4.1.3,persets

Use some configuration information in Advanced as the default configuration

5, Capability

5.1. What is Capability

  • The function of desired capability is to configure Appium sessions. They tell the Appium server which platform (version) and application you want to automate.
  • Desired Capabilities is a set of set key value pairs, where the key corresponds to the name of the setting and the value corresponds to the value of the setting. (e.g. "platformName": "Android") Desired Capabilities is mainly used to notify Appium server to establish the required Session.

5.2,session

The communication between Appium's client and server must be carried out in the context of a session. When initiating communication, the client will first send a JSON object called "Desired Capabilities" to the server. After receiving the data, the server will create a session and return the session ID to the client. After that, the client can send subsequent commands with the ID of the session.

5.3 common Capability configurations

Official and complete documentation of Capability
If you know about Capability, you will find a problem. In fact, it is mainly divided into three parts: public part, ios part and android part. If android wants to use ios, it is impossible. so, honestly understand what each platform has and what their role is


5.4. Capability launch App demo

5.4.1 New Session Window session establishment

  • Automatic Server local AppiumServer service
  • Custom Server: for example, this is useful if you want to start an Inspector session against an Appium server running on another computer in the network.
  • Sauce Labs: if you cannot access the iOS emulator on the machine, you can use the Sauce Labs account to start Appium sessions in the cloud.
  • TestObject: you can also use TestObject's real device cloud for real machine testing.
  • headspin: use a remote device to create a session.

5.4.2. desired capability parameter Json

Use the adb shell dumpsys window | findstr mCurrentFocus command to view the currently running package name and Activity, which is clearer
adb shell dumpsys activity activities >C:\Users\xxx\Desktop\aa.txt
View information such as activation: appt dump baking e: \ XXX apk
Find the initial loading page: appt dump bagging e: \ XXX apk | findstr “lauchable-activity”

5.4.3 common problems

——Those trampled pits

[MJSONWP] Encountered internal error running command: Error: Error occured while starting App. Original error: Permission to start activity denied.

If android:exported = "true" is not added to the activity list file, the corresponding activity cannot be opened directly. It needs to be opened from the activity on the startup page. The exported property is to set whether the activity is allowed to be called by other programs
The new session window allows you to construct a set of desired capabilities for starting Appium sessions. You can start a session against the currently running Appium Desktop server (the default), or you can start a session against various other endpoints.
Because you do not need to use Appium Desktop's own server, you can enter a New Session window without starting the Appium Desktop server. Just click "File" (Windows / Linux) or "Appium" (Mac) and select "New Session..." and it will open a New Session window without starting the local server. In this case, attaching to the local server is disabled.

6, Case - kaoyan3 1.0. apk

6.1 inspection items before operation

1. Check whether the equipment is connected
2. Check whether Appium server is started
3. Check whether the Capability configuration information is correct

6.2,Appium Settings,Unlock

1. When appium is started for the first time, two daemon apps will be installed on the device. Appium Settings and Unlock some device systems require users to manually confirm the installation due to permission problems (such as Samsung S6 edge +). Otherwise, failure to install daemon apps will lead to script failure. Do not uninstall these two apps after installation.

  • Unlock: used to unlock the pop-up prompt of the phone
  • Appium Setting: appium Guardian app
    2. The webdriver module in from appium import webdriver is different from that in selenium!
{python Installation path}}\Lib\site-packages\appium\webdriver #webdriver module source code path

6.3 Appium error reporting & solution

——The pit we stepped on in those years

6.3.1 Appium service is not started

urllib.error.URLError: <urlopen error [WinError 10061] Unable to connect because the target computer actively refused.

[solution] after clicking the start Appium button, the [Appium] welcome to Appium v1.0 will appear 7.2 run the script after prompt

6.3.2. Session conflict

error: Failed to start an Appium session, err was: Error: Requested a new session but one was in progress

[error analysis] the previous session was not closed, then the test instance was run, and no override was set
[solution] restart the Appium service, start the Appium service, check the Allow Session Override option on the advice interface, restart the Appium test, and add a driver in AfterClass quit()

6.3.3. java environment is not installed

selenium.common.exceptions.WebDriverException: Message: A new session could not be created. (Original error: 'java -version' failed. Error: Command failed: C:\WINDOWS\system32\cmd.exe /s /c "java -version"

6.3.4 the equipment is not connected

selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not find a connected Android device.

[solution] because the device is not connected, or USB Debug is not enabled after connection. You need to reconnect the device.

6.3.5 remember to update the following attributes after replacing the mobile device, otherwise the script cannot run normally.

desired_caps['platformVersion'] = 'XXX'
desired_caps['deviceName'] = 'Galaxy S6 edge+'

6.3.6. The launchable activity value is written incorrectly

Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity

[solution]

  • Correct the misspelled launch activity.
  • If this activity exists, it must be androidmainfest xml. In XML, the attribute exported=false set by the current activity indicates that the current activity cannot be awakened by an external program. (appium can't wake up this class) the R & D personnel need to modify the parameters.

6.3.7 system permissions

Failure [INSTALL_FAILED_USER_RESTRICTED])

[solution]
1.USB installation management permission limit, just close it.
2. Enable the allow unknown source app installation option

6.3.8 abnormal service

An unknown server-side error occurred while processing the command" while opening the App

[solution] restart Appium service

6.4 element positioning

Like Web automated testing, the most important link in the process of app automated testing is element positioning. Only when the element is accurately located can the operation of relevant elements be carried out, such as input, click, drag, slide, etc. appium provides many methods for element positioning, such as id positioning, name positioning, class positioning, level positioning, etc. next, we will practice these positioning techniques.

6.4.1,id

People may have the same name in their daily lives, but each person's id number is unique. In app interface elements, id values can be used to distinguish different elements, and then locate operation. Find can be used in Appium_ element_ by_ id () method to locate the id.

# Use uiautomatorviewer or appium inspector to view
driver.find_element_by_id('android:id/button2')
driver.find_element_by_id('com.tal.kaoyan:id/tv_skip')

6.4.1.1 thinking

  • If the installed version is the latest package or upgraded to the latest version, what should I do if the pop-up element is not upgraded after startup?
  • How to skip the first start and non first start scenarios of the boot page?

6.4.1.2 exception capture

The if statement cannot take effect (because an error will be reported if the element is not found), but we found a breakthrough, that is, to catch the NoSuchElementException exception.

from  appium import webdriver
from selenium.common.exceptions import NoSuchElementException

desired_caps={}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = 'BTF4C17216005082'


desired_caps['app']='E:\kaoyan3.1.0.apk'
desired_caps['appPackage']='com.tal.kaoyan'
desired_caps['appActivity']='com.tal.kaoyan.ui.activity.SplashActivity'

desired_caps['noReset']='True'

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


def check_cancelBtn():
    print("check_cancelBtn")

    try:
        cancelBtn = driver.find_element_by_id('android:id/button2')
    except NoSuchElementException:
        print('no CancelBtn')
    else:
        cancelBtn.click()


def check_skipBtn():
    print("check_skipBtn")
    try:
        skipBtn = driver.find_element_by_id('com.tal.kaoyan:id/tv_skip')
    except NoSuchElementException:
        print('no skipBtn')
    else:
        skipBtn.click()

check_cancelBtn()
check_skipBtn()

Note:

  • send_ When keys() passes in Chinese, you need to configure the following in capability:
desired_caps['unicodeKeyboard']="True"
desired_caps['resetKeyboard']="True"

After setting, Appium's input method daemon will perform input operations

  • After inputting with Appium, if the input method cannot be invoked, you can replace the current input method with the system input method or other input methods in the system settings - language and input method.

6.4.2,name

Locate by name, which is the text attribute for android

from find_element.capability import *
driver.find_element_by_name('enter one user name')
driver.find_element_by_name('Sign in')

Note: appium 1.5 began to abandon this method because the stability of text is not very good.

6.4.3,class

The classname location is based on the element type, but in practice, the classnames of many elements are the same,
For example, in the above example, the user name and password in the login page are both classname, and the attribute value is: "android.widget.EditText". Therefore, only the first element, that is, the user name, can be located, and the password input box needs to be located in other ways, which is actually very difficult In general, if you have an id, you don't have to use classname to locate it.

by_classname.py
from find_element.capability import driver

driver.find_element_by_class_name('android.widget.EditText').send_keys('Fu Tengfei')
driver.find_element_by_class_name('android.widget.EditText').send_keys('1234')
driver.find_element_by_class_name('android.widget.Button').click()

6.4.4 relative positioning

Relative positioning is to find the parent element node of the element with corresponding attributes, and then locate the element based on the parent element.
Test case
Without using the id element positioning method, click the add avatar button in the new user registration interface.

from find_element.capability import driver

driver.find_element_by_id('com.tal.kaoyan:id/login_register_text').click()

root_element=driver.find_element_by_id('com.tal.kaoyan:id/activity_register_parentlayout')
root_element.find_element_by_class_name('android.widget.ImageView').click()

6.4.5,xpath

xpath positioning is a path positioning method, which mainly depends on the absolute path or related attributes of the element. However, the execution efficiency of absolute path xpath is relatively low (especially when the element path is deep), and it is generally used less. xpath is usually used to locate relative paths and attributes.

expressiondescribe
/Select from root node
//Selects nodes in the document from the current node that matches the selection, regardless of their location
nodenameSelect all children of this node
.Select current node
...Select the parent node of the current node
@Select Properties
wildcarddescribe
*Match any element node
@*Match any attribute node
node()Match any type of node
from find_element.capability import driver

driver.find_element_by_xpath('//android.widget.EditText[@text = "please enter user name"]) send_keys('ftf1234')
driver.find_element_by_xpath('//*[@class="android.widget.EditText" and @index="3"]').send_keys('ftf123456')
driver.find_element_by_xpath('//android.widget.Button').click()
# driver.find_element_by_xpath('//*[@class="android.widget.Button"]').click()

Extended data: xpath syntax

6.4.6 UIAutomator positioning

Uiautomator element location is a native location method supported by android system. Although it is similar to xpath, it is easier to use than xpath, and supports all attribute location of elements The positioning principle is to find elements through the class library of android uiautomator. Appium element positioning method is actually encapsulated based on uiautomator.
Use method find_element_by_android_uiautomator() can be positioned using UiAutomator elements.

6.4.6.1 id positioning

id positioning is based on the resource id attribute of the element, using uiselector() Resourceid () method is enough.

from find_element.capability import driver

driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_email_edittext")').send_keys('ftf1234')

driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys('ftf123456')

driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")').click()

6.4.6.2 text positioning

Text positioning is based on the text attribute value of the element. new UiSelector()

driver.find_element_by_android_uiautomator\('new UiSelector().text("enter one user name")').send_keys('ftf1234')

6.4.6.3 class name positioning

Like Appium class, it is located according to the class attribute of the element.

driver.find_element_by_android_uiautomator\('new UiSelector().className("android.widget.EditText")').send_keys('zxw1234')

Keywords: Appium

Added by hoyo on Tue, 18 Jan 2022 02:23:40 +0200