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)

///

推荐阅读更多精彩内容