今天尝试用monkeyrunner脚本在夜神模拟器上安装并截图QQ,但是遇到了一些问题:

 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

 #连接模拟器
 device = MonkeyRunner.waitForConnection()

 #打开锁屏
 device.drag((355,1061),(621,1061),3,1)

 #安装QQ
 device.installPackage("F:\\QQ_730.apk")
 MonkeyRunner.sleep(8)

 #启动QQ
 device.startActivity(component="com.tencent.mobileqq/.activity.LoginActivity")
 MonkeyRunner.sleep(5)

 #点击登录
 device.touch(180,1170,"DOWN_AND_UP")
 MonkeyRunner.sleep(3)

 #保存截图
 result = device.takeSnapshot()
 result.writeToFile("qq.png","png")

无奈刚开始就报错了。

"SyntaxError: Non-ASCII character in file 'C:\Users\Administrator\qq.py'

这是一个语法错误,原因可能是我们脚本的注释用的是中文。我们可以在脚本开始加上#-*-coding:utf-8-*-

 #-*-coding:utf-8-*-

 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

 #连接模拟器
 device = MonkeyRunner.waitForConnection()

 #打开锁屏
 device.drag((355,1061),(621,1061),3,1)

 #安装QQ
 device.installPackage("F:\\QQ_730.apk")
 MonkeyRunner.sleep(8)

 #启动QQ
 device.startActivity(component="com.tencent.mobileqq/.activity.LoginActivity")
 MonkeyRunner.sleep(5)

 #点击登录
 device.touch(180,1170,"DOWN_AND_UP")
 MonkeyRunner.sleep(3)

 #保存截图
 result = device.takeSnapshot()
 result.writeToFile("qq.png","png")

可是,又报错了:

再次尝试:

 import sys
 reload(sys)
 sys.setdefaultencoding("utf-8")

 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

 #连接模拟器
 device = MonkeyRunner.waitForConnection()

 #打开锁屏
 device.drag((355,1061),(621,1061),3,1)

 #安装QQ
 device.installPackage("F:\\QQ_730.apk")
 MonkeyRunner.sleep(8)

 #启动QQ
 device.startActivity(component="com.tencent.mobileqq/.activity.LoginActivity")
 MonkeyRunner.sleep(5)

 #点击登录
 device.touch(180,1170,"DOWN_AND_UP")
 MonkeyRunner.sleep(3)

 #保存图片
 result = device.takeSnapshot()
 result.writeToFile("qq.png","png")

值得注意的两点是:

1.  检查夜神模拟器有没有连上:adb devices

如果没有连上,用adb connect 127.0.0.1:62001连接,再用adb devices检查。(详见lynnLi的博客monkeyrunner之夜神模拟器的安装与使用(二)

2.  一定要用好MonkeyRunner.sleep()

在实际的测试过程中,有的时候发现脚本业务逻辑是正确的,可是不知道为什么一旦执行起来结果却是错误的,那么有一种可能就是你的操作过快,导致界面元素没有完全展示出来就开始了后续操作,从而引起的问题。(引自《精通移动App测试实战:技术、工具和案例》,于涌 王磊 曹向志编著)

====================================9月26日更新======================================

不知是Eclipse出错概率多,还是我的python脚本问题,今天写了一段python脚本,在Elipse中运行也出现了相同的错误,解决的办法却不一样:

 '''
 Created on 2017-9-26

 @author: Administrator
 '''

 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

 #连接设备
 device = MonkeyRunner.waitForConnection()
 MonkeyRunner.sleep(5)

 #启动QQ
 device.startActivity(component="com.tencent.mobileqq/.activity.SplashActivity")
 MonkeyRunner.sleep(5)

 #点击动态
 device.touch(596, 1112, "DOWN_AND_UP")
 MonkeyRunner.sleep(3)

 #点击好友动态
 device.touch(119, 296, "DOWN_AND_UP")
 MonkeyRunner.sleep(5)

 #向下滑
 device.drag((288, 189), (288, 947), 1, 10)
 MonkeyRunner.sleep(3)

 #向上滑
 device.drag((288, 947), (288, 189), 1, 10)
 MonkeyRunner.sleep(3)

 #回到桌面
 device.touch(414, 738, "DOWN-AND_UP")
 MonkeyRunner.sleep(3)

出现报错:

170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]SyntaxError: Non-ASCII character in file 'D:\Program\workspace\monkeyrunner_test01\test02.py', but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.Py.SyntaxError(Py.java:166)
170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.ParserFacade.fixParseError(ParserFacade.java:102)
170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.ParserFacade.parse(ParserFacade.java:184)
170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.Py.compile_flags(Py.java:1731)
170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.__builtin__.execfile_flags(__builtin__.java:514)
170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:225)
170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at com.android.monkeyrunner.ScriptRunner.run(ScriptRunner.java:116)
170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at com.android.monkeyrunner.MonkeyRunnerStarter.run(MonkeyRunnerStarter.java:77)
170926 23:53:26.465:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:189)

方法一:如果我加上

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

报错:

170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]SyntaxError: Non-ASCII character in file 'D:\Program\workspace\monkeyrunner_test01\test02.py', but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.Py.SyntaxError(Py.java:166)
170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.ParserFacade.fixParseError(ParserFacade.java:102)
170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.ParserFacade.parse(ParserFacade.java:184)
170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.Py.compile_flags(Py.java:1731)
170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.__builtin__.execfile_flags(__builtin__.java:514)
170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:225)
170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at com.android.monkeyrunner.ScriptRunner.run(ScriptRunner.java:116)
170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at com.android.monkeyrunner.MonkeyRunnerStarter.run(MonkeyRunnerStarter.java:77)
170926 23:56:37.058:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:189)

方法二:如果我加上:

#-*-coding:utf-8-*-

依旧报错:

170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]SyntaxError: ('Unknown encoding: utf-8-', ('D:\\Program\\workspace\\monkeyrunner_test01\\test02.py', 1, 0, ''))

170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:336)
170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:286)
170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.ParserFacade.parse(ParserFacade.java:181)
170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.Py.compile_flags(Py.java:1731)
170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.core.__builtin__.execfile_flags(__builtin__.java:514)
170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:225)
170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at com.android.monkeyrunner.ScriptRunner.run(ScriptRunner.java:116)
170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at com.android.monkeyrunner.MonkeyRunnerStarter.run(MonkeyRunnerStarter.java:77)
170926 23:58:09.631:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]    at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:189)

方法三:如果我加上

#coding:utf-8

反而能够正常跑下去而不报错。其中原因,待我分析之后附上。

解决:SyntaxError: Non-ASCII character in file的更多相关文章

  1. SyntaxError: Non-ASCII character 'æ' in file csdn.py on line 7, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

    错误信息: SyntaxError: Non-ASCII character , but no encoding declared; see http://python.org/dev/peps/pe ...

  2. 怎么解决syntaxerror:non-utf-8 code starting with \xc4'in file

    怎么解决syntaxerror:non-utf-8 code starting with \xc4'in file   首行增加,已测试可用. # coding=gbk   程序中出现中文,运行的时候 ...

  3. 创建文件夹并解决解决unicode和ASCII码转换的问题

    # -*- coding: UTF-8 -*-import sysimport timeimport os #解决unicode和ASCII码转换的问题reload(sys) #解决unicode和A ...

  4. atitit.解决SyntaxError: missing ] after element list"不个object 挡成个str eval ....

    atitit.解决SyntaxError: missing ] after element list"不个object  挡成个str eval .... 1. 原因::: 不个object ...

  5. 解决java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

    解决java.lang.SecurityException: Invalid signature file digest for Manifest main attributes 当项目依赖其他jar ...

  6. python解决SyntaxError: Non-ASCII character '\xe6'

    出现情况,输入如下一个函数demo: run,报错: SyntaxError: Non-ASCII character '\xe6' in file /Users/XX/PycharmProjects ...

  7. 解决:"UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position"错误

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Haiyang_Duan/article/ ...

  8. Python读文件报错:SyntaxError: Non-ASCII character in file

    打开city.py文件时报错 问题原因: 程序中的编码错误,python默认是acii模式,没有支持utf8.如果代码中有汉字 ,就会报错 解决方案: 源代码文件(city.py)第一行添加:#cod ...

  9. 【Python】【解决】UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(128)

    1.问题描述 今天在升级Ubuntu到14.04,使用命令行启动软件更新器,进行版本升级,结果开始升级就异常退出了,具体打印如下: $update-manager -d 正在检查新版 Ubuntu 使 ...

随机推荐

  1. HTML 之 Table 表格详解

    HTML 之 Table 表格详解 HTML中的table可以大致分为三个部分: thead ---------表格的页眉 tbody ---------表格的主体 tfoot ---------定义 ...

  2. [Erlang危机](5.1.3)进程

    原创文章,转载请注明出处:server非业余研究http://blog.csdn.net/erlib 作者Sunface 联系邮箱:cto@188.com Processes Trying to ge ...

  3. postgrSQL 错误ERROR: permission denied

    赋权限: GRANT ALL PRIVILEGES ON TABLE 表名 TO 用户;

  4. 什么是 XML Schema(转)

    什么是 XML Schema? XML Schema 的作用是定义 XML 文档的合法构建模块,类似 DTD. XML Schema 的作用: 定义可出现在文档中的元素 定义可出现在文档中的属性 定义 ...

  5. Kafka的架构

    1.Kafka整体架构    一个典型的Kafka集群中包含若干producer(可以是web前端产生的page view,或者是服务器日志,系统CPU.memory等),若干broker(Kafka ...

  6. Python的模块与函数以及与自动化的结合

    3 模块与函数 3.1程序结构 python的程序由package,module,function组成,分别是包,模块,函数.模块是函数和类的集合,包,模块,函数之间的关系如下: 3.2模块 pyth ...

  7. 深入理解MVC架构

    MVC MVC是一种设计模式(Design pattern),也就是一种解决问题的方法和思路, 是上世纪80年代提出的,到现在已经颇有历史了. MVC的意义在于指导开发者将数据与表现解耦,提高代码,特 ...

  8. uwsgi+nginx项目上线

    一.基础环境配置 1.Linux安装配置     1.设置IP地址 [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 ...

  9. 希望and目标

    软件工程是一门枯燥的课程,这门课我不喜欢上,容易犯困,但就因为如此.我不得不好好的学习,我希望在这门课上.我能将基础学扎实,在实践上可以自己慢慢研究,我的目标不是很远大,学好.学扎实..在这门课上一周 ...

  10. PAT 天梯赛 L2-024. 部落 【并查集】

    题目链接 https://www.patest.cn/contests/gplt/L2-024 题意 给出 几个不同的圈子,然后 判断 有哪些人 是属于同一个部落的,或者理解为 ,有哪些人 是有关系的 ...