上篇文章Appium環境搭建: https://blog.csdn.net/CCstar1/article/details/101543977
上篇文章簡單介紹了Appium基本的環境搭建,但是也僅僅只能到拿到簡單的元素id以及手動點擊運行,這裏離自動化測試就差一步了,這篇文章我會簡單介紹如何使Appium和VSCode鏈接起來並寫一個簡單的測試用例,將差的一步完成。
一.下載安裝VSCode
下載地址:https://code.visualstudio.com
簡單使用教程:https://www.jianshu.com/p/11554732b323
VSCode安裝以及使用比較簡單這裏就不做說明了,直接進入appium的相關配置吧。
1.添加VSCode支持Python開發
在VSCode中搜索擴展Python,如下圖
2.下載Appium-Python-Client
打開VSCode終端輸入:
pip3 install Appium-Python-Client
這樣下載默認會下載到Python3的安裝路徑下,這裏本人之前用的Mac再帶的Python2.7 後面改爲使用Python3.7。
3.配置PythonPath在setting.json
"python.formatting.provider": "yapf",
"python.pythonPath":"/Library/Frameworks/Python.framework/Versions/3.7/bin/python3",
"editor.fontSize": 16,
"workbench.colorTheme": "Dracula At Night"
這個需要注意PythonPath的路徑一定要正確
4.導入Appium
在VSCode終端輸入:
1.python3
2.import appium
上圖是我引入 時的操作截圖。
至此我們就可以愉快的開始自動化測試啦,下面是我根據本人公司APP寫的一個簡單的測試用例供大家參考:
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
# case 進入APP簡單瀏覽並登錄
from appium import webdriver
import time
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "9"
caps["deviceName"] = "xxxxxxxx"
caps["appPackage"] = "com.xxx.xx"
caps["appActivity"] = ".module.MainActivity"
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
# 申請權限
driver.find_element_by_id('com.android.packageinstaller:id/permission_allow_button').click()
driver.find_element_by_id('com.android.packageinstaller:id/permission_allow_button').click()
driver.find_element_by_id('com.android.packageinstaller:id/permission_allow_button').click()
# 關閉彈窗
driver.find_element_by_id('com.xxx.xx:id/dialog_home_img_cancel').click()
# 睡眠三秒
time.sleep(3)
# 滑動
driver.swipe(0,2000,0,100,2000)
# driver.scroll(driver.find_element_by_id('com.xxx.xx:id/title_bar_rl_root'),driver.find_element_by_id('com.xxx.xx:id/rv_content'))
# 睡眠三秒
time.sleep(3)
# 點擊美購
driver.tap([(329,2240),])
# 睡眠三秒
time.sleep(3)
# 滑動
driver.swipe(0,500,0,2000,)
# 睡眠三秒
time.sleep(3)
# 點擊我的
driver.tap([(967,2216),])
# driver.find_element_by_id('com.xxx.xx:id/main_rl_menu_message').click()
# driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.LinearLayout[4]').click()
# 點擊賬號登錄
driver.find_element_by_id('com.xxx.xx:id/account_authentication_rb_pass_login').click()
# 輸入手機號
username='17600000000'
print('username: %s' %username)
driver.find_element_by_id('com.xxx.xx:id/account_authentication_et_typeface').send_keys(username)
# 輸入密碼
password='123456'
print('password: %s' %password)
driver.find_element_by_id('com.xxx.xx:id/account_authentication_et_verify_label').send_keys(password)
# 點擊登錄
driver.find_element_by_id('com.xxx.xx:id/account_authentication_tv_main').click()
# 執行完退出
driver.quit()
下面打算學習配置測試結果反饋,大家一起加油,有問題歡迎一起討論!