Python+Appium automated testing-writing automated scripts

I have already described how to manually use appium-desktop to start the app on the test machine, but when we actually run the automation script, we need to use the script to call appium to start the app. Next, we will try to write a Python script to start the app and log in to the app. The environment is Windows10 + Python3.7 + appium1.18.0 + Android phone + Toutiao app + Pycharm

1. Connect to the test mobile phone to obtain the configuration of the test machine and the APP under test. For
specific acquisition methods, please refer to appium for APP automated testing. Connect the real machine to start the app. The configuration information is as follows:

{
  "platformName": "Android",
  "platformVersion": "10",
  "deviceName": "PCT_AL10",
  "appPackage": "com.ss.android.article.news",
  "appActivity": ".activity.MainActivity"
}

2. Write a Python script to start the app
1. The editor recommends that you use PyCharm. Download the pycharm community version. It is free and does not require cracking.

2. Since logging in requires an account number and password, two new parameters are added here. The parameter unicodeKeyboard is whether to enable Unicode format input strings. The default value is False. If set to True, it means enabled. The parameter resetKeyboard is whether to reset the keyboard to its original state after using the unicodeKeyboard function for Unicode input. The default is False.

desired_caps = {
    "platformName": "Android",
    "platformVersion": "10",
    "deviceName": "PCT_AL10",
    "appPackage": "com.ss.android.article.news",
    "appActivity": ".activity.MainActivity",
    "unicodeKeyboard": True,
    "resetKeyboard": True,
}

3. Pass in desired_caps to start the app through appium and construct the driver object (that is, create a session).

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

If appium is opened on this machine, the IP is 127.0.0.1 and the port defaults to 4723.

4. Connect the computer to the mobile phone, start the appium service, run the script, and debug whether the app can be started.

from appium import webdriver
 
desired_caps = {
    "platformName": "Android",
    "platformVersion": "10",
    "deviceName": "PCT_AL10",
    "appPackage": "com.ss.android.article.news",
    "appActivity": ".activity.MainActivity",
    "unicodeKeyboard": True,
    "resetKeyboard": True,
}
 
# 启动app
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

We will see the Toutiao app being opened on the mobile phone.

Third, locate the element through appium Inspector and log in to APP
1. The login steps are:

Start the Toutiao app --> Click [I Got It] --> Confirm management permissions --> Click the bottom tab "Not logged in" to enter the non-logged in page --> Click "Login" --> Click "..." - -> Select password to log in --> Enter account password --> Click to log in

2. According to the operation steps, use Appium’s Inspector to obtain the attributes of the login-related operation elements.

[Enter the appium Inspector page](#), first click the Select Element button, and then on the mobile page displayed on the left side of the window, click to select the element that needs to be positioned. The attribute value of the element will be displayed on the right side of the window.

 If appium Inspector cannot obtain element attributes, you can also use the tool uiautomatorviewer that comes with the Android SDK to obtain them. We will specifically introduce how to obtain element attributes later, so we won’t go into details here.

3. Login operation script

# -*- coding:utf-8 -*-
 
import time
from appium import webdriver
 
desired_caps = {
    "platformName": "Android",
    "platformVersion": "10",
    "deviceName": "PCT_AL10",
    "appPackage": "com.ss.android.article.news",
    "appActivity": ".activity.MainActivity",
    "unicodeKeyboard": True,
    "resetKeyboard": True,
}
 
# 启动app
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
 
# 登陆操作
driver.find_element_by_id("com.ss.android.article.news:id/chj").click() # 点击【我知道了】
time.sleep(1)
driver.find_element_by_id("android:id/button1").click() # 点击权限管理-确定按钮
time.sleep(1)
driver.find_element_by_xpath("//android.widget.TabWidget/android.widget.RelativeLayout[@index=3]").click() # 点击未登录
time.sleep(1)
driver.find_element_by_id("com.ss.android.article.news:id/a1c").click() # 未登录页点击登录按钮
time.sleep(1)
driver.find_element_by_id("com.ss.android.article.news:id/bfm").click() # 登录页点击“。。。”
time.sleep(1)
driver.find_element_by_xpath("//android.widget.LinearLayout[@index=4]").click() # 选择密码登录
time.sleep(1)
driver.find_element_by_id("com.ss.android.article.news:id/c7").send_keys("********")   # 输入账号
time.sleep(1)
driver.find_element_by_id("com.ss.android.article.news:id/ch").send_keys("********")   # 输入密码
time.sleep(1)
driver.find_element_by_id("com.ss.android.article.news:id/a31").click() # 点击登录
time.sleep(5)

When we check the mobile phone, we will find that the mobile phone is automatically logging into the Toutiao app and the login is successful.

At this point, we have completed the operation of logging in to the app by writing a simple Python script. If we add assertions, it will be a complete use case.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Guess you like

Origin blog.csdn.net/2301_76643199/article/details/133524201