Python Appium 开启Android测试之路

1、获取 Android app的Activity

打开终端cmd,先cd进入到刚才下载的“新浪.apk”目录下,然后使用aapt dump badging xxx.apk命令获取包内信息。注意,启动类名称一个字母都不能错。

 aapt dump badging 新浪.apk cat>d:/log.txt     这样可以将信息指定到某一文件下然后 查看 

 

  罪过了,后面发现正常是没有appt.exe 这个应用程序的。可以下载一个放到tools文件下,因为已经配置了环境变量,所以 可以直接使用。

可以从下面拷贝一个到tools下即可。

 

 

2、启动微博

#-*-coding:utf-8-*-
#Time:2017/7/20 18:06
#Author:YangYangJun
import time
from appium import webdriver
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = 'N79SIV5PVCSODAQC'
desired_caps['appPackage'] = 'com.sina.weibo'
desired_caps['appActivity'] = 'com.sina.weibo.SplashActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)
time.sleep(3)
driver.quit()

 

3、元素定位

 3.1、appium的webdriver提供了11种元素定位的方法,在selenium的基础上扩展了三个,可以在pycharm里面输入driver.find_element_by然后会自动匹配出来 
 
 
 多的三个:
      driver.find_element_by_accessibility_id()
      driver.find_element_by_android_uiautomator()
      driver.find_element_by_ios_uiautomation()
      (第三个是ios的专用的)
 

3.2、下面一一举例进行 介绍

3.2.1、元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作.

uiautomatorviewer是android-sdk自带的一个元素定位工具,非常简单好用,使用uiautomatorviewer,你可以检查一个应用的UI来查看应用的布局和组件以及相关的属性。

启动uiautomatorviewer.bat

    打开目录D:\SProgram\Sdk\tools

 

如果不喜欢双击启动的话,也可以在cmd里面通过指令启动

先cd到tools目录下,然后输入uiautomatorviewer.bat回车后启动服务 

 

3.2.1、find_element_by_accessibility_id

driver.find_element_by_accessibility_id(u"我的").click()

 

 

 3.2.2、通过id定位元素
resrouce-id属性是id:
driver.find_element_by_id('username') 

 

 

3.2.3、通过name定位元素
text属性是name

driver.find_element_by_name('password')

 

 

 4、结合公司刚开发出来的app,进行项目实训
#-*-coding:utf-8-*-
#Time:2017/7/21 16:32
#Author:YangYangJun


import time
import unittest

from appium import webdriver

import sys
reload(sys)
sys.setdefaultencoding('utf8')


class Login(unittest.TestCase):

    def setUp(self):
        self.desired_caps = {}

        self.desired_caps['platformName'] = 'Android'
        self.desired_caps['platformVersion'] = '6.0'
        self.desired_caps['deviceName'] = 'N79SIV5PVCSODAQC'
        self.desired_caps['appPackage'] = 'com.xxxx.mrg'
        self.desired_caps['appActivity'] = 'com.uzmap.pkg.EntranceActivity'
        #隐藏键盘
        self.desired_caps['unicodeKeyboard'] = True
        self.desired_caps['resetKeyboard'] = True

        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_caps)
        #self.driver.implicitly_wait(15)



    #测试异常登录-用户不存在

    def test_aNoUserLogin(self):

        driver  = self.driver
        time.sleep(5)
        driver.find_element_by_accessibility_id(u"我的").click()

        time.sleep(5)

        driver.find_element_by_accessibility_id(u"登录/注册").click()
        time.sleep(2)

        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("username")').clear()

        time.sleep(2)
        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("username")').send_keys('xzbuyer1')
        time.sleep(2)

        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("password")').clear()

        time.sleep(2)
        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("password")').send_keys('111111')

        time.sleep(2)

        driver.find_element_by_accessibility_id(u"登录").click()
        time.sleep(18)

        get_alertMessage = driver.find_elements_by_android_uiautomator('new Uiselector().resourceId("android:id/message")').get_attribute('name')
        time.sleep(2)
        if get_alertMessage == u'用户不存在':
            print u"用户不存在,用例执行成功!"
        else:
            print u"用例执行失败!"


    def test_bNoPassLogin(self):

        driver  = self.driver
        time.sleep(5)
        driver.find_element_by_accessibility_id(u"我的").click()

        time.sleep(5)

        driver.find_element_by_accessibility_id(u"登录/注册").click()
        time.sleep(2)

        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("username")').clear()

        time.sleep(2)
        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("username")').send_keys('xzbuyer')
        time.sleep(2)

        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("password")').clear()

        time.sleep(2)
        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("password")').send_keys('1111112')

        time.sleep(2)

        driver.find_element_by_accessibility_id(u"登录").click()
        time.sleep(18)

        get_alertMessage = driver.find_elements_by_android_uiautomator('new Uiselector().resourceId("android:id/message")').get_attribute('name')
        time.sleep(2)
        print get_alertMessage[0:5]

        # if get_alertMessage == u'用户不存在':
        #     print u"用户不存在,用例执行成功!"
        # else:
        #     print u"用例执行失败!"
        #

    # 测试正常登录

    def test_cLogin(self):

        driver = self.driver
        time.sleep(5)
        driver.find_element_by_accessibility_id(u"我的").click()

        time.sleep(5)

        driver.find_element_by_accessibility_id(u"登录/注册").click()
        time.sleep(2)

        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("username")').clear()

        time.sleep(2)
        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("username")').send_keys('xzbuyer')
        time.sleep(2)

        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("password")').clear()

        time.sleep(2)
        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("password")').send_keys('111111')

        time.sleep(2)

        driver.find_element_by_accessibility_id(u"登录").click()
        time.sleep(2)

        # 判断是否登录成功

        print driver.find_element_by_accessibility_id(u"西藏医药销售有限公司").get_attribute('name')

        nickName = driver.find_element_by_accessibility_id(u"西藏医药销售有限公司").get_attribute('name')
        temp = nickName[0:2]
        print  temp
        if nickName == u'西藏医药销售有限公司':
            print '登录成功!'

        else:
            print '登录失败!'

    def tearDown(self):
        self.driver.close_app()
        self.driver.quit()



if __name__ == "__main__":
    unittest.main()

 

posted @ 2017-09-16 01:18  Blue·Sky  阅读(3603)  评论(0编辑  收藏  举报