您的位置:首页 > 移动开发 > Android开发

Robotium截图设置

2015-01-29 11:03 357 查看

Robotium截图设置

1、前提条件

在被测试的应用中添加对SD卡的读写权限,文件为AndroidManifest.xml,添加以下标示

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>

2、使用solo的takeScreenshot方法来截取当前页面


takeScreenshot

public void takeScreenshot()


Takes a screenshot and saves it in "/sdcard/Robotium-Screenshots/". Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under
test.

使用该方法时,如果文件目录不存在,则会自动创建,且生成的图片文件格式如下

271113-054228.jpg, 其中27为日,11为月,13为年,其他的为时分秒,且文件默认格式为jpg

代码实现为:

solo.sleep(10000) ;

        solo.takeScreenshot() ;

        solo.sleep(1000) ;


takeScreenshot

public void takeScreenshot(String name)

Takes a screenshot and saves it with the specified name in "/sdcard/Robotium-Screenshots/". Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml
of the application under test.

Parameters:
name
 - the name to give the screenshot

使用该方法时,可以自定义生成的文件名字,且可以自动创建文件目录

代码实现为:

solo.sleep(10000) ;

        solo.takeScreenshot("test") ;

        solo.sleep(1000) ;

生成的文件为test.jpg


takeScreenshot

public void takeScreenshot(String name,
int quality)

Takes a screenshot and saves the image with the specified name in "/sdcard/Robotium-Screenshots/". Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml
of the application under test.

Parameters:
name
 - the name to give the screenshot
quality
 - the compression rate. From 0 (compress for lowest size)
to 100 (compress for maximum quality)

使用该方法可以对生成的图片进行压缩,大小从0到100,值越小越失真

代码实现:

solo.sleep(10000) ;

        solo.takeScreenshot("test100",100) ;

        solo.sleep(1000) ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息