3

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"));
Community
  • 1
  • 1
Alam
  • 93
  • 1
  • 1
  • 15
  • 1
    What have you tried and what was the result? As you did in school... please show your work. :) It's part of the process of getting questions answered on SO. It's helpful to you because it forces you to investigate your own problem and think it through. It also proves to readers that you did your homework and made a reasonable attempt to answer your own question. Thirdly, it helps readers find and diagnose the problem resulting in a better answer for you and less time wasted for us. – JeffC Oct 10 '15 at 01:07
  • Thank you for answering the question. I am adding the code. – Alam Oct 10 '15 at 11:30

1 Answers1

1

If you have a google drive or dropbox accounts, you probably have "dropbox" or "doogle drive" applications installed. Those applications sync contents of your local folders with the cloud. Simply save your screenshots in that folder and you're good to go. Google Drive app Dropbox app

If you experience problem with saving the screenshot itself, please provide the error message you get, or at least a screenshot. I'll be glad to help.

  • Thank you for answering the question. Installing Google Drive and Dropbox is not a possibility otherwise I should've done that earlier. – Alam Oct 10 '15 at 11:28