Search code examples
javaseleniumscreenshot

Why I can't take a screenshot using selenium webdriver in java?


I am working on a project on regression testing using Selenium webdriver in java. I have written the following code to take a screenshot.

    package SelTest;
    import java.io.File;
    import org.apache.commons.io.FileUtils;
    import org.openqa.selenium.By;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.Test;

    public class takeScreenShottest {
        public WebDriver driver;

        @Test
        public void openBrowser() throws Exception{
            driver = new FirefoxDriver();
            driver.manage().window().maximize();
            driver.get("http://www.google.com");
            try{
                driver.findElement(By.id("testing")).sendKeys("test");
            }catch(Exception e){
                System.out.print("I am in Exception");
                getscreenshot();
            }
        }

        public void getscreenshot() throws Exception{
            File scrFile=((TakesScreenshot)).driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(scrFile, new File("E:\\Android-workspace\\Test1\\src\\ScreenShot\\screenshot.png"));
          }

        public static void main(String[] args) throws Exception{
            takeScreenShottest ts = new takeScreenShottest();
            ts.openBrowser();
        }
    }

I am getting an error for

    File scrFile=((TakesScreenshot)).driver).getScreenshotAs(OutputType.FILE);

Why is this happening?


Solution

  • Error solved. The error was TakesScreenshot cannot be resolved to a variable and Syntax error on token ")". So, I removed the ")." So the correct syntax was

        File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);