環境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元素的工具
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)
///