I want to automatically take a screenshot and save the image on my PC as well as Google Drive or Dropbox if my WebDriver test failed (any error). I am using Java and TestNG & JUnit testing frameworks for Selenium Webdriver.
I have already gone through THIS and THIS questions and the solution given in both questions are not working for saving the screenshots locally.
//JUnit Test
driver = new FirefoxDriver();
baseUrl = "http://example.com";
driver.get(baseUrl + "");
JOptionPane.showMessageDialog(null, "Site loaded");
driver.findElement(By.xpath("/html/body/div[3]/header/div[1]/div[1]/div[6]/div/div/div[2]/div[1]/span/a[2]")).click();
JOptionPane.showMessageDialog(null, "Join Free link is clicked");
driver.findElement(By.id("gender")).click();
driver.findElement(By.id("c_name")).click();
driver.findElement(By.id("c_name")).clear();
driver.findElement(By.id("c_name")).sendKeys("Test");
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("abc@test.com");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("abc123");
driver.findElement(By.id("re-password")).clear();
driver.findElement(By.id("re-password")).sendKeys("abc123");
driver.findElement(By.id("captcha")).clear();
driver.findElement(By.id("captcha")).sendKeys("WJWEA");
driver.findElement(By.id("button")).click();
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("c:\\tmp\\screenshot.png"));