Python + Appium+ IOS自动化测试

导言:前面写了Appium环境的搭建,想必木有太大的问题,现在整理下如何编写Python + appium + iOS自动化脚本
  • 1
  • 2

1.官方文档必须得看熟悉了,不懂的可以看官方文档,或者加入阳台测试群进行咨询

Python Appium官网文档:http://appium.io/slate/cn/v1.2.0/?python#appium
  • 1
  • 2

2.Python利用Appium编写iOS自动化脚本步骤

1)搭建好Appium环境
2)拿到iOS开发APP项目文档(含***.xcodeproject文档)
3)cmd进入含.xcodeproject文档目标下利用:xcodebuild -sdk iphonesimulator命令编译iOS文档,将会在同级目录下生产build目录文件,文件中保护编写iOS自动化脚本的.app文件
4)开启Appium客户端,选择ios脚本执行,并选择刚生成.app目录下文件
5)编写Python iOS自动化脚本
6)不断调试测试脚本
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.Python Appium客户端库

1)锁定屏幕  driver.lock(5)
2)把当前应用放到后台去  driver.background_app(5)
3)在 iOS 上收起键盘  driver.hide_keyboard()
4)检查应用是否已经安装  driver.is_app_installed('com.example.android.apis')
5)安装应用到设备中去  driver.install_app('path/to/my.apk')
6)从设备中删除一个应用  driver.remove_app('com.example.android.apis')
7)模拟设备摇晃  driver.shake()
8)关闭应用  driver.close_app()
9)启动应用  driver.launch_app()
10)应用重置  driver.reset()
11)列出所有的可用上下文  driver.contexts
12)列出当前上下文  driver.current_context
13)将上下文切换到默认上下文  driver.switch_to.context(None)
14)iOS 里是 Localizable.strings Android 里是 strings.xml  driver.app_strings
15)发送一个按键事件给设备  driver.keyevent(176)
16)Android only 得到当前 activity  driver.current_activity
17)生成触摸动作的接口。这部分文档很快将会补充更多的内容进来
action = TouchAction(driver)
action.press(element=el, x=10, y=10).release().perform()
18)模拟用户滑动  driver.swipe(75, 500, 75, 0, 1000)
19)在 0% 到 100% 内双指缩放屏幕  driver.pinch(element=el)
20)放大屏幕 在 100% 以上放大屏幕  driver.zoom(element=el)
21)从设备中拉出文件  driver.pull_file('Library/AddressBook/AddressBook.sqlitedb')
22)推送文件到设备中去
data = "some data for the file"
path = "/data/local/tmp/file.txt"
driver.push_file(path, data.encode('base64'))
......
详情请查看上面提到的官方文档
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

4.Python Appium例子

AppStore下载猫宁智能考勤APP,哈哈,这里打个小广告,猫宁考勤是一款APP考勤软件,妈妈再也不用担心我上下班忘记打卡了,猫宁官网:http://isen-tech.com/

代码如下:
  • 1
  • 2
  • 3
  • 4
# -*- coding:utf-8 -*-
import unittest
import os
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from time import sleep
import datetime
import random

class MyIOSTests(unittest.TestCase):
    #开启猫宁3.0
    def setUp(self):
        app = os.path.join(os.path.dirname(__file__),
                           '/Users/xuyangting/Desktop/AttendanceAdminIOS/CloudRecord/build/Debug-iphonesimulator',
                           'CloudRecord.app')
        app = os.path.abspath(app)
        self.driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4723/wd/hub',
            desired_capabilities={
                'app': app,
                'platformName': 'iOS',
                'platformVersion': '8.4',
                'deviceName': 'iPhone 6'
            })

    #引导页滑屏处理
    def test_boot_page(self):
        sleep(10)
        self.driver.swipe(350, 300, 0, 300, 800)
        #滑屏的问题暂时还没解决,心好痛,Android滑屏文档先前也是用swipe不行,后来用drag搞定了,这次ios自动化又被滑屏卡到了,真的引导页虐我千百遍,我待它如初恋

    #登录猫宁3.0
    def test_login(self, mobile, password):
        #启动页滑屏处理
        self.test_boot_page()
        #登录
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAScrollView[1]/UIATextField[1]").send_keys(mobile)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAScrollView[1]/UIASecureTextField[1]").send_keys(password)
        self.driver.hide_keyboard()
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAScrollView[1]/UIAButton[1]").click()
        sleep(5)

    #测试注册&忘记密码
    def test_reg_forget_password(self):
        pass

    #测试查询模块
    def test_query(self):
        self.test_login("13133847086", "123456")
        #测试按日
        #测试切换时间按日查询
        #测试未来时间
        self.driver.find_element_by_name("query dayCircle bt").click()
        self.driver.find_element_by_name("calender left arrow").click()
        self.driver.find_element_by_name("01").click()
        #测试过去时间
        self.driver.find_element_by_name("query dayCircle bt").click()
        self.driver.find_element_by_name("calender right arrow").click()
        self.driver.find_element_by_name("02").click()
        sleep(3)
        #测试按日数据
        choose = datetime.datetime.today().day
        if choose == 1:
            ch = "0" + str(choose)
        elif choose < 10 and choose > 1:
            ch = "0" + str(choose-1)
        else:
            ch = str(choose)
        self.driver.find_element_by_name("query dayCircle bt").click()
        sleep(5)
        self.driver.find_element_by_name(ch).click()
        sleep(3)
        #测试修改考勤 旷工改成事假
        self.driver.find_element_by_name("旷工").click()
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATableView[1]/UIATableCell[1]").click()
        self.driver.find_element_by_name("修改考勤").click()
        sleep(3)
        self.driver.find_element_by_name("事假").click()
        self.driver.find_element_by_name("确认修改").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        sleep(3)
        #刷新页面
        kuang_gong = self.driver.find_element_by_name("旷工")
        zheng_chang = self.driver.find_element_by_name("正常")
        action = TouchAction()
        action.press(kuang_gong).move_to(zheng_chang).release()
        #测试按月
        self.driver.find_element_by_name("按月").click()
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        #测试选择月份
        self.driver.find_element_by_name("选择月份").click()
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAButton[2]").click()
        #测试选择员工
        self.driver.find_element_by_name("选择员工").click()
        sleep(3)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATableView[1]/UIATableCell[1]").click()
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        #测试选择排序
        self.driver.find_element_by_name("选择排序").click()
        self.driver.find_element_by_name("按在岗时长排序").click()
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        self.driver.find_element_by_name("按字母排序").click()
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        #测试导出结果
        self.driver.find_element_by_name("导出结果").click()
        email = "407708323@qq.com"
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[4]/UIAAlert[1]/UIAScrollView[1]/UIATableView[1]/UIATableCell[1]/UIATextField[1]").send_keys(email)
        self.driver.find_element_by_name("取消").click()
        self.driver.find_element_by_name("导出结果").click()
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[4]/UIAAlert[1]/UIAScrollView[1]/UIATableView[1]/UIATableCell[1]/UIATextField[1]").send_keys(email)
        self.driver.find_element_by_name("好").click()
        sleep(5)
        #测试查看详情
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATableView[1]/UIATableCell[1]/UIAStaticText[5]").click()
        sleep(3)
        self.driver.find_element_by_name("查看异常").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATableView[1]/UIATableCell[2]/UIAStaticText[5]").click()
        sleep(3)
        self.driver.find_element_by_name("查看异常").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()

    #测试管理模块
    def test_manage(self):
        self.test_login("13133847086", "123456")
        positions = []
        positions.append((180, 630))
        self.driver.tap(positions)
        #测试公司员工
        sleep(3)
        #测试添加员工
        self.driver.find_element_by_name("添加").click()
        sleep(3)
        self.driver.find_element_by_name("添加员工").click()
        sleep(3)
        #测试通讯录导入请在真机测试,模拟器通讯录无联系人
        #测试手动添加
        self.driver.find_element_by_name("手动添加").click()
        sleep(3)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[1]").send_keys("1234567")
        phone_number = random.choice(["139", "131", "158"]) + "".join(random.choice("0123456789") for i in range(8))
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[2]").send_keys(phone_number)
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        self.driver.find_element_by_name("默认工作制").click()
        self.driver.find_element_by_name("完成").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        #测试拨打电话请在真机测试
        self.driver.find_element_by_name("1234567").click()
        self.driver.find_element_by_name("phone icon").click()
        self.driver.find_element_by_name("取消").click()
        sleep(3)
        #测试授权绑定手机请在真机测试
        self.driver.find_element_by_name("manage auth bt").click()
        self.driver.find_element_by_name("取消").click()
        self.driver.find_element_by_name("manage auth bt").click()
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        #测试编辑
        self.driver.find_element_by_name("manage edit bt").click()
        sleep(3)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[1]/UIAButton[1]").click()
        sleep(3)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[1]").send_keys("7654321")
        self.driver.find_element_by_name("保存").click()
        sleep(3)
        #测试删除员工
        self.driver.find_element_by_name("manage delete bt").click()
        self.driver.find_element_by_name("取消").click()
        self.driver.find_element_by_name("manage delete bt").click()
        self.driver.find_element_by_name("确认").click()
        sleep(3)
        #测试工作制
        self.driver.find_element_by_name("工作制").click()
        sleep(3)
        self.driver.find_element_by_name("添加").click()
        sleep(3)
        #测试添加工作制
        self.driver.find_element_by_name("添加工作制").click()
        sleep(3)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[1]").send_keys("7654321")
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAButton[1]").click()
        sleep(3)
        self.driver.find_element_by_name("周六").click()
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAButton[2]").click()
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        self.driver.find_element_by_name("完成").click()
        sleep(3)
        #测试编辑工作制
        self.driver.find_element_by_name("7654321").click()
        sleep(3)
        self.driver.find_element_by_name("编辑").click()
        sleep(3)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[1]").send_keys("654321")
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        #测试查看该工作制下的员工
        self.driver.find_element_by_name("查看该工作制下的员工").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        #测试为员工分配该工作制
        self.driver.find_element_by_name("为员工分配该工作制").click()
        sleep(3)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATableView[1]/UIATableCell[1]").click()
        self.driver.find_element_by_name("确认").click()
        sleep(3)
        self.driver.find_element_by_name("查看该工作制下的员工").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        sleep(3)
        #测试删除工作制
        self.driver.find_element_by_name("删除").click()
        self.driver.find_element_by_name("取消").click()
        self.driver.find_element_by_name("删除").click()
        self.driver.find_element_by_name("确定").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        self.driver.find_element_by_name("默认工作制").click()
        sleep(3)
        self.driver.find_element_by_name("为员工分配该工作制").click()
        sleep(3)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATableView[1]/UIATableCell[1]").click()
        self.driver.find_element_by_name("确认").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAScrollView[1]/UIATableView[1]/UIATableCell[1]").click()
        sleep(3)
        self.driver.find_element_by_name("删除").click()
        self.driver.find_element_by_name("确定").click()
        sleep(3)

    #测试更多模块
    def test_more(self):
        self.test_login("13133847086", "123456")
        positions = []
        positions.append((310, 630))
        self.driver.tap(positions)
        #测试修改密码
        self.driver.find_element_by_name("修改密码").click()
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIASecureTextField[1]").send_keys("123456")
        self.driver.find_element_by_name("下一步").click()
        sleep(3)
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIASecureTextField[1]").send_keys("123456")
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIASecureTextField[2]").send_keys("123456")
        self.driver.find_element_by_name("完成").click()
        sleep(3)
        #测试注销
        self.driver.find_element_by_name("注销").click()
        self.driver.find_element_by_name("取消").click()
        self.driver.find_element_by_name("注销").click()
        self.driver.find_element_by_name("确定").click()
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAScrollView[1]/UIASecureTextField[1]").send_keys("123456")
        self.driver.hide_keyboard()
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAScrollView[1]/UIAButton[1]").click()
        sleep(5)
        self.driver.tap(positions)
        #测试绑定猫宁智能终端
        self.driver.find_element_by_name("绑定猫宁智能终端").click()
        self.driver.find_element_by_name("绑定").click()
        sleep(3)
        #模拟器不能开启相机,请真机测试
        self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIATextField[1]").send_keys("123456")
        self.driver.find_element_by_name("绑定").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()
        #测试用户指南
        #测试关于
        self.driver.find_element_by_name("关于").click()
        sleep(3)
        self.driver.find_element_by_name("backNavi bt").click()

    #调试
    def test(self):
        self.test_boot_page()

    #关闭猫宁3.0
    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(MyIOSTests("test_reg_forget_password"))
    suite.addTest(MyIOSTests("test_query"))
    suite.addTest(MyIOSTests("test_manage"))
    suite.addTest(MyIOSTests("test_more"))
    #suite.addTest(MyIOSTests("test"))
    unittest.TextTestRunner(verbosity=2).run(suite)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303

PS:自动化脚本需要不断的调试,不断优化,本例是在模拟器运行iOS自动化测试,下次写下真机运行iOS自动化测试

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值