我正在尝试在Eclipse android JUnit测试中使用Robotium测试Android应用程序。
我想使用 Robotium 捕获不同阶段的屏幕截图,并使用 PC 上的 OpenCV 库而不是 android 设备处理它们。
我一直在阅读有关分离这两项任务的论坛。但是,一直无法这样做。
如果有人能在这方面提供一些指示,那就太好了。
谢谢
要将这两个任务分开,您需要实现(例如)android上的REST客户端和桌面上的服务器。
- 客户端应连接到服务器
- 服务器ping客户端,客户端向他发送屏幕截图。也许,您需要在测试中创建单独的线程。
客户端应执行以下操作:当您需要截屏时:
Object res = null;
res = new File(takeScreenShot(solo));
然后
public static String takeScreenShot(Solo _solo) {
solo = _solo;
String dir = "/sdcard/Robotium-Screenshots/";
String fileName = "screenshot";
java.io.File file = new java.io.File(dir, fileName);
if (file.exists()) {
file.delete();
}
solo.takeScreenshot(fileName);
solo.sleep(2000);
return "/sdcard/Robotium-Screenshots/" + fileName + ".jpg";
}
你需要发送它
service.sendScreen(file);
在您的服务中将像这样执行 smthng:
public static JSONObject sendScreen(final File file) {
JSONObject res;
String response;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(host + "/" + methodName);
MultipartEntity entity = new MultipartEntity();
entity.addPart("file", new FileBody(file));
httppost.setEntity(entity);
try{
response = httpclient.execute(httppost, responseHandler);
res = (JSONObject) JSONValue.parse(response);
}
catch(IOException e)
{
res = null;
e.printStackTrace();
}
return res;
}
下面是 C# 上用于分析屏幕截图的代码示例
public void StepError(Stream stream)
{
var result = new DCStepErrorResponce();
const string imageName = "file";
string fullFileName = String.Empty;
var parser = new MultipartFormDataParser(stream);
if (parser.Files.Any(file => file.Name == imageName))
{
Stream data = parser.Files.First(file => file.Name == imageName).Data;
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "HFAutomationTesting", "Temp", "ScreenShots");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
do
{
string fileName = String.Format("{0:yy_MM_dd_HH_mm_ss_ffff}.jpg", DateTime.Now);
fullFileName = Path.Combine(path, fileName);
} while (File.Exists(fullFileName));
var fileToUpload = new FileStream(fullFileName, FileMode.Create);
byte[] bytearray = ToByteArray(data);
fileToUpload.Write(bytearray, 0, bytearray.Length);
fileToUpload.Close();
fileToUpload.Dispose();
}
}
您只需要在 C# 上实现 rest 服务器并对其进行调试。