Appium的一点一滴

来源:互联网 发布:数据库安全 编辑:程序博客网 时间:2024/05/22 12:26

Appium的一点一滴

1、如果通过uiautomatorviewer获取app的element

(Windows)uiautomatorviewer是andriod自带的处在andriodsdk安装目录下的tools里面(uiautomatorviewer.bat)

1.1 uiautomatorviewer:分析Android应用UI组件

uiautomatorviewer:分析Android应用UI组件

uiautomator测试框架是Android SDK自带的App UI自动化测试Java库,使用uiautomator测试框架编写自动化用例前,需要使用uiautomatorviewer 识别分析android应用UI组件,文件位于sdk\tools目录下uiautomatorviewer.bat。

使用uiautomatorviewer的方法:

  • 1.连接Android 设备到开发机器,打开被测app

  • 2.运行/tools/目录下的uiautomatorviewer.bat

  • 3.点击uiautomatorviewer用户界面上的“设备截图按钮”

将鼠标悬停在左侧窗口的快照,查看被工具检测到UI控件,窗口右下方显示组件属性和右上方显示层次结构,可选择性的点击NAF按键查看测试框架不能生效的ui组件。

有多个设备连接开发机器时(模拟器或真机),通过设置ANDROID_SERIAL环境变量指定需要截图的设备。
adb devices命令查看连接中的设备编号,设置环境变量set ANDROID_SERIAL=:

  • a. 运行如下命令获取连接到电脑的设备序列号

    $ adb devices

  • b. 设置ANDROID_SERIAL为需要测试的设备序列号

    Windows:set ANDROID_SERIAL=
    Unix: eport ANDROID_SERIAL=

如果电脑只连接一个设备则无需设置ANDROID_SERIAL 环境变量。

注意:

  • 1、点击右侧上方的黄三角按钮(Toggle NAF Nodes)来查看不能被uiautomator测试工具访问到的控件。这些控件只设置了有限的属性,所以导致uiautomator无法获取到这些控件。所以你可能很难测试这些控件。这种情况下你可以要求开发者为这些控件添加必要的属性,比如 如果是ImageView或者ImageButton应该添加android:contentDescription属性。
  • 2、由于uiautomator 工具依赖Android设备的可访问行来获取UI控件,所以这不是非常重要的。要支持uiautomator 工具需要一下条件:
    使用android:contentDescription属性给 ImageButton, ImageView, CheckBox和其他控件设置标签。使用android:hint 属性来标记EditText 控件,而不是使用里面的文本(文本内容用户是可以修改的)。
    对于用来提供操作视觉反馈的UI(文本或者图标),都添加一个android:hint 属性来识别。确保所有用户可操作的界面元素都可以通过方向控制键选中(例如轨迹球)。
    通过uiautomatorviewer 工具来确保所有的UI元素都可以被测试工具访问到。还可以通过“辅助功能”(在设置界面)中的“TalkBack”等服务来测试UI的可访问性。

2、 Appium 和Android真机

2.1打开手机的USB调试模式

每种手机都不一样,因此大家自便

2.2连接手机到电脑

adb devices -l查看UDID

2.3 启动Appium服务

根据查到的UDID启动appium服务,运行命令:

#>appium -a 127.0.0.1 -p 4723  –U  S85P5DJJJNFATO4L  --no-reset

-U 参数后面跟的一串字符就是手机的UDID,这个是通过第二步查到的。

3、获取Android Activity三种方法

3.1 源代码

要源代码,查找main activity
找到manifest文件

带有luancher的activity就是启动acitivity

3.2 反编译

这里将用到2个工具,分别是dex2jar和jd-gui。
工具包里的ContactManager.apk为例,简单介绍一下反编译的流程。

  • 1,重命名ContactManager.apk为ContactManager.zip并解压得到文件classes.dex;
  • 2,解压dex2jar-0.0.9.15.zip,并从命令行进入该文件夹;
  • 3,运行命令 d2j-dex2jar.bat path_to\classes.dex 在当前文件夹下得到classes-dex2jar.jar;
  • 4,解压jd-gui-0.3.6.windows.zip得到文件jd-gui.exe;
  • 5,使用jd-gui.exe打开classes-dex2jar.jar;
    如下图ContactManager就是待测app的main activity。

3.3 Log

  • a、启动待测apk
  • b、开启日志输出:adb logcat>D:/log.txt
  • c、关闭日志输出:ctrl+c
  • d、查看日志 找寻:

    Displayed com.mm.android.hsy/.ui.LoginActivity: +3s859ms

    appPackage = com.mm.android.hsy

    appActivity = .ui.LoginActivity

4 Mobile JSON Wire Protocol

DRAFT

4.1Introduction


This specification is designed to extend the JSON Wire
Protocol (JSONWP),
a W3C working draft for web browser automation. The JSONWP has been greatly
successful for that purpose. The need for automation of native and hybrid
mobile applications can be met by the extension of the JSONWP, which already
has a proven basic automation framework (architecture, interaction model,
etc…).

The initial details of this specification were worked out at a series of
meetings held in Mozilla’s offices in London in August of 2013. The
participants were:

  • David Burns (Mozilla) - website - twitter
  • Dominik Dary (eBay) - website - twitter
  • Jonathan Lipps (Sauce Labs) - website - twitter
  • Jason ? (Facebook) - website - twitter
  • François Reynaud (eBay) - website - twitter
  • Simon Stewart (Facebook) - website - twitter
  • Santiago Suarez Ordoñez (Sauce Labs) - website - twitter

4.2 Sessions


Sessions work just like WebDriver: you POST to /session and receive a sessionId
as a response if the server can give you one, at which point you can send
further automation commands. If the server can’t start a session, for example
if another session is running and only one session can be handled at a time,
the server must return the appropriate 500 response. Sessions are ended with
a DELETE to /session/:id as per the original WebDriver spec.

The server may but is not required to launch the AUT or a device/simulator in
the process of creating a session. It may but is not required to perform some
kind of cleaning or resetting of the AUT in order to provide a clean test
environment. It may but is not required to stop the running AUT at the session
end. It may but is not required to remove the AUT from the device or otherwise
reset the device state after the session is complete. In general, it is the
responsibility of the user to manage the test environment; it is not a part of
this specification. But a server conforming to this specification may by other
means provide that functionality as a convenience.

4.3 Desired Capabilities


New desired capability keys:

  • automationName: specific automation tool, e.g., appium, ios-driver, selendroid
  • platformName: platform to automate, e.g., Android, iOS
  • platformVersion: platform version e.g., 4.3 (for Android) or 6.1 (for iOS)
  • deviceName: specific device names including version information, e.g., Nexus 4, iPhone 4S, iPhone Simulator, iPad Mini
  • app (optional): path or uri to AUT
  • browserName (optional): web browser to automate as a webdriver session, e.g., Safari, Chrome

4.4 Locator Strategies


The following locator strategies must be supported for non-HTML-based platforms:

  • class name: a string representing the UI element type for a given platform, e.g., UIAPickerWheel for iOS or android.widget.Button for Android
    • These should exactly match the class names given by the underlying automation frameworks
  • accessibility id: a string representing the accessibility id or label attached to a given element, e.g., for iOS the accessibility identifier and for Android the content-description
  • xpath: a valid xpath string applied to the XML document that would be retrieved using the page source command

The following locator strategies may be supported, depending on the automation
platform:

  • id: a string corresponding to a resource ID
  • -android uiautomator: a string corresponding to a recursive element search using the UiAutomator library (Android only)
    • TODO: specify this
  • -ios uiautomation: a string corresponding to a recursive element search using the UIAutomation library (iOS-only)
  • TODO: figure out whether server should report support of these strategies

If automating a mobile browser in WebDriver mode, or a platform that uses HTML
as its element hierarchy, the usual array of WebDriver commands must be
supported instead, with their usual semantics.

4.5 Page Source


All platforms must respond to the GET source command with an XML (or HTML in
the case of HTML-based platforms) document representing the UI hierarchy. The
precise structure of the document may differ from platform to platform. Schemas
that must be followed for iOS and Android automation are as follows:

TODO: get together schemas for UIAutomation (iOS), Instruments (Android), and
UiAutomator (Android).

The elements in these documents may be augmented with such attributes as, for
example, ids, in order to support internal behaviors.

4.6 Touch Gestures


All platforms must adopt the Multi-Action API pioneered by Mozilla. In some
cases it will not be possible to support the full range of gestures potentially
described by this API on a given platform. In this case, the platform should
respond with a 500 when it cannot faithfully render the requested gesture.

TODO: show what the gestures API actually looks like in terms of server
endpoints that must be supported.

4.7 Device Modes


Devices have various states of network connectivity. In order to control
those states we have the following endpoints:

  • GET /session/:sessionid/network_connection
    • returns ConnectionType
  • POST /session/:sessionid/network_connection
    • accepts a ConnectionType
    • returns ConnectionType

Setting the network connection in the POST returns the ConnectionType because
the device might not be capable of the network connection type requested.

The remote end MUST reply with the capability “networkConnectionEnabled”

4.8 ConnectionType -

Is a bit mask that should be translated to an integer value when serialized.

Value (Alias) | Data | Wifi | Airplane Mode

1 (Airplane Mode) | 0 | 0 | 1
6 (All network on) | 1 | 1 | 0
4 (Data only) | 1 | 0 | 0
2 (Wifi only) | 0 | 1 | 0
0 (None) | 0 | 0 | 0

Example payload for setting “Airplane Mode”:

{ "name": "network_connection", "parameters": { "type": 1 } }

Data is the upper bits since in the future we may want to support setting
certain types of Data the device is capable of. For example 3G, 4G, LTE.

4.9 Other Device Features


Mobile devices have a variety of sensors and input methods. These are automated
as follows:

  • The virtual keyboard: use sendKeys
  • acceleromator: TODO @mdas is working on this
  • geolocation: use regular webdriver endpoints
  • rotation (different from orientation): TODO
  • battery level: not in spec, perhaps exposed via executeScript
  • network speed: not in spec, perhaps exposed via executeScript

4.10 WebViews and Other Contexts


One common feature of mobile platforms is the ability to embed a chromeless
webbrowser inside of a ‘native’ application. These are called ‘webviews’, and,
if possible, a server for a given platform should implement support for
automating the webview using the full, regular, WebDriver API.

This creates a situation where there are two potential contexts for automation
in a given AUT: the native layer and the webview layer. If providing webview
support, the server must have the following endpoints:

  • GET /session/:sessionid/contexts
    • returns an array of strings representing available contexts, e.g.
      ‘WEBVIEW’, or ‘NATIVE’
  • GET /session/:sessionid/context
    • returns one of:
    • a string representing the current context
    • null, representing “no context”
  • POST /session/:sessionid/context
    • accepts one of:
    • a string representing an available context

The first endpoint must return a possibly-empty array of strings. Each string
must be the arbitrary name of an available context, e.g., one of possibly
multiple webviews. The second must interpret the body of the request as the
name of an available context. If that context is not found, a NoSuchContext
error (code 35) must be returned. If the context is available, the server must
switch automation to that context, such that all subsequent commands are taken
to apply to that context. If the body of the POST is null, the server must
return to the original context.

If a server receives a request at an endpoint which is valid in some context
but not the currently active context (for example if a user calls
driver.get() in a native context instead of a webview context), the server
must respond with an InvalidContentException.

4.11 Waiting for Conditions


The server must respond to the management commands for implicit wait timeouts,
such that when a user sets an implicit wait timeout and tries to find an
element(s), the server keeps trying to find the element(s) until that timeout
expires, rather than responding with the first failure to find the element(s).

TODO: figure out what the serversidewait implementation will be and talk about
it.

5 UiSelector

This class provides the mechanism for tests to describe the UI elements they intend to target. A UI element has many properties associated with it such as text value, content-description, class name and multiple state information like selected, enabled, checked etc. Additionally UiSelector allows targeting of UI elements within a specific display hierarchies to distinguish similar elements based in the hierarchies they’re in.

5.1 Summary

  • Public Constructors

    UiSelector()
  • Public Methods

    - UiSelector    checked(boolean val)Set the search criteria to match widgets that are currently checked (usually for checkboxes).- UiSelector    childSelector(UiSelector selector)Adds a child UiSelector criteria to this selector.- UiSelector    className(String className)Set the search criteria to match the class property for a widget (for example, "android.widget.Button").- <T> UiSelector    className(Class<T> type)Set the search criteria to match the class property for a widget (for example, "android.widget.Button").- UiSelector    classNameMatches(String regex)Set the search criteria to match the class property for a widget (for example, "android.widget.Button").- UiSelector    clickable(boolean val)Set the search criteria to match widgets that are clickable.- UiSelector    description(String desc)Set the search criteria to match the content-description property for a widget.- UiSelector    descriptionContains(String desc)Set the search criteria to match the content-description property for a widget.- UiSelector    descriptionMatches(String regex)Set the search criteria to match the content-description property for a widget.- UiSelector    descriptionStartsWith(String desc)Set the search criteria to match the content-description property for a widget.- UiSelector    enabled(boolean val)Set the search criteria to match widgets that are enabled.- UiSelector    focusable(boolean val)Set the search criteria to match widgets that are focusable.- UiSelector    focused(boolean val)Set the search criteria to match widgets that have focus.- UiSelector    fromParent(UiSelector selector)Adds a child UiSelector criteria to this selector which is used to start search from the parent widget.- UiSelector    index(int index)Set the search criteria to match the widget by its node index in the layout hierarchy.- UiSelector    instance(int instance)Set the search criteria to match the widget by its instance number.- UiSelector    longClickable(boolean val)Set the search criteria to match widgets that are long-clickable.- UiSelector    packageName(String name)Set the search criteria to match the package name of the application that contains the widget.- UiSelector    packageNameMatches(String regex)Set the search criteria to match the package name of the application that contains the widget.- UiSelector    scrollable(boolean val)Set the search criteria to match widgets that are scrollable.- UiSelector    selected(boolean val)Set the search criteria to match widgets that are currently selected.- UiSelector    text(String text)Set the search criteria to match the visible text displayed for a widget (for example, the text label to launch an app).- UiSelector    textContains(String text)Set the search criteria to match the visible text displayed for a widget (for example, the text label to launch an app).- UiSelector    textMatches(String regex)Set the search criteria to match the visible text displayed for a widget (for example, the text label to launch an app).- UiSelector    textStartsWith(String text)Text property is usually the widget's visible text on the display.- String    toString()
  • Inherited Methods

5.2 Public Constructors

public UiSelector ()

5.3 Public Methods

public UiSelector checked (boolean val)

Set the search criteria to match widgets that are currently checked (usually for checkboxes). Typically, using this search criteria alone is not useful. You should also include additional criteria, such as text, content-description, or the class name for a widget. If no other search criteria is specified, and there is more than one matching widget, the first widget in the tree is selected.

Parameters

val Value to match

Returns

UiSelector with the specified search criteria

public UiSelector childSelector (UiSelector selector)

Adds a child UiSelector criteria to this selector. Use this selector to narrow the search scope to child widgets under a specific parent widget.

Returns

UiSelector with this added search criterion

public UiSelector className (String className)

Set the search criteria to match the class property for a widget (for example, “android.widget.Button”).

Parameters

className Value to match

Returns

UiSelector with the specified search criteria

public UiSelector className (Class<T> type)

Set the search criteria to match the class property for a widget (for example, “android.widget.Button”).

Returns

UiSelector with the specified search criteria

Since

Android API Level 17

public UiSelector classNameMatches (String regex)

Set the search criteria to match the class property for a widget (for example, “android.widget.Button”).

Returns

UiSelector with the specified search criteria

Since

Android API Level 17

public UiSelector clickable (boolean val)

Set the search criteria to match widgets that are clickable. Typically, using this search criteria alone is not useful. You should also include additional criteria, such as text, content-description, or the class name for a widget. If no other search criteria is specified, and there is more than one matching widget, the first widget in the tree is selected.

Parameters

val Value to match

Returns

UiSelector with the specified search criteria

public UiSelector description (String desc)

Set the search criteria to match the content-description property for a widget. The content-description is typically used by the Android Accessibility framework to provide an audio prompt for the widget when the widget is selected. The content-description for the widget must match exactly with the string in your input argument. Matching is case-sensitive.

Parameters

desc Value to match

Returns

UiSelector with the specified search criteria

public UiSelector descriptionContains (String desc)

Set the search criteria to match the content-description property for a widget. The content-description is typically used by the Android Accessibility framework to provide an audio prompt for the widget when the widget is selected. The content-description for the widget must contain the string in your input argument. Matching is case-insensitive.

Parameters

desc Value to match

Returns

UiSelector with the specified search criteria

public UiSelector descriptionMatches (String regex)

Set the search criteria to match the content-description property for a widget. The content-description is typically used by the Android Accessibility framework to provide an audio prompt for the widget when the widget is selected. The content-description for the widget must match exactly with the string in your input argument.

Returns

UiSelector with the specified search criteria

Since

Android API Level 17

public UiSelector descriptionStartsWith (String desc)

Set the search criteria to match the content-description property for a widget. The content-description is typically used by the Android Accessibility framework to provide an audio prompt for the widget when the widget is selected. The content-description for the widget must start with the string in your input argument. Matching is case-insensitive.

Parameters

desc Value to match

Returns

UiSelector with the specified search criteria

public UiSelector enabled (boolean val)

Set the search criteria to match widgets that are enabled. Typically, using this search criteria alone is not useful. You should also include additional criteria, such as text, content-description, or the class name for a widget. If no other search criteria is specified, and there is more than one matching widget, the first widget in the tree is selected.

Parameters

val Value to match

Returns

UiSelector with the specified search criteria

public UiSelector focusable (boolean val)

Set the search criteria to match widgets that are focusable. Typically, using this search criteria alone is not useful. You should also include additional criteria, such as text, content-description, or the class name for a widget. If no other search criteria is specified, and there is more than one matching widget, the first widget in the tree is selected.

Parameters

val Value to match

Returns

UiSelector with the specified search criteria

public UiSelector focused (boolean val)

Set the search criteria to match widgets that have focus. Typically, using this search criteria alone is not useful. You should also include additional criteria, such as text, content-description, or the class name for a widget. If no other search criteria is specified, and there is more than one matching widget, the first widget in the tree is selected.

Parameters

val Value to match

Returns

UiSelector with the specified search criteria

public UiSelector fromParent (UiSelector selector)

Adds a child UiSelector criteria to this selector which is used to start search from the parent widget. Use this selector to narrow the search scope to sibling widgets as well all child widgets under a parent.

Returns

UiSelector with this added search criterion

public UiSelector index (int index)

Set the search criteria to match the widget by its node index in the layout hierarchy. The index value must be 0 or greater. Using the index can be unreliable and should only be used as a last resort for matching. Instead, consider using the instance(int) method.

Parameters

index Value to match

Returns

UiSelector with the specified search criteria

public UiSelector instance (int instance)

Set the search criteria to match the widget by its instance number. The instance value must be 0 or greater, where the first instance is 0. For example, to simulate a user click on the third image that is enabled in a UI screen, you could specify a a search criteria where the instance is 2, the className(String) matches the image widget class, and enabled(boolean) is true. The code would look like this: new UiSelector().className(“android.widget.ImageView”) .enabled(true).instance(2);

Parameters

instance Value to match

Returns

UiSelector with the specified search criteria

public UiSelector longClickable (boolean val)

Set the search criteria to match widgets that are long-clickable. Typically, using this search criteria alone is not useful. You should also include additional criteria, such as text, content-description, or the class name for a widget. If no other search criteria is specified, and there is more than one matching widget, the first widget in the tree is selected.

Parameters

val Value to match

Returns

UiSelector with the specified search criteria

Since

Android API Level 17

public UiSelector packageName (String name)

Set the search criteria to match the package name of the application that contains the widget.

Parameters

name Value to match

Returns

UiSelector with the specified search criteria

public UiSelector packageNameMatches (String regex)

Set the search criteria to match the package name of the application that contains the widget.

Returns

UiSelector with the specified search criteria

Since

Android API Level 17

public UiSelector scrollable (boolean val)

Set the search criteria to match widgets that are scrollable. Typically, using this search criteria alone is not useful. You should also include additional criteria, such as text, content-description, or the class name for a widget. If no other search criteria is specified, and there is more than one matching widget, the first widget in the tree is selected.

Parameters

val Value to match

Returns

UiSelector with the specified search criteria

public UiSelector selected (boolean val)

Set the search criteria to match widgets that are currently selected. Typically, using this search criteria alone is not useful. You should also include additional criteria, such as text, content-description, or the class name for a widget. If no other search criteria is specified, and there is more than one matching widget, the first widget in the tree is selected.

Parameters

val Value to match

Returns

UiSelector with the specified search criteria

public UiSelector text (String text)

Set the search criteria to match the visible text displayed for a widget (for example, the text label to launch an app). The text for the widget must match exactly with the string in your input argument. Matching is case-sensitive.

Parameters

text Value to match

Returns

UiSelector with the specified search criteria

public UiSelector textContains (String text)

Set the search criteria to match the visible text displayed for a widget (for example, the text label to launch an app). The text for the widget must contain the string in your input argument. Matching is case-sensitive.

Parameters

text Value to match
Returns

UiSelector with the specified search criteria

public UiSelector textMatches (String regex)

Set the search criteria to match the visible text displayed for a widget (for example, the text label to launch an app). The text for the widget must match exactly with the string in your input argument.

Returns

UiSelector with the specified search criteria

Since

Android API Level 17

public UiSelector textStartsWith (String text)

Text property is usually the widget’s visible text on the display. Adding this to the search criteria indicates that the search performed should match a widget with text value starting with the text parameter. The matching will be case-insensitive.

Returns

UiSelector with this added search criterion

public String toString ()

6 屏幕滑动屏幕坐标获取

java方式获取

DisplayMetrics metrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics;

用Python是不行的。没这个接口获取。

获取方法只能这样:

7、 HybirdApp中webView元素如何查看(Appium)

7.1 Andriod

准备工作:

  • chrome47.0以上版本
  • 翻墙软件
  • 手机打开开发者模式下的通过usb调试与电脑相连
    工作步骤:
  • 打开chrome
  • 输入chrome://inspect/#devices
  • 再打开的页面中勾选 discover USB devices
  • 在对应列表中选择测试网络即可(过程需要翻墙)

7.2 IOS

准备工作:
- mac笔记本
- apple手机在设置中找到safari,进入后在最后面选择高级,然后打开web检查器后一电脑相连
- 打开mac笔记本的Safari的对应菜单下就可以操作手机上的webview了

8 XPATH的match、contains

8.1 matches()

self.driver.find_element_by_xpath('//div[matches(@class,"col-xs-6")')

8.2 contains()

self.driver.find_element_by_xpath('//div[contains(@class,"col-xs-6") and contains(@class,"cmb_film")]')

8.3 Xpath常用的正则符合

0 0