python + appnium 做android自动化测试

环境准备:

  1. android模拟器:androidstudio 下载

  2. python

  3. python开发工具PyCharm 下载地址: https://www.jetbrains.com/pycharm/

  4. node.js 环境

  5. chrome浏览器

  6. appnium 下载:
    http://appium.io/
    api: https://testerhome.com/topics/3144

  7. chrome 远程查看webview中内容:https://developers.google.com/web/tools/chrome-devtools/remote-debugging/?utm_source=dcc&utm_medium=redirect&utm_campaign=2016q3#configure-webview

参考:http://blog.csdn.net/qq744746842/article/details/47839645

示例代码:

#coding=utf-8

from appium import webdriver
import unittest
import  os
from time import  sleep

"""
试验测试android中的webview控件
"""

PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)

class AndroidWebview(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '5.1'
        desired_caps['deviceName'] = 'emulator-5554'
        desired_caps['app'] = PATH(
            '../../../sample-code/mobile.apk'
        )

        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

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

    def testWebview(self):
        sleep(15)
        ele = self.driver.find_element_by_name("新高考英语听力强化训练")
        ele.click()
        sleep(3)
        ele = self.driver.find_element_by_name("英语听力模拟试题(一)")
        ele.click()
        print self.driver.contexts

        self.driver.switch_to.context('WEBVIEW_com.android.browser')
        sleep(6)
        ele = self.driver.find_element_by_class_name("look")
        ele.click()
        sleep(6)

if __name__ == "__main__":
    suite = unittest.TestLoader().loadTestsFromTestCase(AndroidWebview)
    unittest.TextTestRunner(verbosity=2).run(suite)

你可能感兴趣的:(自动化测试)