我想使用appium在混合Android应用程序内部截取Webview部分的屏幕截图。
我使用以下代码截取屏幕截图。
String Screenshotpath = System.getProperty("user.dir") + "/";
System.out.println(Screenshotpath);
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
System.out.println(scrFile);
try {
FileUtils.copyFile(scrFile, new File(Screenshotpath
+ "screenshotwebview" + ".jpg"));
} catch (IOException e) {
e.printStackTrace();
}
它工作正常,并截取整个屏幕的截图。但是当我尝试将上下文设置为webview并尝试使用上面的代码截取屏幕截图时,它会显示以下错误
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command.
Original error: Could not proxy. Proxy error: Could not proxy command to remote server.
Original error: Error: ESOCKETTIMEDOUT (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds
是否可以仅使用Appium获取webview部分的屏幕截图?
任何人都可以帮忙
答案 0 :(得分:0)
你可以这样做。
在此之前,你可以这样做。
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webView.requestFocusFromTouch();
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("your url");
并检查版本。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkSdkVersion();
setContentView(R.layout.activity_webview_capture);
}
// check version
private void checkSdkVersion() {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
WebView.enableSlowWholeDocumentDraw();
}
}
在清单中添加权限。
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
方式1
private void getScreenshot() {
float scale = webView.getScale();
int webViewHeight = (int) (webView.getContentHeight() * scale + 0.5);
Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webViewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);
// save to the File
try {
String fileName = Environment.getExternalStorageDirectory().getPath() + "/webview_capture1.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
// Save
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromDraw.this, "Screenshot OK", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
e.getMessage();
}
}
方式2
private void getScreenshot() {
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_capture3.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromCapture.this, "Screenshot ok", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}