An Appium test environment setup test

What is Appium

Appiun is a set of test framework, which adopts a typical C/S architecture.
Appium can easily realize automatic testing of mobile phone applications and reduce the investment of manual testing.
Appium is a framework, which has nothing to do with the specific language used. You can have many choices, Python/Java/Php and so on.
Use code to clearly describe what you want to do, and leave the rest to Appium to do.
She is your right-hand man. She will finish what you have told according to your will and tell you the results you care about.

What is needed to build Appium

Appium is written in Node.js, so you need to install Node.js to provide her with a stage for display.

The stage is set up. Now it's the actor Appium. She has two faces, front and back. The front desk is the Appium desktop version, and the behind the scenes is the server version.
The desktop version includes the server version, and provides an additional interface for you to issue instructions to operate the mobile phone and view the UI layout information of the mobile application. Therefore, there are two ways to operate the mobile phone. You can specify the actions to be completed through the parameters of the interface, and you can also directly use the script. No matter which way you use, you will finally give these inputs to the Appium server for processing. She will convert them into corresponding commands and send them to the Bootstrap.jar on the mobile phone through UIAutomator to complete your desired operation and return the results in the opposite direction. So the Appium server is essential. As for the desktop version, it depends on your needs. It is recommended to install the desktop version, because you can easily view the UI resources of the mobile application. When you write the test script, these are the resources you use to operate the application.

The purpose is to write scripts to manipulate mobile applications. After you choose the programming language to use, you need to install the corresponding running environment and the encapsulated Appium development package.

When all is said and done, Appium is just an HTTP server. It sits and waits for connections from a client, Which then instruments appium what kind of session to start and what kind of automation behaviors to enable once a session is started. This means that you never use appium just by itself. You always have to use it with a client library of some kind (or, if you're adventurous, cURL!). – introduction from the official website

The official website says Appium Client, but I prefer to call it package, a library that is more convenient for developers to code.
There are also related drivers corresponding to the mobile terminal. For Android, JRE and SDK need to be installed.

Points needing attention in the whole process

Apple's M1 chip does not have official full support for the time being. It does not support Java. Of course, it does not support Android. After referring to the relevant articles on the Internet and various problems I have encountered, I suggest this way, because it is relatively simple. download IntelliJ IDEA After that, open the following window through tools -- > Android -- > SDK manger:

Just find the installation you need. Install the environment that will install Java by default. Finally, remember to set the environment variables of Java and Android. If everything goes well, the environment will be set up.
You can install an inspector appium doctor through npm, which can help you check whether everything required by the environment is ready.

The installation commands are as follows:

npm install -g appium-doctor

Delete wechat zombie friends through Appium

The principle is actually very simple. The process of manual operation interface is simulated by operating the resource ID corresponding to the control in the UI interface.
Can refer to article I also refer to the explanation inside before I have the following code.

import time
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

friends = set()
default_wait_time = 2   # 2 seconds
server = 'http://localhost:4723/wd/hub'
desired_capabilities = {
    'platformName': 'Android',  # operating system
    'deviceName': '215cb0a4',  # The device ID is obtained by using the adb devices command in cmd
    'platformVersion': '10',  # Device version number, which can be viewed in mobile phone settings
    'appPackage': 'com.tencent.mm',  # app package name
    'appActivity': 'com.tencent.mm.ui.LauncherUI',  # Main Activity when app starts
    'noReset': True  # Do you want to keep the session information to avoid re login
}
driver = webdriver.Remote(server, desired_capabilities)


def has_warn(res_id):
    try:
        time.sleep(default_wait_time)
        driver.find_element_by_id(res_id)
        return True
    except NoSuchElementException:
        return False


def switch_tab(tab_name):
    time.sleep(default_wait_time)
    address_list = driver.find_elements_by_id('com.tencent.mm:id/dub')
    for address in address_list:
        cur_tab = address.get_attribute('text')
        if cur_tab == tab_name:
            address.click()
    time.sleep(default_wait_time)


def select_func(fun_name):
    time.sleep(default_wait_time)
    address_list = driver.find_elements_by_id('com.tencent.mm:id/rt')
    for address in address_list:
        cur_fun = address.get_attribute('text')
        if cur_fun == fun_name:
            address.click()
            break
    time.sleep(default_wait_time)


def come_back(times):
    while times > 0:
        driver.press_keycode(4)
        times -= 1
    time.sleep(default_wait_time)


def wait_load(res_id):
    wait = WebDriverWait(driver, 10)
    return wait.until(EC.presence_of_element_located((By.ID, res_id)))


def button_click(res_id):
    wait_load(res_id).click()


def send_words(res_id, keys):
    wait_load(res_id).send_keys(keys)


def reach_end(your_name, last_name):
    time.sleep(3)
    over = False
    address_list = driver.find_elements_by_id('com.tencent.mm:id/ft6')
    for address in address_list:
        friend = address.get_attribute('text')
        if friend != your_name and friend != 'Wechat team' and friend != 'File transfer assistant':
            friends.add(friend)
        if friend == last_name:
            over = True
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    driver.swipe(0.5 * x, 0.75 * y, 0.5 * x, 0.25 * x)
    return over


def get_friends(your_name, last_name):
    while not reach_end(your_name, last_name):
        continue


def find_friend(full_name):
    flag = False
    button_click('com.tencent.mm:id/he6')      # Search circle
    send_words('com.tencent.mm:id/bxz', full_name)      # Search box
    try:
        address = driver.find_element_by_id('com.tencent.mm:id/ir3')  # contacts
        if address.get_attribute("text") == full_name:  # Contact name
            return True
    except NoSuchElementException:
        flag = False
    come_back(2)
    return flag


def delete_friend(full_name):
    come_back(2)
    button_click('com.tencent.mm:id/d8')       # Three point buttons
    button_click('com.tencent.mm:id/h8t')      # Friend Avatar
    button_click('com.tencent.mm:id/d8')       # Three point buttons
    button_click('com.tencent.mm:id/ijq')      # delete
    button_click('com.tencent.mm:id/ffp')      # Prompt box delete


def proc_select(full_name):
    if not find_friend(full_name):
        return False
    button_click('com.tencent.mm:id/ir3')      # contacts
    button_click('com.tencent.mm:id/au0')      # plus
    select_func('transfer accounts')
    button_click('com.tencent.mm:id/e64')      # Number 1
    button_click('com.tencent.mm:id/e6c')      # Submit transfer
    if has_warn('com.tencent.mm:id/ffh'):       # Non friend prompt box
        button_click('com.tencent.mm:id/ffp')      # I see. Button
        delete_friend(full_name)
        print(full_name + 'You have been deleted, Now your world doesn't have her')
    else:
        come_back(6)


print('Wechat opened and began to deal with zombie friends')
time.sleep(5)
switch_tab('mail list')
get_friends('snowy', '99 last bus')
switch_tab('WeChat')
while len(friends):
    proc_select(friends.pop())
print('Zombie friends have finished processing and have exited wechat')

I wonder why the resource ID I see is different from that seen by the author for the same button.
Therefore, this code may not work for your mobile phone, and there may be other unknown risks. You need to test it in combination with your own environment.

A little gain

  1. When obtaining a resource, you need to wait until it is loaded, otherwise it cannot be obtained and an exception occurs
  2. When Python uses the try... Exception... Finally structure to catch exceptions, the return value of exception does not work and will be overwritten by finally
  3. When the mobile phone turns on USB debugging, Appium cannot be used. You also need to turn on the application that can be installed through USB
  4. There seems to be no problem with the program logic, but it stops when it is running. It may be that the mobile phone is stuck or the security housekeeper prevents it

Reference documents

Official reference documents
Appium Desktop Download
Talk about the pitfalls encountered using Appium on the Mac
Explain the interaction principle of Appium framework
Explain what npm is

Tags: node.js Android

Posted on Mon, 13 Sep 2021 15:24:16 -0400 by merrittj