0

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
Sanket Gupte
  • 357
  • 1
  • 4
  • 16

2 Answers2

1

You can use this as below:

FileUtils.copyFile(((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE), new File("E:\\Android-workspace\\Test1\\src\\ScreenShot\\screenshot.png"));
Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46
0

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);
Sanket Gupte
  • 357
  • 1
  • 4
  • 16