APPium automation common instructions

APPium automation common instructions

Common connection configuration

desired_caps = {
    'platformName': 'Android',    # 被检测手机系统 安卓 or 苹果
    'platformVersion': '5.1.1',    # 手机安卓版本
    # 'deviceName': '127.0.0.1:62001',    # 设备名称。如果是真机,在'设置->关于手机->设备名称'里查看
    'deviceName': 'emulator-5554',
    'appPackage': 'com.tencent.mobileqq',    # 启动APP Package名称
    'appActivity': '.activity.SplashActivity',   # 启动Activity的名称
    'noReset': True,  # 不重置APP
    'newCommandTimeout': 6000,    # 加长操作时间,默认一分钟后自动关闭
    # 以上必填,以下选填
    'unicodeKeyboard': True,    # 使用unicode输入法,输入中文时需要使用,填True,看模拟器输入法,有些需要安装,有些不需要
    'resetKeyboard': True,    # 执行完程序回复原来输入法
    # 'automationName': 'UiAutomator2', # 获取toast信息弹出框需添加,但需要先配置UiAutomator2
    # 'app': r'D:\apk\bili.apk',	# 测试apk包的路径,如果有了app就不需要APP Package和Activity,反理同之
}

Connect to Appium Server and initialize the automation environment

from appium import webdriver

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

Implicit wait and explicit wait

Implicit wait

When an element is not found, it does not immediately return an error that the element cannot be found. But periodically (every half second) re-find the element, long time until the element is found or exceeds the specified maximum wait, then it throws an exception (if it is find_elementsa method of class, return empty list).

Selenium's Webdriver object has a method called implicitly_wait

This method accepts a parameter to specify the maximum waiting time.

# 设置缺省等待时间
driver.implicitly_wait(10)

Explicit wait

As the name implies, explicit wait is to set a fixed waiting time after running the code, and then continue to run, the commonly used sleep method of the time module

This method accepts a parameter to specify the maximum waiting time.

import time
time.sleep(1)

Interface element viewing tool

When doing Selenium Web automation, to find the element, we view the characteristics of the element through the developer toolbar of the browser, and locate the element according to these characteristics (attributes and location)

For Appium to automate mobile applications, it also needs tools to view the characteristics of interface elements.

The commonly used viewing tools are: uiautomateviewer and Appium in the Android Sdk package

Positioning element

Appium is based on Selenium, so the basic rules of locating elements are the same as Selenium code:

  • The find_element_by_XXX method returns the first element that meets the conditions, and throws an exception if not found
  • The find_elements_by_XXX method returns a list of all elements that meet the conditions, but returns an empty list if not found
  • Call this method through the WebDriver object, the search scope is the entire interface
  • Call this method through the WebElement object, the search scope is the child node of the node

According to ID

In the Selenium Web automation tutorial, if you can select the positioning element based on the ID, it is best to use the ID, because usually the ID is unique, so the selection based on the ID is efficient.

When the Android application is automated, it can also be searched by ID.

But this ID is the resource-id attribute of the Android application element

Use the following code

driver.find_element_by_id('')

According to CLASS NAME

The class attribute of Android interface elements is actually based on the type of the element, similar to the tagname in the web, so it is usually not unique.

Usually, we select elements based on the class attribute, and we have to select more than one.

Of course, if you are sure that the type of interface element you are looking for is only one in the current interface, you can uniquely select it based on the class.

Use the following code

driver.find_elements_by_class_name('android.widget.TextView')

According to ACCESSIBILITY ID

The content-desc attribute of an element is used to describe the role of the element.

If the interface element to be queried has a content-desc attribute, we can use it to locate the selected element.

Use the following code

driver.find_element_by_accessibility_id('')

Xpath

Appium also supports selecting elements via Xpath.

But its reliability and performance are not as automated as Selenium Web. Because Web Automation supports Xpath by the browser, and Appium Xpath supports Appium Server.

After all, the maturity of browser products is much higher than Appium.

Of course, Xpath is a standard grammar, so the grammatical rules of expressions here are the same as the grammar of Xpath in Selenium learned before, such as

driver.find_element_by_xpath('')

note:

In selenium automation, each node name in the xpath expression is the tagname of html.

But in appium, the name of each node in the xpath expression is the value of the class attribute of the element.

For example: To select all text nodes, use the following code

driver.find_element_by_xpath('//android.widget.TextView')

Appium keyboard operation

The appium extension provides the pressKeyCode() method. This method is unique to Android.

Common keyboard operation keys:

KEYCODE_HOME Apportioning Home

KEYCODE_MENU menu key

KEYCODE_BACK return key

KEYCODE_ENTER Enter key

KEYCODE_ESCAPE ESC键

KEYCODE_CTRL_LEFT Control+Left

KEYCODE_CTRL_RIGHT Control+Right

such as:

from appium.webdriver.extensions.android.nativekey import AndroidKey

driver.pressKeyCode(AndroidKey.enter)

Guess you like

Origin blog.csdn.net/weixin_45609519/article/details/106670484