0
TakesScreenshot ts=(TakesScreenshot)driver;

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

the above code is used to capture screenshot and paste in a file but it cant find the FILE and third line shows an error - the construct(string) is not visible. What is the error?

Flown
  • 11,480
  • 3
  • 45
  • 62
Madhu Ragi
  • 41
  • 3
  • 2
    Make sure you are dealing with `java.io.File` . – Arnaud Jun 20 '17 at 12:57
  • 2
    Check your import of File. Is it importing `java.io.File`? – Andy Thomas Jun 20 '17 at 12:57
  • It would be great if you [format your code first](https://stackoverflow.com/posts/44653648/edit) – ΦXocę 웃 Пepeúpa ツ Jun 20 '17 at 12:57
  • i have this included import org.apache.commons.io.FileUtils; – Madhu Ragi Jun 20 '17 at 13:00
  • you should have also import of File class – Ori Marko Jun 20 '17 at 13:04
  • 1
    Let's not try to do this piecemeal. Present a [mcve] demonstrating this problem. I estimate that it should require fewer than 20 lines. If the exercise of putting that together does not help you discover the problem, then edit it into your question, and we will be able to sort it out. – John Bollinger Jun 20 '17 at 13:04
  • John Bollinger - i just need to execute the above lines to test if the screenshot is captured and pasted in the FILE. – Madhu Ragi Jun 20 '17 at 13:11
  • I aslo tried with this piece of code: File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64); FILE and BASE64 cannot be resolved or is not a field is the error. How can i correct this? – Madhu Ragi Jun 20 '17 at 13:12

2 Answers2

0

Are you just trying to capture a screen shot when a test fails, or going for something different? In case it's the former this shows how to do it in an @After, in case you are just trying to capture a screen shot when a test fails.

Helgi
  • 94
  • 4
0

Please try with this full method to take screenshot and store the results in particular path. It is working for me. Try with this and let me know.

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

Please check this piece of code and respond to me.

SAUMARS
  • 73
  • 1
  • 11