Appium是一個開源的自動化測試工具,其支持iOS和安卓平臺上的原生的,基於移動瀏覽器的,混合的應用。html
Appium是基於如下的四個理念設計來知足移動平臺測試自動化的要求的:java
1)您不該該由於須要自動化測試您的應用而不得不以任何形式去從新編譯或者修改你的appnode
2)您不該該把本身固定在一門特定的語言和一個特定的框架上去實現和運行你的測試python
3)當說到測試自動化APIs的時候,一個移動測試框架不該該作「從新發明輪子」的事情,android
4)一個移動測試自動化框架應該是開源的,不管是在精神上,實際上,仍是名義上!git
Appium在不一樣平臺中使用了標準的自動化APIs,因此在跨平臺時,不須要從新編譯或者修改本身的應用。github
Appium支持Selenium WebDriver支持的全部語言,如java、Object-C、JavaScript、Php、Python、Ruby、C#、Clojure,或者Perl語言,更可使用Selenium WebDriver的Api。Appium支持任何一種測試框架.Appium實現了真正的跨平臺自動化測試。(本文主要介紹Python的用法)web
Appium 是一個用Node.js編寫的HTTP server,它建立、並管理多個 WebDriver sessions 來和不一樣平臺交互,如 iOS ,Android等等. 瀏覽器
Appium 開始一個測試後,就會在被測設備(手機)上啓動一個 server ,監聽來自 Appium server的指令. 每種平臺像 iOS 和Android都有不一樣的運行、和交互方式。因此Appium會用某個樁程序「侵入」該平臺,並接受指令,來完成測試用例的運行。網絡
1) jdk(步驟再也不囉嗦)
2) android SDK,下載地址:http://developer.android.com/sdk/index.html,下載sdk tools,可能須要FQ,提供一個國內下載地址:http://www.androiddevtools.cn/
3) appium,下載地址:http://appium.io/
4) nodejs,下載地址:https://nodejs.org/en/
5) appium lib,下載地址:http://appium.io/downloads.html
選擇Python版本的Lib: Appium-Python-Client-0.22.tar.gz
因爲Appium依賴於Selemium,因此還要下載 Selemium Lib: selenium-2.53.2.tar.gz https://pypi.python.org/pypi/selenium
6) python, 下載地址:https://www.python.org/, 下載2.X 的版本。
上述軟件都準備好後,則進入搭建步驟。
將上述軟件依次安裝。
1) android sdk安裝完畢後,須要配置環境變量
新建ANDROID_HOME D:\ProgramFiles (x86)\Android\android-sdk
在PATH中添加:%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools;
2) nodejs安裝完畢後,須要配置環境變量
在PATH中添加:D:\Program Files\nodejs;
3) appium安裝完畢後,須要配置環境變量
D:\Program Files (x86)\Appium\node_modules\.bin;
4) 配置好後,啓動cmd,
輸入node -v,查看node安裝版本
輸入appium-doctor檢查appium的安裝環境是否成功,以下圖:
5) 安裝Python,配置環境變量,如C:\Python27,檢查是否設置成功,以下圖:
打開命令行,輸入appium, 顯示成功啓動:
假設咱們的代碼放在目錄E:\PythonTest\AppiumClientPython 中。首先把 Appium-Python-Client-0.22.tar.gz 裏面的 appium 目錄解壓到AppiumClientPython 中, 把 selenium-2.53.2.tar.gz裏面的 selenium 目錄解壓到AppiumClientPython中。
建立文件hello_appium.py , 編輯內容:
#coding=utf-8 from appium import webdriver
desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '4.4.2' desired_caps['deviceName'] = 'Android Emulator' desired_caps['appPackage'] = 'com.android.calculator2' desired_caps['appActivity'] = '.Calculator'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) driver.find_element_by_name("1").click() driver.find_element_by_name("5").click() driver.find_element_by_name("9").click() driver.find_element_by_name("9").click() driver.find_element_by_name("5").click() driver.find_element_by_name("+").click() driver.find_element_by_name("6").click() driver.find_element_by_name("=").click() driver.quit() |
打開命令行,cd到E:\PythonTest\AppiumClientPython 中,運行 python hello_appium.py, 正常狀況能夠看到手機按照代碼控制,打開計算器,逐個點擊按鈕完成計算。
import os from appium import webdriver APK_PATH = 'apk/ECloud-debug.apk' COMMAND_EXECUTOR_URL = 'http://localhost:4723/wd/hub'
desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '5.0' desired_caps['deviceName'] = 'Android Emulator' desired_caps['app'] = os.path.abspath(APK_PATH)
driver = webdriver.Remote(COMMAND_EXECUTOR_URL, desired_caps) |
btn = driver.find_element_by_name("+")
2) 經過ID查找
start_btn =driver.find_element_by_id('com.cn21.ecloud:id/instruction_close_btn')
或 start_btn = driver.find_element_by_id('instruction_close_btn')
child_text =parent.find_element_by_class_name('android.widget.TextView')
start_btn =driver.find_element_by_android_uiautomator('new UiSelector().clickable(true)')
以上find_element_by_XX 都是返回符合條件的第一個控件,若是要返回多個控件,能夠調用 find_elements_by_XX, 返回的是一個list。
注意:若是找不到符合條件的控件,會拋出異常。
def find_element_by_id_no_except(driver, id): element = None try : element = driver.find_element_by_id(id) except Exception,e: print Exception, ':', e return element |
login_btn.click()
注意:有的點擊若是須要等待動畫、或者網絡請求,建議等待一會:
import time time.sleep(2) # 睡眠2秒 |
user_input.send_keys('123456')
注意:Android若是要正確輸入,須要把使用系統自帶的輸入法,第三方輸入法沒法正確輸入。
driver.press_keycode(4)
其中按鈕的定義,由Android裏的KeyEvent.java裏定義的,因此其它的Android按鈕也是支持的。
driver.quit()
注意:必定要記得關閉driver, 不然下次鏈接的時候可能會出異常,由於Appium覺得你上次未關閉,會建立Session失敗。
爲了不代碼出現異常而沒有關閉,能夠在捕獲異常時再關閉。
下面的例子,演示點擊屏幕中間,並向上拉動(至關於查看列表下面的內容了)。
from appium.webdriver.common.touch_action import TouchAction def test_scroll_down(driver): screen = driver.get_window_size() action = TouchAction(driver) action.press(x=screen['width']/2,y=screen['height']/2) action.move_to(x=0,y=-screen['height']/10) action.release() action.perform()
|
等等,怎麼獲取界面的屬性來驗證正確性?
1)獲取當前Activity名稱
activity = driver.current_activity
2) 獲取屏幕寬高
screen = driver.get_window_size()
3)獲取控件文本
mobile_name.get_attribute('text') 或者 mobile_name.text
4)獲取控件類名
mobile_name.get_attribute('className')
5)判斷控件是否顯示
mobile_name.is_displayed() 或者 mobile_name.get_attribute('displayed')
6)得到控件位置
mobile_name.location
7)得到控件大小
mobile_name.size
8)查找控件子結點
parent.find_elements_by_class_name('android.widget.TextView')
一樣:查找控件的其它方法,也適用於查找子結點。
對於交互後的驗證,沒法驗證到具體的數據內容,能夠驗證當前的Activity,或者文本,或者列表是否爲空等等。
更多參考:http://blog.csdn.net/crisschan/article/details/50416860
Python自帶有unittest用於單元測試,其結構相似於JUnit。
一個測試類須要繼承於unittest.TestCase, 方法setUp 用於測試初始化,每一個用例開始前都會調用,tearDown用於用例結束時調用,每一個以test開始的函數被當成一個用例。
test_random.py
import random import unittest
class TestSequenceFunctions(unittest.TestCase):
def setUp(self): self.seq = range(10)
def test_shuffle(self): # make sure the shuffled sequence does not lose any elements random.shuffle(self.seq) self.seq.sort() self.assertEqual(self.seq, range(10))
# should raise an exception for an immutable sequence self.assertRaises(TypeError, random.shuffle, (1,2,3))
def test_choice(self): element = random.choice(self.seq) self.assertTrue(element in self.seq)
def test_sample(self): with self.assertRaises(ValueError): random.sample(self.seq, 20) for element in random.sample(self.seq, 5): self.assertTrue(element not in self.seq)
if __name__ == '__main__': unittest.main(verbosity=2) |
運行此測試: python test_random.py 能夠查看測試的結果
上面結果顯示,有2個用例測試經過,1個用例不經過。
能夠在一個目錄下寫多個以test開頭的測試文件,而後經過如下命令運行全部測試類:
python -m unittest discover . -v
test_ecloud_login_logout.py
#coding=utf-8
|
auto_test_ecloud.py
#coding=utf-8
|
注:爲了更方便的編寫python代碼,推薦使用PyCharm IDE,編輯代碼跟Java同樣方便。
1)官網 http://appium.io/index.html
2)appium/python-client使用文檔https://github.com/appium/python-client
3)搭建appium的android環境http://www.cnblogs.com/qiaoyeye/p/5131382.html
4)Appium移動自動化測試(四)http://www.cnblogs.com/fnng/p/4579152.html
5)AppiumPython API http://blog.csdn.net/crisschan/article/details/50416860
6)appium經常使用方法總結 http://www.cnblogs.com/fanxiaojuan/p/4882676.html