python--Android APP automated test environment construction (Appium + night god simulator)

0. Preface

After reading a lot of courses on the introduction of automated testing, Baiyue Heiyu at Station B talked the most detailed.
From the environment construction to the common functions, they are all more detailed.

Since there is no project practice, I simply wrote what is worth buying APP automatic sign-in script.
Later I plan to buy a Raspberry Pi and put the whole environment on the Raspberry Pi, but I don’t know if I can install appium.

The code implementation idea is similar to the browser automation test on the PC side (Selenium library)
. There are many pits in the environment. It is best to follow the previous configuration (Baidu cloud link is below)
. Jdk, appium, Androidsdk, night god emulator,
but it has been unable to connect to the emulator, just change to an Android phone and connect directly

Mainly to record the environment construction process and connection process

1. Environment setup

Using appium+AndroidSDK+Ye Shen emulator (download from official website)
jdk+appium+AndroidSDK test passed version
link: https://pan.baidu.com/s/1sgR3V7VRRYggfCWMojDvCA Extraction code: ct3c

Automated testing principle:
python code (appium library-client)-connect to Appium Server/AndroidSDK-connect to Android device to control operation

  • The automation program (python code)
     needs to use the appium client library to send automation instructions to the Appium server to operate the mobile phone

  • Appium Server
     is responsible for managing the mobile phone automation environment and forwarding the control instructions of the automation program to the mobile phone


  •  Automated testing can be done on Android devices using IOS/Android. You can use real Android devices or emulators

Environment setup:

  • python install appium library
pip install appium-python-client
  • Install Appium Server+AndroidSDK+jdk
     Use the Baidu cloud link above to install AppiumServer and AndroidSDK to
      add the environment variable ANDROID_HOME, the directory is the decompressed Androidsdk root directory to
      add the environment variable PATH path, androidsdk\platform-tools
     needs to add the environment variable JAVA_HOME after installing jdk
    Insert picture description here
    Insert picture description here

  • Install the night god simulator
     directly from the official website to download and install

  • Connect the Android device to
     open the developer mode of the Android device, select to allow USB debugging

Insert picture description here
Insert picture description here

2. Connect the device

#查看adb是否添加环境变量成功
adb

Insert picture description here

#连接模拟器
#夜神模拟器开机,开启USB调试
adb device -l

Insert picture description here
But it shows that the emulator device is not found, and the connection is not successful.
Find the information. Some are because the adb version of the AndroidSDK and the nox_adb version of the night god emulator are inconsistent.
Try to copy the adb of the AndroidSDK to the night god emulator directory and change it to nox_adb

#查看adb版本
adb version

After the versions are the same, the device has not been found. After
connecting to 127.0.0.1:62001, the connection is finally successful.
Insert picture description here
Insert picture description here
Check the adb connected device again, it shows that it is connected.
Insert picture description here
Start appium
Insert picture description here

3. Code implementation

I simply wrote what is worth buying for APP check-in. The
core code is only two lines. The
overall analysis is similar to crawlers and Selenium. I
still recommend xpath positioning.

#encoding=utf-8
from appium import webdriver
from appium.webdriver.extensions.android.nativekey import AndroidKey

desired_caps = {
  'platformName': 'Android', # 被测手机是安卓
  'platformVersion': '5.1.1', # 模拟器安卓版本
  'deviceName': 'xxx', # 设备名,安卓手机可以随意填写
  'appPackage': 'com.smzdm.client.android', # 启动APP Package名称
  'appActivity': '.app.WelComeActivity', # 启动Activity名称
  'unicodeKeyboard': True, # 使用自带输入法,输入中文时填True
  'resetKeyboard': True, # 执行完程序恢复原来输入法
  'noReset': True,       # 不要重置App,防止登录信息丢失
  'newCommandTimeout': 6000,
  'automationName' : 'UiAutomator2'
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)
driver.find_element_by_id("com.smzdm.client.android:id/tab_usercenter").click()
driver.find_element_by_id("com.smzdm.client.android:id/tv_login_sign").click()
driver.quit()

Guess you like

Origin blog.csdn.net/m0_46622606/article/details/106437934