今天尝试用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. 关于在python manage.py createsuperuser时报django.db.utils.OperationalError: no such table: auth_user的解决办法

    在stackflow上看到解决的办法是需要进行数据路的migrate:https://stackoverflow.com/questions/39071093/django-db-utils-oper ...

  2. php扩展redis链接失败,返回false

    刚开始接触redis,发现一直返回false,其实只要关闭防火墙就可以连接成功了. 关闭selinux操作   方法1:修改grub.conf将参数selinux=1修改为等于selinux=0,这个 ...

  3. Windows下比较小巧的c/c++ ide

    下载:codeblocks 只有几十兆. http://www.codeblocks.org/downloads/26#windows codeblocks-16.01mingw-nosetup.zi ...

  4. MySQL复制(二):二进制日志、二进制日志的结构和内容

    通常只有即将执行完毕的语句才会写入到二进制日志中.但是一些特殊情况:语句附加的信息或直接代替语句被写入. 二进制日志记录的内容 作用:记录数据库中表的更变,用于复制和PITP(即时恢复) 基于语句SB ...

  5. Python3.6全栈开发实例[017]

    17.念数字:给出一个字典,在字典中标识出每个数字的发音,包括相关符号,然后由用户输入一个数字,让程序读出相对应的发音(不需要语音输出.单纯的打印即可). dics = { '-':'fu', ':' ...

  6. 021-Spring Boot 测试,Junit方式使用,mock方式,Controller测试

    一.概述 二.Junit方式使用 2.1.基本使用 eclipse在新建的类上,右键→new→Junit Test Case,修改一下Source folder,选择src/test/java,下一步 ...

  7. admin 模块功能

    class CustomModelAdmin(admin.ModelAdmin): def has_module_permission(self, request): 是否会显示model def h ...

  8. JavaScript:学习笔记(3)——正则表达式的应用

    JavaScript:正则表达式的应用 应用正则表达式对象RegExp 创建正则表达式 JavaScript中使用RegExp对象来表述一个正则表达式.使用正则表达式之前首先要创建一个RegExp对象 ...

  9. JVM内存的堆、栈和方法区

    JVM的内存分为堆.栈.方法区和程序计数器4个区域 存储内容:基本类型,对象引用,对象本身,class,常量,static变量 堆: 拥有者:所有线程 内容:对象本身,不存放基本类型和对象引用 垃圾回 ...

  10. linux usb简介

    参考书:<linux device drivers>.<usb 2.0规范> <usb3.1规范><usb白皮书> 以linux为例来说明usb系统. ...