1. 程式人生 > >Appium+python 一個簡單的登入測試例項

Appium+python 一個簡單的登入測試例項

# coding=utf-8

from appium import webdriver
import time
import unittest
import os
import HTMLTestRunner


class LoginTestLizi(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'  # 裝置系統
        desired_caps['platformVersion'] = '6.0.1'  # 裝置系統版本
desired_caps['deviceName'] = '270f2988' # 裝置名稱 desired_caps['appPackage'] = 'com.lizi.app' # 測試app包名 desired_caps['appActivity'] = '.activity.MainActivity' # 測試appActivity self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 啟動app def test_login
(self):
driver = self.driver # 進入首頁後點擊‘我的’按鈕 driver.find_element_by_name(u'我的').click() time.sleep(2) # 點選登入頭像按鈕,進行登入,跳轉到登入介面 driver.find_element_by_id('com.lizi.app:id/user_login_iv').click() time.sleep(2) # 輸入使用者名稱 driver.find_element_by_id('com.lizi.app:id/zhanghao_edittext'
).send_keys('18267200735') # 輸入密碼 driver.find_element_by_id('com.lizi.app:id/password_edittext').send_keys('password') # 點選確認登入按鈕 driver.find_element_by_id('com.lizi.app:id/login_button').click() time.sleep(3) # 登入成功,頁面下滑,不然點選不到設定按鈕 driver.swipe(500, 200, 500, 800, 0) time.sleep(2) # 獲取登入後的暱稱 name = driver.find_element_by_id('com.lizi.app:id/login_username_tv').text # 新增斷言,若暱稱不正確,則列印錯誤資訊 try: assert 'No_matter' in name print 'loginUser is right' except AssertionError as e: print 'loginUser is Error' # 點選設定按鈕,進入設定頁面 driver.find_element_by_id('com.lizi.app:id/setting_imageView').click() # 點選退出按鈕 driver.find_element_by_id('com.lizi.app:id/exit_button').click() def tearDown(self): self.driver.quit() if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(LoginTestLizi('test_login')) filename = 'C:\\Temp\\app.html' fb = file(filename, 'wb') runner = HTMLTestRunner.HTMLTestRunner(stream=fb, title='liziapptestreport', description='liziapp') runner.run(suite) fb.close()