有段時間沒有使用python編寫測試用例了,很長時間以來,感覺appium這個測試工具確實不錯,今天又重新拿起來,分享一下自己學習的一些用例,歡迎大家一起交流、學習!
1.登錄客戶端
#coding=utf-8
import os
import unittest
from appium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
#定義時間格式
ISOTIMEFORMAT='%Y-%m-%d %X'
class LoginTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4.4'
desired_caps['deviceName'] = '24c23456' #可以使用adb devices查看連接的機器
#desired_caps['app'] = PATH('ContactManager/ContactManager.apk')
desired_caps['appPackage'] = 'com.aaa.bbb'
desired_caps['appActivity'] = '.MainActivity'
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
def tearDown(self):
self.driver.quit() #退出應用
def test_user_login(self):
#為了計算找到“個人中心”這個元素需要多長時間,所以打印了2個當前時間
print time.strftime(ISOTIMEFORMAT, time.localtime())
#當前進程等待直到找到指定的元素
WebDriverWait(self.driver,20).until(EC.presence_of_element_located((By.NAME, u'個人中心')))
print time.strftime( ISOTIMEFORMAT, time.localtime() )
els = self.driver.find_elements_by_class_name("android.widget.Radio")
#els[4].click()
#el = self.driver.find_element_by_id("gerenzhongxin")
#打開個人中心
el = self.driver.find_element_by_name(u"個人中心")
el.click()
#打開登錄頁面
login_register = self.driver.find_element_by_id('btn_islogin')
login_register.click()
#輸入用戶名,密碼,然后點擊登錄
user_name = self.driver.find_element_by_id('edit_username')
user_name.send_keys('aaa')
user_pass = self.driver.find_element_by_id('edit_password')
user_pass.send_keys('123456')
user_login = self.driver.find_element_by_id('btn_login')
user_login.click()
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(LoginTests)
unittest.TextTestRunner(verbosity=2).run(suite)
說明:
大家查看app頁面的元素可以使用sdk/tools目錄下的uiautomatorviewer.bat(win7直接執行即可)
