Python编写Appium测试用例(1)

      有段时间没有使用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直接执行即可)

posted @ 2015-11-14 22:07  jiguanghover  阅读(2361)  评论(2编辑  收藏  举报