appium automated testing---stress testing robot automatically recharges

Preface: The following describes a simple appium automated test. Combined with the current work tasks, a simple automated program is performed; it includes the configuration of the automated test environment and a simple appium automated test example;

1. Construction of automated environment

For detailed installation steps, please see Appium Installation and Environment Configuration

1. Install python

1. Open the official Python website https://www.python.org/ , click Download, and download the latest version of Python. (Note: I am using Python 3.10.11)
2. When installing, remember to check "Add python to PATH" on the first page and click "install now". When success appears, the installation is complete.
3. After the installation is completed, enter "python" in cmd. If you can see the version number of Python, the installation is successful.

2. Install pycharm

Open the pycharm official website and click download to jump to the installation page to download the free community version. Double click to install.

3.Install jdk

1. Download the latest jdk version from the official website
2. Double-click to install after downloading
3. Configure environment variables

4.Install sdk

Download android sdk sdk download address

5.Install node

1. Download the node.js official website address
2. Select SDK Tools and select the installation package to download in Windows
3. Check whether the installation is successful.
Enter adb version in the terminal. If the message as shown in the figure appears, it means the installation is successful.

6.Install appium server

  1. Download and install the client (window)

Desktop client official website address
Follow the installation guide to the next step to completion

  1. Command version installation (supports window, mac, linux)

1. Download
(1) Install the latest version by default
npm install -g appium
(2) Specify the installation version
npm install -g [email protected]
2. Check the installation results.
Run cmd and enter
appium --version
. If the version information is displayed normally, the appium environment is installed successfully;

7. Install Appium-Python-Client

1. Enter cmd
pip install Appium-Python-Client
2. If downloading Appium-Python-Client from the official resource library fails, please change the download source and download again.
Enter at the command line
pip install Appium-Python-Client -i http://pypi.douban.com/simple

2. Code

The following is the appium connection robot, which uses element positioning to locate the button and click it

# 导入所需模块
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.extensions.android.nativekey import AndroidKey
from appium.webdriver.common.mobileby import MobileBy as AppiumBy
from selenium.webdriver.support.wait import WebDriverWait

# 连接参数
desired_caps = {
    
    
    "platformName": "Android", #移动平台名称
    "appium:automationName": "uiautomator2", #自动化框架或者库
    "appium:deviceName": "192.168.3.6", #连接设备的名称
    "appium:appPackage": "cn.mrobot.web.hermes", #测试应用的app的包名
    "appium:adbExecTimeout": 600000, #ADB命令执行超时时间
    "appium:uiautomator2ServerInstallTimeout": 600000, #设置UIAutomator2服务器安装超时时间,以毫秒为单位。
    "appium:appActivity": ".MainActivity", #指定应用的启动Activity。可以通过查看应用的AndroidManifest.xml文件获取
    "appium:ensureWebviewsHavePages": True, #设置为True以确保所有webview都已加载页面,False则不进行等待,默认为False
    "appium:nativeWebScreenshot": True, #设置为True以使用本机截图功能进行截图,False则使用Appium自己的截图功能,默认为False。
    "appium:newCommandTimeout": 3600, #设置命令超时时间,以秒为单位。
    "appium:connectHardwareKeyboard": True, #设置为True以允许硬件键盘输入,False则禁用,默认为False。
    "appium:noReset": True #设置为True以保留应用的数据和状态,False则每次运行测试时重置应用,默认为False。
}

# # 连接appium server,初始化自动化环境
# driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
#连接appium server ,不初始化自动化环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps, initialize=False)


# 自定义异常类,用于表示步骤执行异常
class StepException(Exception):
    def __init__(self, step, button_text):
        self.step = step
        self.button_text = button_text

    def __str__(self):
        return f"Step '{
      
      self.step}': Button '{
      
      self.button_text}' did not get clicked."


# 自定义函数来查找并点击按钮
def find_and_click_button(driver, button_class_name, button_text):
    try:
        # 查找按钮元素
        buttons = WebDriverWait(driver, 10).until(
            lambda drv: drv.find_elements(by=AppiumBy.CLASS_NAME, value=button_class_name)
        )
        for button in buttons:
            if button.text == button_text:
                button.click()
                return

        raise StepException("find_and_click_button", button_text)  # 若没有找到按钮,抛出自定义异常

    except Exception as e:
        print(f"Error: {
      
      e}")
        raise


# 输入密码
def enter_password(driver, password):
    try:
        for digit in password:
            find_and_click_button(driver, "android.view.View", digit)

        raise StepException("enter_password", password)  # 引发自定义异常,表示密码输入未成功

    except Exception as e:
        print(f"Error: {
      
      e}")
        raise


# 调用示例
try:
    # 点击“我要下单”
    find_and_click_button(driver, "android.widget.Button", "我要下单")

    # 点击“输入密码”
    find_and_click_button(driver, "android.view.View", "输入密码")

    # 点击“bill测区发货166”
    find_and_click_button(driver, "android.view.View", "bill测区发货166")

    # 输入密码"100893"
    enter_password(driver, '100893')

    # 点击确认
    find_and_click_button(driver, "android.view.View", "确认")

except StepException as e:
    print(e)

In the above code, since some buttons cannot always be clicked when running, an exception is added. For elements that are not clicked, an exception is thrown and the output is printed.

3. adb connection robot

Preliminary preparation:
1. The screen on the robot needs to enter developer mode and open adb debugging
2. Connect to the robot’s WiFi and use a wireless connection

  1. Open the terminal and enter
adb connect 192.168.3.6:5555

If a connection error occurs, please see the adb connect connection device appears offline.

  1. View connected devices
adb devices

Summarize

1. Thank you for the sharing of the automatic environment. I am also "borrowing flowers to offer to Buddha";
2. There are also many implementation problems in the code part. I will record the learning process for the time being and continue to update it in the future;
3. If you have any questions, please feel free to ask. point out;

Guess you like

Origin blog.csdn.net/m0_44993010/article/details/131127886