Mac+python+Appium自动化测试

小白第一次使用Appium,记录一下过程。

一、环境安装

1、安装Appium

网上有各种安装方法,小白是直接下载dmg安装包安装的

https://bitbucket.org/appium/appium.app/downloads/

2、安装android sdk

3、python(mac已经自带),python开发端,我用的是pycharm

二、appium配置(安卓端)

配置好之后,点击lunch,然后点击放大镜,没有报错,就是配置成功了

三、第一个脚本

# coding=utf-8

from appium import webdriver

import unittest, time


class LoginTest(unittest.TestCase):
    def setUp(self):

        desired_caps = {}
        # 设置基本参数

        desired_caps['platformName'] = 'Android'

        desired_caps['platformVersion'] = '5.1.1'

        desired_caps['deviceName'] = 'f68ca56'

        desired_caps['noReset'] = True

        desired_caps['appPackage'] = 'ch999.app.UI'

        desired_caps['appActivity'] = 'ch999.app.UI.View.MainActivity'

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

    def tearDown(self):

        self.driver.quit()

    def test_login(self):

        time.sleep(2)

        def get_size():
            x = self.driver.get_window_size()['width']
            y = self.driver.get_window_size()['height']
            return (x,y)

        l = get_size()
        x1 = int(l[0] * 0.75)
        y1 = int(l[1] * 0.5)
        x2 = int(l[0] * 0.05)
        self.driver.swipe(x1, y1, x2, y1, 100)

        print "活动第一个广告页面"

        time.sleep(5)

        self.driver.swipe(x1, y1, x2, y1, 100)

        print "活动第二个广告页面"

        time.sleep(10)

        # self.driver.find_element_by_id("ch999.app.UI:id/btn_skip").click()
        # time.sleep(10)

        self.driver.find_element_by_id("ch999.app.UI:id/ll_tab_5").click()
        time.sleep(10)

        print self.driver.current_activity

        self.driver.find_element_by_id('ch999.app.UI:id/tv_user_name').click()

        time.sleep(10)

        username = self.driver.find_element_by_id('ch999.app.UI:id/et_username')
        password = self.driver.find_element_by_id('ch999.app.UI:id/et_password')
        # login = self.driver.find_element_by_id('ch999.app.UI:id/btn_user_login')

        username.send_keys('**')
        time.sleep(5)
        password.send_keys('******')
        time.sleep(5)

        self.driver.hide_keyboard()
        self.driver.tap([(53, 888), (1027, 1020)], 100)   #点击登陆按钮,通过坐标的形式
        time.sleep(20)

        print self.driver.current_activity

        try:
            x = self.driver.find_element_by_id('ch999.app.UI:id/tv_user_name')
            return x.is_displayed()
        except Exception as e:
            print e
            print "登录失败"


if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(LoginTest)
    unittest.TextTestRunner(verbosity=2).run(suite)
untiest 运行成功,第一个脚本算是通过啦

四、遇到的坑

1、appium 1.5.3版本,不支持selenium 3.8,最后把selenium的版本降下来,才能运行成功

2、appium 不兼容安卓7.0以上真机,需要改文件

3、session报错,配置成功后,只需要点击lunch,不用点放大镜,否则就会报错






你可能感兴趣的:(Mac+python+Appium自动化测试)