2017-09-20 1 views
0

appviewを使用してハイブリッドアンドロイドアプリ内のWebview部分のスクリーンショットを撮りたいと思います。Appiumのみを使用して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

が、それは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()); 
     } 
    } 
} 
関連する問題