Android positioning elements and operations

Source: Internet
Author: User
Tags bitmask xpath appium

a tool for identifying common elementsUiautomator:android SDK comes with a tool, in the tools directory Monitor:android SDK comes with a tool, in the tools directory Appium Inspector:appium comes with a feature, Only Mac can use this function below is the element that is positioned with the Appium inspector below the page element crawled by Monitor Second, element positioning   1. Format: Find_element_by_ positioning method (value) Location by ID(Take Resource-id value): driver.find_element_by_id ("Com.wuba.zhuanzhuan:id/azo") can also directly use the content behind the ID driver.find_element_by_id ("Azo") positioning via class_name(Take the contents of Class) Driver.find_element_by_class_name ("Android.widget.RelativeLayout") positioning via XPath(Take XPath content) Driver.find_element_by_xpath ("//android.widget.linearlayout[1]/android.widget.xxx") Navigate through text(need to use Uiautomator's positioning method, use text content) Driver.find_elements_by_android_uiautomator ("New Uiselector (). Text (\" + Attention \ ")") Use here to note that the result of text positioning is a list, not directly click. So if you want to click on the value of the array, such as the following is the first element found Driver.find_elements_by_android_uiautomator ("New Uiselector ()." Text (\ "+ Attention \") ") [0]. Click () positioning via Css_selector (WebView)HTML pages only for WebView, inherited from Webdriver, consistent with the PC version of the UI Test Driver.find_element_by_css_selector () positioning via Link_text (WebView)Applies only to HTML pages in the WebView container, inherits from Webdriver, and is consistent with the PC version of the UI Test Driver.find_element_by_link_text () Navigate by nameThe HTML page in the Web View container can be positioned with name, native does not have a name attribute Driver.find_element_by_name () 2. Another way of positioning elements: Find_element (By,value)The Find_element_by_ method (value) is actually called find_element (by,value) to import this package: from selenium.webdriver.common.by import by For example: Locate the element mode ID AG2 one: driver.find_element_by_id ("Ag2 ") mode two: Driver.find_element (by.id, "Ag2") the advantage of this operation is that the operation's by and value can be placed directly into a tuple, and then the generic method is called to get the element result cateid= (by.id, "Ag2") Driver.find_element (*cateid). Click () the by operation can be:By.id equivalent to by_idby.class_name equivalent to By_class_nameby.xpath equivalent to by_xpathby.name equivalent to by_nameby.tag_name equivalent By_tag_nameby.cs S_selector equivalent to by_css_selectorby.link_text equivalent to By_link_text 3.find_elements_by_ Positioning method (value) returns an array of elementsThe usage is consistent with the Find_element_by_ method (value), but returns an array. You can access specific results through the index of an array, for example: navigate to multiple elements through class_name, I want to click the first element Driver.find_elements_by_class_name (" Android.widget.RelativeLayout ") [0].click () 4. Another way to return an array of elements: find_elements (By,value)Usage is consistent with find_element (By,value), but returns an array. You can access specific results through the index of an array, for example: navigate to multiple elements via class_name, I want to click the first element driver.find_elements (By.class_name, " Android.widget.RelativeLayout ") [0].click () 5. Positioning elements through elementsYou can find an element first, and then position the element Find_element_by_class_xpath ("xxx") further. Find_element_by_name ("yyy") three, element operationActions that can be performed on elements after the element is found, such as the further positioning elements described above 1.click ()Click action can also use tab to achieve click Action driver.find_element_by_id ("Com.wuba.zhuanzhuan:id/ae8"). Click () 2.clear ()Empty the input box contents driver.find_element_by_id ("Com.wuba.zhuanzhuan:id/ij"). Clear () 3.send (XX)Enter the contents of the Input box driver.find_element_by_id ("Com.wuba.zhuanzhuan:id/ij"). Send_keys ("Test content") 4.textGets the text content of the element print (Driver.find_element_by_xpath ("//android.widget.linearlayout[1]//xxx"). Text) Four, touch operation 1.driver.tap ([coordinate], continuous click time)In addition to locating the elements of the click, you can also use the tab to achieve the coordinates of the Click Driver.tap (Driver.tap ([(216,1776)],2) 2.TouchAction (Driver)Touchaction objects include (tab), press (short), move_to (slide to a coordinate) and other methods through Touchaction objects, add tap, move_to and other operations, and then perform () execution, you can unlock the screen and other functions The available events in the specification are: * Short Press
* Released (release)
* Move to (MoveTo)
* Click (TAP)
* Waiting (Wait)
* Long Press (longpress)
* Cancellation (Cancel) * Execution (Perform) example: a multiple Slide screen example: Action=touchaction (Driver) action.press (x=220,y=700). Move_to (x=840, y=700). Move_to (x=220, y=1530). Move_to (x=840, y=1530). Release (). Perform () Can wait for an operation through wait () 3.MultiAction ()//Multi-Touch via Multiaction (). Add () adds multiple touchaction operations, and finally calls perform () to perform these operations Action0 = Touchaction (). Tap (EL) Action1 = Touchaction (). Tap (EL) Multiaction (). Add (Action0). Add (Action1). Perform () 4.driver.swipe (x1, y1, x2, y2,duration) //slide from coordinates (X1,X2) to coordinates (X2,Y2), duration not required, sliding time(sliding coordinates cannot exceed the width of the screen) can be obtained through the "driver.get_window_size ()" Command window height and width, the result is {' width ': t, ' height ': 1776} An example of a mouse moving up or down is as follows: # Get screen size and height
def getsize (Driver):
x = Driver.get_window_size () [' width ']
y = driver.get_window_size () [' Height ']
return (x, y)

#屏幕向上滑动
def swipeup (driver,t=1000):
L = getsize (driver)
x1 = Int (l[0] * 0.5) #x坐标
y1 = Int (l[1] * 0.75) #起始y坐标
y2 = Int (l[1] * 0.25) #终点y坐标
Driver.swipe (x1, y1, x1, y2,t)

#屏幕向下滑动
def swipedown (driver,t=1000):
L = getsize (driver)
x1 = Int (l[0] * 0.5) #x坐标
y1 = Int (l[1] * 0.25) #起始y坐标
y2 = Int (l[1] * 0.75) #终点y坐标
Driver.swipe (x1, y1, x1, y2,t)
#屏幕向左滑动
def swipleft (driver,t):
L=getsize (Driver)
X1=int (l[0]*0.75)
Y1=int (l[1]*0.5)
X2=int (l[0]*0.05)
Driver.swipe (x1,y1,x2,y1,t)
#屏幕向右滑动
def swipright (driver,t=1000):
L=getsize (Driver)
X1=int (l[0]*0.05)
Y1=int (l[1]*0.5)
X2=int (l[0]*0.75) driver.swipe (x1,y1,x2,y1,t) #调用向下滑动的方法swipeDown (driver) five, System key events Press_keycode (Androidkeycode)Send key events For example: Click the Home button, the home button KeyCode is 3driver.press_keycode (3) key names                   description         Key values
KEYCODE_CALL&NB Sp       Dialing key      5
keycode_endcall      Hang up key      6
KeyCode _home        Press home      3
keycode_menu        Menu key      82
keycode_back        return key      4
keycode_search      Search Key & nbsp    84
keycode_camera      camera key      27
keycode_focus      &N BSP; take a photo to zoom    80
keycode_power        Power key      26
Keycode_ notification  notification key        
keycode_mute        Mic Mute key    91
keycode_volume_mute  Speaker Mute key   164
keycode_volume_up    Volume increase    24
Keycode_volume _down  volume reduction key    25
More KeyCode can view the following blog: http://blog.csdn.net/crisschan/article/details/50419963 Vi. Some of the more important operations of driver 1.reset ()Resetting the app this time the driver will be reset, equivalent to unloading the load application. So the local cache will fail Driver.reset () 2.start_activity (package name, activity name)Start an activity for an app such as: Driver.start_activity ("Com.wuba.zhuanzhuan", "./presentation.view.activity.launchactivity ") to start an activity, the activity must be activity<intent-filter> <action Android with Intent-filter in Androidmanifest.xml: Name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/></ Intent-filter> This startup activity and the driver reset () are different 3.contextsGet all contextsdriver.contexts results as follows: [' Native_app ', ' WEBVIEW_com.android.browser '] native_app:native Contextwebview The context of _com.android.browser:webview, the container for storing HTML 4.current_contextView the current Contextdriver.current_context 5.switch_to.context (context name)You cannot locate WEBVIEW content when switching contextdriver.switch_to.context ("WEBVIEW_com.wuba.zhuanzhuan") native. The content of native cannot be located in the context of WebView. So we need to switch to the corresponding context to do the operation 6.setNetworkConnection (bitmask mask)Set the network type for example: Set the network type to only open wifidriver.set_network_connection (2) The bitmask mask for the network is as follows:| Value (alias) | Data Connection | WiFi Connection | Airplane Mode | | ------------------ | ---- | ---- | ------------- || 0 (Nothing) | 0 | 0 | 0 | | 1 (Airplane mode) | 0 | 0 | 1 | | 2 (WiFi only) | 0 | 1 | 0 | | 4 (data connection only) | 1 | 0 | 0 | | 6 (Turn on all networks) | 1 | 1 | 0 | 7.scroll (start element, end Element)Driver.scroll (Origin_el,destination_el) 8. Get all the elements of the current pageDriver.page_source This can be used to determine whether an element exists, such as (Assert "Publish succeeded" in Driver.page_source) 9. Add some items that may be used when the driver startsIn fact, these are introduced in the previous launch, but some may not be noticed by the points to be listed again. These are the points I actually encountered in the test. AutoLaunch:Appium whether to start or install the app automatically, default truedesired_caps[' autoLaunch ' = ' false ' Sometimes I don't want Appium to start the app every time, want to start activity, Then this one's going to work. NoReset:Whether to reset the app state before the session. The default is falsedesired_caps[' noreset ') = ' true '   newcommandtimeout:Set timeout for new command not received, default 60s if no new command is received within 60s, Appium will automatically disconnect, and if I need a long time to do something other than driver, it may prolong the time-out of receiving a new command desired_caps[" Newcommandtimeout "]=1800 This article turn from: https://www.cnblogs.com/meitian/p/6103391.html

Android positioning elements and operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.