appium+webdriveragent+Python+真机/模拟器 app自动化测试

环境xode8.3 iOS10.3.2 Mac 10.12.1
1、安装homebrew
2、安装:libimobiledevice、carthage、ios-deploy、node、xcpretty
brew install libimobiledevice --HEAD
brew install carthage
npm install -g ios-deploy
brew install node
gem install xcpretty
3、安装appium (http://appium.io)
npm install -g appium
npm install wd
npm install -g appium-doctor
appium &

可以运行appium-doctor查看环境配置
info AppiumDoctor Appium Doctor v.1.4.2
info AppiumDoctor ### Diagnostic starting ###
info AppiumDoctor ✔ The Node.js binary was found at: /usr/local/bin/node
info AppiumDoctor ✔ Node version is 8.1.3
info AppiumDoctor ✔ Xcode is installed at: /Applications/Xcode.app/Contents/Developer
info AppiumDoctor ✔ Xcode Command Line Tools are installed.
info AppiumDoctor ✔ DevToolsSecurity is enabled.
info AppiumDoctor ✔ The Authorization DB is set up properly.
info AppiumDoctor ✔ Carthage was found at: /usr/local/bin/carthage
info AppiumDoctor ✔ HOME is set to: /Users/sunny
info AppiumDoctor ✔ ANDROID_HOME is set to: /Users/sunny/Library/Android/sdk
info AppiumDoctor ✔ JAVA_HOME is set to: /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
info AppiumDoctor ✔ adb exists at: /Users/sunny/Library/Android/sdk/platform-tools/adb
info AppiumDoctor ✔ android exists at: /Users/sunny/Library/Android/sdk/tools/android
info AppiumDoctor ✔ emulator exists at: /Users/sunny/Library/Android/sdk/tools/emulator
info AppiumDoctor ✔ Bin directory of $JAVA_HOME is set
info AppiumDoctor ### Diagnostic completed, no fix needed. ###
info AppiumDoctor
info AppiumDoctor Everything looks good, bye!
info AppiumDoctor

配置Javahome、Androidhome路径 在这个文件里: open ~/.bash_profile

4、webdriveragent安装:(https://github.com/facebook/WebDriverAgent)(Facebook出的在iOS9之后基于苹果的新自动化框架写的)
appium安装好之后,在默认路径里会有webdrveragent (/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent)
命令行进入路径,然后mkdir -p Resources/WebDriverAgent.bundle
然后:sh ./Scripts/bootstrap.sh ,如果有报错的话 执行:sh ./Scripts/bootstrap.sh -d
然后xcode打开webdriveragent,选中webdriveragentrunner,点product-Test 就可以再模拟器上跑了,如果真机的话 加一下证书就可以在真机跑,会在APP加一个webdriveragentrunner的软件,也可以通过命令行:xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination 'id=D2B6B905-5AFF-48BF-8D40-1F55D728BE48' test ID就是测试机的udid
fc1e03402ba725307418d6e58c0705fad1df63e9

启动成功:ServerURLHere->http://172.16.80.162:8100<-ServerURLHere

5、下载Python代码:
Python-client 的环境配置 地址:https://github.com/appium/python-client
git clone git@github.com:appium/python-client.git
cd python-client
python setup.py install

环境基本上配置完了,然后可以下载appium的例子来跑
https://github.com/appium/sample-code

找到里面的TestApp项目,打包生成一个.app的包,然后写一段Python

-- coding:utf-8 --

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

class SimpleIOSTests(unittest.TestCase):

def setUp(self):
    # set up appium
    app = os.path.abspath('/Users/sunny/Desktop/TestApp.app')
    self.driver = webdriver.Remote(
        command_executor='http://127.0.0.1:4723/wd/hub',
        desired_capabilities={
            'app': app,
            'platformName': 'iOS',
            'platformVersion': '10.3.2',
            'deviceName': 'iPhone 6s Plus',
            'automationName':'XCUITest',
            'udid':'fc1e03402ba725307418d6e58c0705fad1df63e9'
        })
def testsend(self):
    self.driver.find_element_by_accessibility_id("IntegerA").send_keys("186****1761"),
    self.driver.find_element_by_accessibility_id("IntegerB").send_keys("wsg123")
    sleep(3)

if name == 'main':
suite = unittest.TestSuite()
suite.addTest(SimpleIOSTests("testsend"))
unittest.TextTestRunner(verbosity=2).run(suite)

准备工作完毕,然后开始跑任务:
首先安装webdriveragent到手机,
然后命令行输入appium & 启动appium服务,
然后新建一个命令行,在命令行输入:Python 文件路径 就可以了。

6、appium-desktop
可以查看APP元素的工具


屏幕快照 2017-07-06 下午2.54.57.png

屏幕快照 2017-07-06 下午2.55.26.png

7、遇到的问题:Google (百度搜索出来的答案都不对)
webdriverexception: message: parameters were incorrect. we wanted {"required":["value"]} and you sent ["text","sessionid","id","value”]报这个错误,找到原因是selenium版本的问题,把selenium版本降到3.0.1 就可以了
首先要先到Python的第三方目录里面(pip show selenium 查看路径) /Library/Python/2.7/site-packages删除老版本的selenium,然后安装要安装的版本pip install selenium==3.0.1

pip是Python包管理工具

//报错:
[debug] [XCUITest] Connection to WDA timed out
[debug] [iProxy] recv failed: Operation not permitted

解决: 在命令行输入:
ps aux|grep fc1e03402ba725307418d6e58c0705fad1df63e9|grep -v grep|awk '{print $2}'|xargs kill -9

fc1e03402ba725307418d6e58c0705fad1df63e9 是设备的udid

///

-- coding:utf-8 --

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

class MyIOSTests(unittest.TestCase):
def setUp(self):
app = os.path.abspath('/Users/sunny/Desktop/appiumTest/test.app')
self.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities={
'app': app,
'platformName': 'iOS',
'platformVersion': '10.3.2',
'deviceName': 'iPhone 6s Plus',
'automationName':'XCUITest',
'udid': 'auto',
'noReset': True,
'clearSystemFiles': True
})

def scrollLeft(self) :
    x=self.driver.get_window_size()['width']
    y=self.driver.get_window_size()['height']
    self.driver.swipe(x/4*3, y/2, x/4*1, y/2, 500)

def testLogin(self):
    
    sleep(2)
    self.driver.find_element_by_name("允许").click()
    sleep(1)
    scrollLeft()
    sleep(1)
    self.driver.find_element_by_xpath('//XCUIElementTypeButton[@name=\"跳过 >>\"]').click()
    sleep(1)
    self.driver.find_element_by_xpath('//XCUIElementTypeButton[@name=\"xiaoxi\"]').click()
    sleep(1)
    self.driver.find_element_by_xpath('//XCUIElementTypeApplication[@name=\"百汇金服\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]/XCUIElementTypeTextField').send_keys("186****1761")
    sleep(1)
    self.driver.find_element_by_xpath('//XCUIElementTypeApplication[@name=\"百汇金服\"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[2]/XCUIElementTypeSecureTextField').send_keys("wsg123")
    sleep(1)
    self.driver.hide_keyboard('Done')
    sleep(1)
    self.driver.find_element_by_xpath('//XCUIElementTypeButton[@name=\"登录\"]').click()
    sleep(1)

if name == 'main':
suite = unittest.TestSuite()
suite.addTest(MyIOSTests('testLogin'))
suite.addTest(MyIOSTests('scrollLeft'))
unittest.TextTestRunner(verbosity=2).run(suite)

///

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容