appium自动化环境搭建怎么利用sdk取app元素 执行命令

Appium是一个开源、跨平台的测试框架,可以用来测试原生及混合的移动端应用。App..
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
Appium调研及使用报告
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer--144.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口appium+java+ios自动化环境的搭建部分代码,资料的搜集与整合。
appium+java+ios自动化环境的搭建部分代码,资料的搜集与整合。
[摘要:对情况的需供: http://appium.io/ 上的道法: iOS ?Mac OSX 10.7 ?XCode 4.5 战 Command Line Tools /appium/appium 的道法: npm 0.8 or greater ?Mac OS X 10.7 or higher, 10.8.4 recommended ?XCode = 4.6.3]
对环境的需求:
http://appium.io/&上的说法:
iOS?Mac&OSX&<span style="color:#.7&#43;?XCode&<span style="color:#.5&#43;&和&Command&Line&Tools
/appium/appium的说法:
npm&<span style="color:#.8&or&greater?Mac&OS&X&<span style="color:#.7&or&higher,&<span style="color:#.8.4&recommended?XCode&&=&<span style="color:#.6.3
<span style="color:#.png
<span style="color:#.png
<span style="color:#.png
xcode6:没有此信息:
node.js安装:
第一步,从nodejs.org&中下载Node.js
forMac 安装包,也就是一个<span style="color:#M多的pkg文件,下载之后点击安装即可。
& & &安装完成之后我们关闭窗口,接下来我们打开“终端”,并且输入命令:
curl https://npmjs.org/install.sh | sh
&& &这个npm.js实际上是Node.js的套件管理工具,执行完成之后我们继续执行下面都命令:
sudo&npmupdate npm -g
&& &这一步是更新npm,全部完成之后我们使用打印版本命令来看一下npm是否安装成功,如果不行大家就多试几次。我的版本输出结果:
&& & 我当前版本为1.1.12
&& & 最后,还是为大家写一个helloword吧!复制下面代码到记事本。
var http =require(http);&
http.createServer(function(req, res) {&
& res.writeHead(200, {Content-Type:text/plain});&
& res.end(Hello World
}).listen(.0.1&);&
console.log(Serverrunning at http://127.0.0.1:1337/);
&& & 然后在终端里面敲入命令 nodehe.js
这里都he.js是你javascript文件都路径回车就OK了!终端会显示如图:
& & & 在浏览器中输入网址与端口并访问,你就可以看到效果了!
安装:在mac上打开shell
#sudo npm install -g appium  //加上sudo以防Permission的问题
#npm installwd  //这个还不清楚有什么影响??
appium 在模拟器中跑,只需要注意:
<span style="color:#:app=path找到编译后app所在的位置,如:注意Release-iphonesimulator这个是模拟器编译后的文件夹
& & final File appDir = newFile(System.getProperty(&user.dir&),?& & & &&../../../apps/TestApp/build/Release-iphonesimulator&);?& &final File app = new File(appDir, &TestApp.app&);?& &capabilities.setCapability(&app&,
app.getAbsolutePath());
appium真机上运行:
<span style="color:#:运行的时候要将APP安装到真机上面
<span style="color:#:运行的时候,不能再开instrument&
<span style="color:#:命令行里面启动的时候,参数写错了应该是appium -U 3d2cad5aa98a2cccddd1c --app io.appium &然后代码里面app=bundleID,如以下例子中:&
capabilities.setCapability(&app&,&io.appium.TestApp&);而不是那个很长的路径,其中-U参数后面是真机的uuid,可以打开xcode,在windows下的organise下找到,一般mac连接上ipad后会有绿色圆点
现在贴上真机上运行python实例:
Kingsons-MacBook:Study kingson$appium
-U <span style="color:#a601f5cd791bcfc81cd69b
--app com.telenavsoftware.doudouy
info: Welcome to Appium v0.<span style="color:#.4 (REVb04decde9bfcd04a036)
info: Appium REST http
listener started on
<span style="color:#.0.<span style="color:#.0:<span style="color:#23
&&info& - socket.iostarted
debug: Appium request initiated at
/wd/hub/session
debug: Request received with params:{&sessionId&:null,&desiredCapabilities&:{&device&:&iPhone
Simulator&,&platform&:&Mac&,&browserName&:&iOS&,&version&:&7.0&,&app&:&com.telenavsoftware.doudouy&}}
info: App is an iOS bundle, willattempt to run
as pre-existing
info: Creating new appium session a36fdf63-<span style="color:#b22-<span style="color:#a4-bb01-<span style="color:#b44ddb54aff
info: Removing any remaining instruments sockets
info: Cleaned up instruments socket
/tmp/instruments_sock
info: Cleaning up any tracedirs
info: No tracedirs to clean up
info: Localizable.strings
is not currentlysupported when using real devices.
info: Not setting device
type since we're connected to a device
info: Starting iOS device log capture viaidevicesyslog
debug: Real device specified but no ipa,assuming bundle ID is on device
debug: Creating instruments
info: instruments is:/Applications/Xcode.app/Contents/Developer/usr/bin/instruments
info: [INSTSERVER] Instruments socketserver started at /tmp/instruments_sock
info: Attempting to run app on real devicewith UDID 1a601f5cd791bcfc81cd69b
info: Spawning instruments with command:/Applications/Xcode.app/Contents/Developer/usr/bin/instruments -t/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate-w
1a601f5cd791bcfc81cd69b com.telenavsoftware.doudouy -eUIASCRIPT /usr/local/lib/node_modules/appium/lib/devices/ios/uiauto/bootstrap.js -e UIARESULTSPATH/tmp/appium-instruments/
info: And extra without-delay env: {}
info: And launch timeout: 90000ms
info: [INST STDERR]
10:40:22.536instruments[] Connection to the remote device lost while launchingtarget. Aborting...
info: [INST STDERR]
10:40:22.539instruments[] Recording cancelled : At least one ta aborting run
info: [INST STDERR] Instruments Trace Error: Error Domain=com.apple.instruments Code=1 &Error StartingRecording& UserInfo=0x7ff6d308f220 {NSLocalizedDescription=Error StartingRecording, NSLocalizedRecoverySuggestion=At
least one taraborting run}
Instruments Trace Error : Failed to starttrace.
info: [INSTSERVER] Instruments exited withcode 253
error: Instruments did not launchsuccessfully, failing session
info: Cleaning up appium session
error: Failed to start an Appium session, errwas: Error: Instruments did not launch successfully--please check your apppaths or bundle IDs and try again
info: Responding to client with error:{&status&:33,&value&:{&message&:&A newsession could not be created. (Original error: Instruments did not launchsuccessfully--please check your app paths or bundle IDs and tryagain)&,&origValue&:&Instruments
did not launchsuccessfully--please check your app paths or bundle IDs and tryagain&},&sessionId&:null}
POST /wd/hub/session 500 6015ms - 342b```
我的部分代码片段:
classTestSequenceFunctions(unittest.TestCase):
&&&def setUp(self):
&&&&&&&# set up appium
&&&&&&&app = &com.telenavsoftware.doudouy&
&&&&&&&#app = os.path.join(os.path.dirname(__file__),
&&&&&&&#&&&&&&&&&&&&&&&&&& '../../apps/DDY/build/',
&&&&&&&#&&&&&&&&&&&&&&&&&& 'DouDouY.app')
&&&&&&&#app = os.path.abspath(app)
&&&&&&&self.driver = webdriver.Remote(
&&&&&&&&&&&command_executor='http://<span style="color:#7.0.<span style="color:#.1:<span style="color:#23/wd/hub',
&&&&&&&&&&&desired_capabilities={
&&&&&&&&&&&&&&& 'browserName': 'iOS',
&&&&&&&&&&&&&&& 'device': 'iPhone Simulator',
&&&&&&&&&&&&&&& 'platform': 'Mac',
&&&&&&&&&&&&&&& 'version': '<span style="color:#.0',
&&&&&&&&&&&&&&& 'app': app
&&&&&&&&&&&})```
1.启动Appium时,保证你的Real Device的UDID和你的应用的Bundle
2.确保你的应用可以安装到你的真机上
3.安装后请断开与Xcode的连接
4.确保Mac上没有启动Instruments工具
以下是JAVA真机运行log:
devexus-iMac:build devexu$ appium -U3d2cad5aa98a2cccddd1c --app io.appium
info: Welcome to Appium v1.1.0 (REVe433bbcdb2bcb32117)
info: Appium REST http interface listener started on 0.0.0.0:4723
info: socket.io started
info: Non-default server args:{&app&:&io.appium&,&udid&:&3d2cad5aa98a2cccddd1c&}
debug: Appium request initiated at /wd/hub/session
debug: Request received with params:{&desiredCapabilities&:{&platformVersion&:&7.1&,&app&:&io.appium.TestApp&,&platformName&:&iOS&,&deviceName&:&iPhoneSimulator&,&browserName&:&&}}
info: App is an iOS bundle, will attempt to run as pre-existing
info: Creating new appium session 2bcd619b-b7fb-45f5-b20c-79caff8cd24f
info: Removing any remaining instruments sockets
info: Cleaned up instruments socket /tmp/instruments_sock
info: Cleaning up any tracedirs
info: No tracedirs to clean up
info: Setting Xcode folder
info: Setting Xcode version
info: Setting iOS SDK Version
info: iOS SDK Version set to 7.1
info: Detecting automation tracetemplate
info: Not auto-detecting udid, running on sim
info: Localizable.strings is not currently supported when using realdevices.
info: Not setting locale because we're using a real device
debug: Creating instruments
info: Not setting iOS and app preferences since we're on a real device
info: Starting iOS device log capture via idevicesyslog
info: Not setting device type since we're connected to a device
debug: Real device specified but no ipa or app path, assuming bundle ID ison device
debug: Starting instruments
info: Instruments is at:/Applications/Xcode.app/Contents/Developer/usr/bin/instruments
info: [INSTSERVER] Instruments socket server started at/tmp/instruments_sock
info: Attempting to run app on real device with UDID3d2cad5aa98a2cccddd1c
info: Spawning instruments with command: /Applications/Xcode.app/Contents/Developer/usr/bin/instruments-t/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate-w
3d2cad5aa98a2cccddd1c io.appium.TestApp -e UIASCRIPT/usr/local/lib/node_modules/appium/node_modules/appium-uiauto/uiauto/bootstrap.js-e UIARESULTSPATH /tmp/appium-instruments
info: And extra without-delay env: {}
info: And launch timeouts (in ms): {&global&:90000}
info: [INST]
01:29:30 &#43;0000 Start: Bootstrapping uiauto
info: [INST]
01:29:32 &#43;0000 Start: Got user: devexu
info: [INST]
01:29:33 &#43;0000 Start: Not using settings file.
info: [INST]
01:29:33 &#43;0000 Start: isVerbose:false
info: [INST]
01:29:37 &#43;0000 Start: Using node at:/usr/local/bin/node
info: [INSTSERVER] Instruments is ready to receive commands
info: Instruments launched. Starting poll loop for new commands.
info: Setting bootstrap config keys/values
info: Pushing command to appium work queue: &setBootstrapConfig:autoAcceptAlerts=false&
debug: Sending command to instruments: setBootstrapConfig:autoAcceptAlerts=false
info: [INSTSERVER] Socket data received (15 bytes)
info: [INSTSERVER] Socket data being routed for 'cmd' event
info: [INSTSERVER] Sending command to instruments: setBootstrapConfig:autoAcceptAlerts=false
info: [INSTSERVER] Socket data received (48 bytes)
info: [INSTSERVER] Socket data being routed for 'cmd' event
info: [INSTSERVER] Got result from instruments:{&status&:0,&value&:&&}
info: Setting initial orientation to PORTRAIT
info: Pushing command to appium work queue:&au.setScreenOrientation('PORTRAIT')&
debug: Sending command to instruments: au.setScreenOrientation('PORTRAIT')
info: [INSTSERVER] Sending command to instruments:au.setScreenOrientation('PORTRAIT')
info: [INST]
01:29:41 &#43;0000 Debug:target.setDeviceOrientation(&1&)
info: [INSTSERVER] Socket data received (56 bytes)
info: [INSTSERVER] Socket data being routed for 'cmd' event
info: [INSTSERVER] Got result from instruments:{&status&:0,&value&:&PORTRAIT&}
info: Device launched! Ready for commands
info: Setting command timeout to the default of 60 secs
info: Appium session started with sessionId2bcd619b-b7fb-45f5-b20c-79caff8cd24f
POST /wd/hub/session 303 22483ms - 9b
debug: Appium request initiated at/wd/hub/session/2bcd619b-b7fb-45f5-b20c-79caff8cd24f
debug: Request received with params: {}
info: Responding to client with success: {&status&:0,&value&:{&webStorageEnabled&:false,&locationContextEnabled&:false,&browserName&:&&,&platform&:&MAC&,&javascriptEnabled&:true,&databaseEnabled&:false,&takesScreenshot&:true,&warnings&:{},&desired&:{&platformVersion&:&7.1&,&app&:&io.appium.TestApp&,&platformName&:&iOS&,&deviceName&:&iPhoneSimulator&,&browserName&:&&},&platformVersion&:&7.1&,&app&:&io.appium.TestApp&,&platformName&:&iOS&,&deviceName&:&iPhoneSimulator&},&sessionId&:&2bcd619b-b7fb-45f5-b20c-79caff8cd24f&}
..........
java真机运行的代码:
package com.saucelabs.???import staticorg.junit.Assert.assertT?import io.appium.java_client.AppiumD?importio.appium.java_client.MobileBy;???import java.net.URL;?importjava.util.ArrayL?import java.util.L?import
java.util.R???importorg.junit.A?import org.junit.B?import org.junit.T?importorg.openqa.selenium.By;?import org.openqa.selenium.D?importorg.openqa.selenium.P?import org.openqa.selenium.WebE?importorg.openqa.selenium.remote.CapabilityT?importorg.openqa.selenium.remote.DesiredC???/**?&*
Simple &ahref=&/appium/appium&&Appium&/a& testwhich runs?&* against a local Appium instance deployed with the 'TestApp'iPhone project?&* which is included in the Appium sourcedistribution.?&*&?&* @author Ross Rowe?&*/?public classWiMyOffice
{???& private AppiumDriver & &???&private List&Integer& & &???& private static finalint MINIMUM = 0;?& private static final int MAXIMUM = 10;???& privatePoint getCenter(final WebElement element) {???& & final PointupperLeft = element.getLocation();?&
& final Dimension dimensions =element.getSize();?& & return new Point(upperLeft.getX() &#43;dimensions.getWidth() / 2,?& & & & upperLeft.getY() &#43;dimensions.getHeight() / 2);?& }???& private void populate() {?&& // populate text fields with two random number?&
& finalList&WebElement& elems = driver.findElements(By?& & && .className(&UIATextField&));?& & final Random random= new Random();?& & for (final WebElement elem : elems) {?&& & final int rndNum = random.nextInt(MAXIMUM - MINIMUM &#43; 1) &#43;MINIMUM;?& & & elem.sendKeys(String.valueOf(rndNum));?&&
& values.add(rndNum);?& & }?& }???& @Before?& public void setUp() throws Exception{?& & // set up appium???& & final DesiredCapabilitiescapabilities = new DesiredCapabilities();?& &capabilities.setCapability(CapabilityType.BROWSER_NAME,
&&);?&& capabilities.setCapability(&platformVersion&,&7.1&);?& & capabilities.setCapability(&platformName&,&iOS&);?& & capabilities.setCapability(&deviceName&,&iPhone Simulator&);?& &capabilities.setCapability(&app&,&io.appium.TestApp&);?& & driver = new AppiumDriver(newURL(&http://127.0.0.1:4723/wd/hub&),?&
& & &capabilities);?& & values = new ArrayList&Integer&();?& }???& @After?& public void tearDown() throws Exception {?&& driver.quit();?& }???& @Test?& public voidtestHideKeyboard() throws Exception {?& &driver.findElement(By.xpath(&//UIATextField[1]&)).sendKeys(&12&);???&&
final WebElement button = driver.findElement(MobileBy?& && & .AccessibilityId(&Done&));?& &assertTrue(button.isDisplayed());???& & button.click();?& }???}
java上在模拟器上跑的实例:
命令行输入:appium&启动服务就可以了
lsof -i:4723查看端口号有没有被占用,占用就用 kill -9
package com.saucelabs.???importstatic org.junit.Assert.assertT?importio.appium.java_client.AppiumD???import java.io.F?importjava.net.URL;?import java.util.ArrayL?import java.util.L?importjava.util.R???import
org.junit.A?import org.junit.B?importorg.junit.T?import org.openqa.selenium.By;?importorg.openqa.selenium.D?import org.openqa.selenium.P?importorg.openqa.selenium.WebE?importorg.openqa.selenium.remote.CapabilityT?importorg.openqa.selenium.remote.DesiredC???/**?&*
Simple &ahref=&/appium/appium&&Appium&/a& testwhich runs?&* against a local Appium instance deployed with the 'TestApp'iPhone project?&* which is included in the Appium source distribution.?&*&?&*@author Ross Rowe?&*/?public class SimpleTest
{???& privateAppiumDriver & &???& private List&Integer& &&???& private static final int MINIMUM = 0;?& privatestatic final int MAXIMUM = 10;???& private Point getCenter(finalWebElement element) {???& & final Point upperLeft =element.getLocation();?&
& final Dimension dimensions =element.getSize();?& & return new Point(upperLeft.getX() &#43;dimensions.getWidth() / 2,?& & & & upperLeft.getY() &#43;dimensions.getHeight() / 2);?& }???& private void populate() {?&& // populate text fields with two random number?&
& finalList&WebElement& elems = driver.findElements(By?& & && .className(&UIATextField&));?& & final Random random= new Random();?& & for (final WebElement elem : elems) {?&& & final int rndNum = random.nextInt(MAXIMUM - MINIMUM &#43; 1) &#43;MINIMUM;?& & & elem.sendKeys(String.valueOf(rndNum));?&&
& values.add(rndNum);?& & }?& }???& @Before?& public void setUp() throws Exception {?& &// set up appium?& & final File appDir = newFile(System.getProperty(&user.dir&),?& & & &&../../../apps/TestApp/build/Release-iphonesimulator&);?&
&final File app = new File(appDir, &TestApp.app&);?& & finalDesiredCapabilities capabilities = new DesiredCapabilities();?& &capabilities.setCapability(CapabilityType.BROWSER_NAME, &&);?&& capabilities.setCapability(&platformVersion&,&7.1&);?& &capabilities.setCapability(&platformName&,
&iOS&);?&& capabilities.setCapability(&deviceName&, &iPhoneSimulator&);?& & capabilities.setCapability(&app&,app.getAbsolutePath());?& & driver = new AppiumDriver(newURL(&http://127.0.0.1:4723/wd/hub&),?& & & &capabilities);?& & values = new ArrayList&Integer&();?&
}???& @After?& public void tearDown() throws Exception {?&& driver.quit();?& }???& @Test?& public void testActive()throws Exception {?& & final WebElement text =driver.findElement(By.xpath(&//UIATextField[1]&));?& &assertTrue(text.isDisplayed());???&
& final WebElement button =driver.findElement(By.xpath(&//UIAButton[1]&));?& &assertTrue(button.isDisplayed());?& }?&?}
感谢关注 Ithao123Java频道,是专门为互联网人打造的学习交流平台,全面满足互联网人工作与学习需求,更多互联网资讯尽在 IThao123!
Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。
Hadoop是一个由Apache基金会所开发的分布式系统基础架构。
用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。
Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上;而且它提供高吞吐量(high throughput)来访问应用程序的数据,适合那些有着超大数据集(large data set)的应用程序。HDFS放宽了(relax)POSIX的要求,可以以流的形式访问(streaming access)文件系统中的数据。
Hadoop的框架最核心的设计就是:HDFS和MapReduce。HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了计算。
产品设计是互联网产品经理的核心能力,一个好的产品经理一定在产品设计方面有扎实的功底,本专题将从互联网产品设计的几个方面谈谈产品设计
随着国内互联网的发展,产品经理岗位需求大幅增加,在国内,从事产品工作的大部分岗位为产品经理,其实现实中,很多从事产品工作的岗位是不能称为产品经理,主要原因是对产品经理的职责不明确,那产品经理的职责有哪些,本专题将详细介绍产品经理的主要职责
IThao123周刊因项目需要,开始研究Android自动化测试方法,对其中的一些工具、方法和框架做了一些简单的整理,其中包括Monkey、Monkeyrunner、Athrun、appium,目前仍在了解学习android测试框架、CTS等。本文主要为前段时间学习总结,由于刚接触不久,故本文难免会有些肤浅,大神请绕走~
1、Monkeyrunner简介
monkeyrunner即android SDK中自带的工具之一,此工具提供API可按制android设备或模拟器。
monkeyrunner提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner,您可以写出一个Python程序去安装一个Android应用程序或测试包,运行它,向它发送模拟击键,截取它的用户界面图片,并将截图存储于工作站上。
monkeyrunner工具的主要目的是用于测试功能/框架水平上的应用程序和设备,或用于运行单元测试套件,但您当然也可以将其用于其它目的。
2、Monkeyrunner工具特性
多设备控制:monkeyrunner API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器(或统统一起),依据程序依次连接到每一个,然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器,运行一个或多个测试,然后关闭模拟器。
功能测试:monkeyrunner可以为一个应用自动化功能测试。为您提供按键或触摸事件的输入数值,然后观察输出结果的截屏。
回归测试:monkeyrunner可以运行某个应用,并将其结果截屏与既定已知正确的结果截屏相比较,以此测试应用的稳定性。
可扩展的自动化:由于monkeyrunner是一个API工具包,您可以基于Python模块和程序开发一整套系统,以此来控制Android设备。除了使用monkeyrunner
API之外,您还可以使用标准的Python
os和subprocess模块来调用如adb这样的Android工具。
官方原文描述请见:
3、Monkeyrunner工具同Monkey工具的差别
Monkey工具直接运行在设备或模拟器的adb shell中,生成用户或系统的伪随机事件流。
Monkeyrunner:
Monkeyrunner工具是在工作站上通过API定义的特定命令和事件控制设备或模拟器。
4、运行Monkeyrunner
Monkeyrunner API
主要包括三个模块
1、MonkeyRunner:这个类提供了用于连接monkeyrunner和设备或模拟器的方法,它还提供了用于创建用户界面显示提供了方法。
2、MonkeyDevice:代表一个设备或模拟器。这个类为安装和卸载包、开启Activity、发送按键和触摸事件、运行测试包等提供了方法。
3、MonkeyImage:这个类提供了捕捉屏幕的方法。这个类为截图、将位图转换成各种格式、对比两个MonkeyImage对象、将image保存到文件等提供了方法。
#引用导入API
from com.android.monkeyrunner import
运行monkeyrunner
命令语法为:
monkeyrunner -plugin
方式一:在CMD命令窗口直接运行monkeyrunner
方式二:使用Python编写测试代码文件,在CMD中执行monkeyrunner Findyou.py运行
不论使用哪种方式,您都需要调用SDK目录的tools子目录下的monkeyrunner命令。
注意:在运行monkeyrunner之前必须先运行相应的模拟器或连接真机,否则monkeyrunner无法连接到设备
运行模拟器有两种方法:1、通过eclipse中执行模拟器 2、在CMD中通过命令调用模拟器
这里介绍通过命令,在CMD中执行模拟器的方法
emulator -avd test
上面命令中test是指模拟器的名称。
问题:CMD运行提示monkeyrunner不是内部或外部命令,也不是可运行的程序或批处理文件。
解决:电脑环境变量未配置,将monkeyrunner所在目录配在环境变量里。
变量名:Path
变量值:D:ndroidndroid-sdk-windows D:ndroidndroid-sdk-windowsplatform-tools
实例一:卸载旧的APP,安装新的APP
a.连接安卓真机设备
b.运行CMD,检测是否连接成功
CMD>adb devices
问题:CMD运行提示adb不是内部或外部命令,也不是可运行的程序或批处理文件。
解决:电脑环境变量未配置,将adb所在目录配在环境变量里。
1.打开CMD,运行monkeyrunner
2.进入monkeyrunner的shell命令交互模式后,逐条输入以下命令
1 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
2 device = MonkeyRunner.waitForConnection()
3 device.removePackage('cn.richinfo.thinkdrive')
4 device.installPackage('E:\JAVA\monkeyrunner\Test1\ThinkDrive_new.apk')
注:每条命令的作用,请见方法二中的注解
实操如图:
c.检查手机app是否已更新
a.编写Python测试代码
# Vision: V1.0
# Author: Findyou
# 引入本程序所用到的模块
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# 连接手机设备
device = MonkeyRunner.waitForConnection()
result = device.takeSnapshot()
# 将截图保存到文件
result.writeToFile('E:\JAVA\monkeyrunner\Test1\Test1_001.png','png')
device.removePackage('cn.richinfo.thinkdrive')
print ('Uninstall Success!')
MonkeyRunner.sleep(5)
result = device.takeSnapshot()
result.writeToFile('E:\JAVA\monkeyrunner\Test1\Test1_002.png','png')
# 安装新的APP
device.installPackage('E:\JAVA\monkeyrunner\Test1\ThinkDrive_new.apk')
print ('Install Success!')
result = device.takeSnapshot()
result.writeToFile('E:\JAVA\monkeyrunner\Test1\Test1_003.png','png')
注:拷贝运行时请去掉中文注释;或者在开头加入 #coding=utf-8
b.执行Test1.py脚本
Monkeyrunner E:JAVAmonkeyrunnerTest1Test1.py
实操如图:
c.检查手机app是否已更新
6、学习笔记
#引入程序所用的模块
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
#引入程序所用的模块,使用别名
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner import MonkeyDevice as md
from com.android.monkeyrunner import MonkeyImage as mi
#如果给导入的模块起了别名,就必须使用别名,否则会出现错误。
#比如连接设备或模拟器,起了以上别名后,命令应该如下:
device=mr.waitForConnection()
#连接到设备或模拟器
#参数1:超时时间,单位秒,浮点数。默认是无限期地等待。
#参数2:串deviceid,指定的设备名称。默认为当前设备(手机优先,比如手机通过USB线连接到PC、其次为模拟器)。
#默认连接:
device = MonkeyRunner.waitForConnection()
#参数连接:
device = MonkeyRunner.waitForConnection(1.0,'4df74b8XXXXXXX')
#向设备或模拟器安装APK
#以下两种方式都是对的
device.installPackage('E:/JAVA/monkeyrunner/Test1/ThinkDrive_new.apk')
device.installPackage('E:\JAVA\monkeyrunner\Test1\ThinkDrive_new.apk')
#参数可以为绝对路径,也可为相对路径
#卸载设备或模拟器中的APK
#参数为APK包名
device.removePackage('cn.richinfo.thinkdrive')
#启动任意的Activity
#device.startActivity(component="包名/启动Activity")
#以下两种都OK
device.startActivity(component="cn.richinfo.thinkdrive/cn.richinfo.thinkdrive.ui.activities.NavigateActivity")
device.startActivity(component="cn.richinfo.thinkdrive/.ui.activities.NavigateActivity")
#获取设备的屏蔽缓冲区,产生了整个显示器的屏蔽捕获。(截图)
result=device.takeSnapshot()
#返回一个MonkeyImage对象(点阵图包装),我们可以用以下命令将图保存到文件
result.writeToFile('E:\JAVA\monkeyrunner\Test1\Test1_001.png','png')
#暂停目前正在运行的程序指定的秒数
#MonkeyRunner.sleep(秒数,浮点数)
MonkeyRunner.sleep(5)
#字符串发送到键盘
#device.type('字符串')
device.type('Findyou')
#唤醒设备屏幕
#锁屏后,屏幕关闭,可以用下命令唤醒
device.wake()
device.reboot()
#device.drag(X,Y,D,S)
#X 开始坐标
#Y 结束坐标
#D 拖动持续时间(以秒为单位),默认1.0秒
#S 插值点时要采取的步骤。默认值是10
device.drag((100,1053),(520,,10)
#在指定位置发送触摸事件
#device.touch(x,y,触摸事件类型)
#x,y的单位为像素
#触摸事件类型,请见下文中Findyou对device.press描述
device.touch(520,520,'DOWN_AND_UP')
#发送指定类型指定键码的事件
#device.press(参数1:键码,参数2:触摸事件类型)
#参数1:见android.view.KeyEvent
#参数2,如有TouchPressType()返回的类型-触摸事件类型,有三种。
#1、DOWN 发送一个DOWN事件。指定DOWN事件类型发送到设备,对应的按一个键或触摸屏幕上。
#2、UP 发送一个UP事件。指定UP事件类型发送到设备,对应释放一个键或从屏幕上抬起。
#3、DOWN_AND_UP 发送一个DOWN事件,然后一个UP事件。对应于输入键或点击屏幕。
以上三种事件做为press()参数或touch()参数
#按下HOME键
device.press('KEYCODE_HOME',MonkeyDevice.DOWN_AND_UP)
#按下BACK键
device.press('KEYCODE_BACK',MonkeyDevice.DOWN_AND_UP)
#按下下导航键
device.press('KEYCODE_DPAD_DOWN',MonkeyDevice.DOWN_AND_UP)
#按下上导航键
device.press('KEYCODE_DPAD_UP',MonkeyDevice.DOWN_AND_UP)
device.press('KEYCODE_DPAD_CENTER',MonkeyDevice.DOWN_AND_UP)
home键 KEYCODE_HOME
back键 KEYCODE_BACK
send键 KEYCODE_CALL
end键 KEYCODE_ENDCALL
上导航键 KEYCODE_DPAD_UP
下导航键 KEYCODE_DPAD_DOWN
左导航 KEYCODE_DPAD_LEFT
右导航键 KEYCODE_DPAD_RIGHT
ok键 KEYCODE_DPAD_CENTER
上音量键 KEYCODE_VOLUME_UP
下音量键 KEYCODE_VOLUME_DOWN
power键 KEYCODE_POWER
camera键 KEYCODE_CAMERA
menu键 KEYCODE_MENU
更多KeyCode
更多Monkeyrunner学习:
转自:/findyou/p/3420936.html
原文链接:,转发请注明来源!
分类列表(11)

我要回帖

更多关于 appium自动化原理 的文章

 

随机推荐