APP mobile terminal test advanced

Posted by BizLab on Mon, 27 Dec 2021 00:00:02 +0100

8, APP element event operation API

8.1. Pre code

from appium import webdriver
# server startup parameters
desired_caps = {}
# Equipment information
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
# app information
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

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

8.2. swip slip event

⚠️ Sliding from one coordinate position to another can only be between two points

  • Method: swing (start_x, start_y, end_x, end_y, duration = none)
  • Parameters:
    1.start_x: Starting point X-axis coordinate
    2.start_y: Y-axis coordinates of starting point
    3.end_x: End X-axis coordinates
    4.end_y. End point: Y axis coordinates
    5.duration: total duration of sliding operation, unit: ms
    • Business scenario:
      1. Enter settings
      2. Slide from coordinate (148659) to coordinate (148248)
  • Code implementation:
    # Sliding has no duration
    driver.swipe(188,659,148,248)
    # Sliding lasts for 5 seconds
    driver.swipe(188,659,148,248,5000)
    

8.3. scroll slide event

⚠️ Slide from one element to another until the page stops automatically

  • Method: scroll(origin_el, destination_el)
  • Parameters:
    1.origin_el: element at the beginning of the slide
    2.destination_el: element at the end of the slide
  • Business scenario:
    1. Enter the setting page
    2. Simulate the up sliding operation of the finger from the storage menu position to the WLAN menu position
  • Code implementation:
    # Navigate to the storage menu bar
    el1 = driver.find_element_by_xpath("//*[contains(@text, 'store')] ")
    # Navigate to the WLAN menu bar
    el2 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
    # Perform sliding operation
    driver.scroll(el1,el2)
    

8.4. drag drag event

⚠️ Slide from one element to another, and the second element replaces the original screen position of the first element

  • Method: drag_and_drop(origin_el, destination_el)
  • Parameters:
    1.origin_el: element at the beginning of the slide
    2.destination_el: element at the end of the slide
  • Business scenario:
    1. Enter the setting page
    2. Slide the storage menu to the WLAN menu bar with an analog finger
  • Code implementation:
    # Navigate to the storage menu bar
    el1 = driver.find_element_by_xpath("//*[contains(@text, 'store')] ")
    # Navigate to the WLAN menu bar
    el2 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
    # Perform sliding operation
    driver.drag_and_drop(el1,el2)
    

8.5. Application put in background event

APP is placed in the background to simulate hot start

  • Method: background_app(seconds)
  • Parameters:
    1.seconds: time to stay in the background, unit: seconds
  • Business scenario:
    1. Enter the setting page
    2. Place the APP in the background for 5s
  • Code implementation:
    driver.background_app(5)
    
  • effect:
    app Put in background 5 s After, show the current page again
    

9, APP analog gesture advanced operation

TouchAction is an auxiliary class of AppiumDriver. It mainly aims at gesture operations, such as sliding, long pressing, dragging, etc. the principle is to put a series of actions in a chain and send them to the server. After receiving the chain, the server parses each action and executes it one by one.

9.1. Pre code

from appium import webdriver
# server startup parameters
desired_caps = {}
# Equipment information
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
# app information
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

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

⚠️ All gestures are executed by the function

9.2. Finger tapping operation

Simulate a finger tap on the screen

  • Method: tap(element=None, x=None, y=None)
  • Method: perform() # sends a command to the server to execute the operation
  • Parameters:
    1.element: the element to be located
    2.x: relative to the coordinates of the upper left corner of the element, the x-axis coordinates of the element are usually used
    3.y: the Y coordinate of the element is usually used
  • Business scenario:
    1. Enter settings
    2. Click WLAN option
  • Code implementation:
    # Tap the screen by element positioning
    el = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
    TouchAction(driver).tap(el).perform()
    
    # Tap the screen through the coordinate mode. WLAN coordinates: x=155,y=250
    # TouchAction(driver).tap(x=155,y=250).perform()
    

9.3. Finger press operation

Analog fingers press the screen, press to leave

  • Method: press(el=None, x=None, y=None)
  • Method: release() # ends the action and leaves the screen with your finger
  • Parameters:
    1.element: the element to be located
    2.x: the x-axis coordinate of the element is usually used
    3.y: the Y coordinate of the element is usually used
  • Business scenario:
    1. Enter settings
    2. Click WLAN option
  • Code implementation:
    # Press the screen by element positioning
    el = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
    TouchAction(driver).press(el).release().perform()
    
    # Press the screen through the coordinate mode. WLAN coordinates: x=155,y=250
    # TouchAction(driver).press(x=155,y=250).release().perform()
    

9.4. Wait for operation

  • Method: wait(ms=0)
  • Parameters:
    ms: number of milliseconds paused
  • Business scenario:
    1. Enter settings
    2. Click WLAN option
    3. Long press the WiredSSID option for 5 seconds
  • Code implementation:
    # Click WLAN
    driver.find_element_by_xpath("//*[contains(@text,'WLAN')]").click()
    # Navigate to WiredSSID
    el =driver.find_element_by_id("android:id/title")
    # Long press the element by element positioning
    TouchAction(driver).press(el).wait(5000).perform()
    
    # Simulate long press elements by coordinates
    # Add wait (with long press) / do not add wait (without long press effect)
    # TouchAction(driver).press(x=770,y=667).wait(5000).release().perform()
    

9.5. Long finger press operation

The analog mobile phone presses the screen for a period of time, and pressing it will correspond to leaving

  • Method: long_press(el=None, x=None, y=None, duration=1000)
  • Parameters:
    1.element: the element to be located
    2.x: the x-axis coordinate of the element is usually used
    3.y: the Y coordinate of the element is usually used
    4.duration: duration, which is 1000ms by default
  • Code implementation:
    # Click WLAN
    driver.find_element_by_xpath("//*[contains(@text,'WLAN')]").click()
    # Navigate to WiredSSID
    el =driver.find_element_by_id("android:id/title")
    # Long press the element by element positioning
    TouchAction(driver).long_press(el,duration=5000).release().perform()
    
    # Long press the element in the coordinate mode. WiredSSID coordinates: x=770,y=667
    # Add wait (with long press) / do not add wait (without long press effect)
    # TouchAction(driver).long_press(x=770,y=667).perform()
    

9.6. Finger movement operation

Simulate the sliding operation of the mobile phone

  • Method: move_to(el=None, x=None, y=None)
  • Parameters:
    1.el: positioned element
    2.x: X-axis offset relative to the previous element
    3.y: Y-axis offset relative to the previous element
  • Business scenario 1:
    1. Enter settings
    2. Slide the screen up
  • Code implementation:
    # Navigate to storage
    el = driver.find_element_by_xpath("//*[contains(@text, 'store')] ")
    # Navigate to more
    el1 = driver.find_element_by_xpath("//*[contains(@text, 'more')] ")
    # Element mode sliding
    TouchAction(driver).press(el).move_to(el1).release().perform()
    # Coordinate sliding
    # TouchAction(driver).press(x=240,y=600).wait(100).move_to(x=240,y=100).release().perform()
    # Note that press connects to a move_to actually calls the swip method, which can be queried in the log without giving relative coordinates.
    
  • Business scenario 2:
    1. Enter settings
    2. Slide the screen up to see the "security" option
    3. Access to security
    4. Click screen lock mode
    5. Click on the pattern
    6. Draw patterns
  • Code implementation:
    # Navigate to WLAN
    el1 = driver.find_element_by_xpath("//*[contains(@text,'WLAN')]")
    # Navigate to storage
    el2 = driver.find_element_by_xpath("//*[contains(@text, 'store')] ")
    # Slide storage to WLAN
    driver.drag_and_drop(el2,el1)
    # Navigate to user
    el3 = driver.find_element_by_xpath("//*[contains(@text, 'user')] ")
    # Note that drag is used this time_ and_ In the drop method, the incoming "storage location" still uses its original position on the screen, so the user can slide it up only when the storage slides to the user. Otherwise, the "storage location" needs to be relocated
    # Slide user position on storage
    driver.drag_and_drop(el2,el3)
    # Click the security button
    driver.find_element_by_xpath("//*[contains(@text, 'safe')] ". click()
    # Click the screen lock mode button
    driver.find_element_by_xpath("//*[contains(@text, 'screen locked')] ". click()
    # Click the pattern button
    driver.find_element_by_xpath("//*[contains(@text, 'pattern')] ". click()
    # Draw four coordinates of the pattern: 1: (244967) 2: (723967) 3: (7231442) 4: (2441916)
    TouchAction(driver).press(x=244,y=967).wait(100).move_to(x=479,y=0).wait(100)\
              .move_to(x=0,y=475).wait(100).move_to(x=-479,y=474).release().perform()
    

10, Mobile operation API

Operate some common setting functions of the mobile phone

10.1. Pre code

from appium import webdriver
# server startup parameters
desired_caps = {}
# Equipment information
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
# app information
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

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

10.2. Get mobile time

  • Method: device_time
  • Code implementation:
    # Get the time of the current phone
    print(driver.device_time)
    
  • Execution results:
    Wed Dec 27 08:52:45 EST 2017
    

10.3. Get the width and height of the phone

Get the width and height of the mobile phone. You can do some coordinate operations according to the width and height

  • Method: get_window_size()
  • Code implementation:
print(driver.get_window_size())
  • Execution results:
{'height': 800, 'width': 480}

10.4. Send key to device

Simulate the operation of system key values, such as home key, volume key, return key, etc.

  • Parameters:
    keycode: the key code sent to the device
    metastate: meta information about the key code to be sent, which is generally the default value
  • Business scenario:
    1. Open Settings
    2. Press the volume increase key several times
  • Code implementation:
    for i in range(3):
        driver.keyevent(24)
    

Appium -- the keycode key value of Android

adb command use
adb shell input keyevent XX(EventCode) # enter the corresponding key value
adb shell input text "www.baidu.com "# send text to browser

EventCodeKeyEventEventName
0KEYCODE_UNKNOWNUnknown key
1KEYCODE_SOFT_LEFTLeft key
2KEYCODE_SOFT_RIGHTRight click
3KEYCODE_HOMEHome key
4KEYCODE_BACKReturn key
5KEYCODE_CALLDialing key
6KEYCODE_ENDCALLHang up key
7KEYCODE_0Key "0"
8KEYCODE_1Key "1"
9KEYCODE_2Key "2"
10KEYCODE_3Key "3"
11KEYCODE_4Key "4"
12KEYCODE_5Key "5"
13KEYCODE_6Key "6"
14KEYCODE_7Key "7"
15KEYCODE_8Key "8"
16KEYCODE_9Key "9"
17KEYCODE_STARKey "*"
18KEYCODE_POUNDPress "#"
19KEYCODE_DPAD_UPNavigation key up
20KEYCODE_DPAD_DOWNNavigation key down
21KEYCODE_DPAD_LEFTNavigation key left
22KEYCODE_DPAD_RIGHTNavigation key right
23KEYCODE_DPAD_CENTERNavigation key OK
24KEYCODE_VOLUME_UPVolume key plus
25KEYCODE_VOLUME_DOWNVolume key minus
26KEYCODE_POWERPower key
27KEYCODE_CAMERACamera key
28KEYCODE_CLEARClear key
29KEYCODE_AKey "A"
30KEYCODE_BKey "B"
31KEYCODE_CKey "C"
32KEYCODE_DKey "D"
33KEYCODE_EKey "E"
34KEYCODE_FKey "F"
35KEYCODE_GKey "G"
36KEYCODE_HKey "H"
37KEYCODE_IKey "I"
38KEYCODE_JKey "J"
39KEYCODE_KKey "K"
40KEYCODE_LKey "L"
41KEYCODE_MKey "M"
42KEYCODE_NKey "N"
43KEYCODE_OKey "O"
44KEYCODE_PPress "P"
45KEYCODE_QKey "Q"
46KEYCODE_RKey "R"
47KEYCODE_SKey "S"
48KEYCODE_TKey "T"
49KEYCODE_UKey "U"
50KEYCODE_VKey "V"
51KEYCODE_WKey "W"
52KEYCODE_XKey "X"
53KEYCODE_YKey "Y"
54KEYCODE_ZKey "Z"
55KEYCODE_COMMAPress the key
56KEYCODE_PERIODKey '
57KEYCODE_ALT_LEFTAlt+Left
58KEYCODE_ALT_RIGHTAlt+Right
59KEYCODE_SHIFT_LEFTShift+Left
60KEYCODE_SHIFT_RIGHTShift+Left
61KEYCODE_TABTab key
62KEYCODE_SPACESpace bar
63KEYCODE_SYMSelect input method
64KEYCODE_EXPLORERbrowser
65KEYCODE_ENVELOPEmail
66KEYCODE_ENTERenter key
67KEYCODE_DELBackspace key
68KEYCODE_GRAVEKey ` `
69KEYCODE_MINUSKey '-'
70KEYCODE_EQUALSKey '='
71KEYCODE_LEFT_BRACKETKey '['
72KEYCODE_RIGHT_BRACKETKey ']'
73KEYCODE_BACKSLASHKey '\'
74KEYCODE_SEMICOLONKey ','
75KEYCODE_APOSTROPHEKey '' (single quotation mark)
76KEYCODE_SLASHKey '/'
77KEYCODE_ATKey '@'
78KEYCODE_NUMKey Number modifier
79KEYCODE_HEADSETHOOKPress the key Headset Hook
80KEYCODE_FOCUSCamera focus key
81KEYCODE_PLUSKey '+'
82KEYCODE_MENUMenu key
83KEYCODE_NOTIFICATIONNotification key
84KEYCODE_SEARCHSearch key
85TAG_LAST_KEYCODE

10.5. Operation mobile phone notification bar

Open the notification bar of the mobile phone to get the relevant information and element operations of the notification bar

  • Method: open_notifications()
  • Business scenario:
    1. Startup settings
    2. Open the notification bar
  • Code implementation:
driver.open_notifications()

10.6. Get the current network of mobile phone

Get the currently connected network of the mobile phone

  • Method: network_connection
  • Business scenario: get the current network mode of the mobile phone
  • Code implementation:
print(driver.network_connection)
  • Execution results:
6
Value (Alias)DataWifiAirplane Mode
0 (None)000
1 (Airplane Mode)001
2 (Wifi only)010
4 (Data only)100
6 (All network on)110

10.7. Set up mobile network

Change the network mode of the mobile phone and simulate the test cases under special network conditions

  • Method: set_network_connection(connectionType)
  • Parameters:
    connectionType: the network type to be set
  • Business scenario:
    1. Startup settings
    2. Set the mobile network to flight mode
  • Code implementation:
driver.set_network_connection(1)

10.8. Screenshot of mobile phone

Capture the current screen of the mobile phone and save the picture in the specified format to the set position

  • Method: get_screenshot_as_file(filename)
  • Parameters:
    filename: the image in the specified format under the specified path
  • Business scenario:
    1. Open the settings page
    2. Save the screenshot to the current directory and name it screen png
  • Code implementation:
    import os
    driver.get_screenshot_as_file(os.getcwd() + os.sep + './screen.png')
    
  • Execution results:
    Screen. Will be generated in the current directory PNG file

11, Script recording

11.1. Cognitive interface

11.2. Click the start recording button to start recording the script

  1. Click the first "select element" button on the left of the top navigation bar to select the search bar, and then click the "click" button on the right to operate (the recording process is to first select the APP element on the left of the recording window, and then record the operation mode on the right of the window).

  2. After recording, click the "stop recording" button, and the operation code will be displayed synchronously in the upper right corner of the recording window. Select the code type, convert the template code into a formal code, and copy the code to pychart.

  3. After the code is copied to pycharm, it is best to add the waiting time at each operation or page switching, otherwise it will not arrive at the next page in time due to network speed, APP design problems or other reasons, resulting in appium unable to obtain page elements and failure to locate elements, and an error will be reported when pycharm runs. After editing the code, click pycharm's run button, and appium will automatically run the script on the mobile phone.