2017-06-20 37 views
0
TakesScreenshot ts=(TakesScreenshot)driver; 

File source=ts.getScreenshotAs(OutputType.FILE); 
FileUtils.copyFile(source,new File("./Screenshots/test.png")); 

上面的代碼是用來捕獲屏幕截圖,並在粘貼文件,但它無法找到該文件,第三行顯示一個錯誤 - 構建(串)不可見。什麼是錯誤?構造文件(字符串)是不可見的,雖然它被定義

+2

確保你正在處理'java.io.File'。 – Berger

+2

檢查您的文件導入。它是否導入'java.io.File'? –

+0

這將是很好,如果你[先格式化你的代碼](https://stackoverflow.com/posts/44653648/edit) –

回答

0

你只是試圖捕獲一個屏幕截圖,當一個測試失敗,或尋求不同的東西?如果前者this顯示瞭如何在@After中執行此操作,以防止在測試失敗時僅捕獲屏幕截圖。

0

請使用此完整方法嘗試截取並將結果存儲在特定路徑中。它爲我工作。試試這個,讓我知道。

public void TakeScreenShots(String TestID,String ResultsPath){ 
    try{ 
     GlobalVariable.bExitTestIteration = false; 
     if(!(TestID.length()>0)){ 
      System.out.println("chk"); 
     } 
     if((TestID.length()>0) && (ResultsPath.length()>0)){ 
       TestID=//DriverScript.sScenarioID; 
       ResultsPath="//DriverScript.ResultsFolder"; 


      //****** Check if local variable is used ********** 
      if (ResultsPath.length()>2){ 
       if (ResultsPath.substring(0, 1)=="%%"){ 
        ResultsPath = getDictinaryValues(ResultsPath.substring(2)); 
       } 
      } 
      //*************************************************   
      Log.info("Take a screenshot "); 
      WebDriverWait wait = new WebDriverWait(driver, 10); 
      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
      // Now you can do whatever you need to do with it, for example copy somewhere 
      FileUtils.copyFile(scrFile, new File(ResultsPath+"\\" +TestID+ ".png")); 
     } 
    }catch(Exception e){ 
     Log.error("Not able to take a screenshot --- " + e.getMessage());   
     } 
    } 

請檢查這段代碼並回復我。

相關問題