APPIUM+Python 简单的自动化测试DEMO

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mo_feng_/article/details/82426343

一、下载Appium
http://appium.io/
我安装的是1.6.3
二、下载Python
我安装的是2.7
三、java环境和Android环境我之前就已经安装存在
四、下载PyCharm–(Python的编程工具)
以上四步均是下载方面的内容。
五、开始测试
5.1、在用Python写脚本之前,我们还需要打开uiautomatorviewer.bat,他的路径是:
(C:\Users\xxx\AppData\Local\Android\Sdk\tools\bin)
点击左上角的手机图标,选择手机型号,就可以去获取手机的界面内容,如下图
这里写图片描述

可以看到右下角有resource-id、class、package、content-desc、bounds等,这些值都是定位用的,之后写python代码的时候有用到。
5.2、打开Appium (Appium可以理解为服务器,python代码就是要把真机或者模拟器与Appium连接)
5.3、简单的Python测试用例
打开PyCharm

import os
import unittest
import time
from appium import webdriver

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

class ContactsAndroidTests(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        #测试机的版本
        desired_caps['platformVersion'] = '8.0.0'
        # desired_caps['deviceName'] = 'Android Emulator'
        #测试机的型号
        desired_caps['deviceName'] = 'Samsung SM-G9500'
        # apk的绝对路径
        desired_caps['app'] = PATH(
            'F:/Program/TestAPP/AutoTest/xxx.apk'
        )
        #包名
        desired_caps['appPackage'] = 'com.xxx'
        #启动WelcomeActivity
        desired_caps['appActivity'] = 'com.xxx.WelcomeActivity'
        print("连接中...")
        #连接Appium
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        print("连接完成!")
        time.sleep(5)
    def tearDown(self):
        self.driver.quit()

    def test_add_contacts(self):
    # "com.xxx:id/user_experience" 这个就是用uiautomatorviewer定位到的
        el = self.driver.find_element_by_id("com.xxx:id/user_experience")
        el.click()

        time.sleep(5)
        self.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button").click()
        time.sleep(2)
        self.driver.find_element_by_id("com.android.packageinstaller:id/permission_allow_button").click()
        time.sleep(2)
        self.driver.find_element_by_name("一楼").click();

        alert = self.driver.switch_to_alert()


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

代码中可能会引用

import selenium
import time
from appium import webdriver

像selenium、webdriver等,可以通过pycharm自动下载。

初次接触,很多方法都不懂,之后会继续更新,直到写一个完整的app自动化测试用例。
引入一篇博客:讲 find_element_by_id 等的用法 https://www.cnblogs.com/miniren/p/7365885.html

猜你喜欢

转载自blog.csdn.net/mo_feng_/article/details/82426343