2017-09-20 74 views
0

我只想在使用appium的混合Android应用程序中截取Webview部分的截图。如何截取webview部分的截图仅使用Appium

我用下面的代码来截图。

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

是否有可能采取的WebView的截图采取截图部分只使用Appium?

任何一个请帮忙

+0

你能检查我的答案吗? – KeLiuyue

回答

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()); 
     } 
    } 
}