Earn Money in 10 days

Selenium Videos

Protractor In Selenium

Livechat

Showing posts with label How to take screenshots of Failed/Passed Testcase. Show all posts
Showing posts with label How to take screenshots of Failed/Passed Testcase. Show all posts

Sunday, 30 November 2014

How to Capture screenshots in Selenium WebDriver

Capturing screenshots in selenium Webdriver

When we run our Selenium test on server sometime our test case failed.Debugging of each and every test cases is very challenging.If we take screenshots of everytest case it helps us to complete our task.

 And sometime we have a requirement that you have to take screenshot of every Test case.Selenium provides an interface which helps us to take screenshots of Everytestcase.

I am writing that two lines of code which helps you to take screenshot in Selenium Webdriver

 // take the screenshot at the end of every test
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  //WebDriver provides an interface TakesScreenshots for capturing the screen shots of every testcase/failed Testcase.
//getScreenshotAs is a method to capture screenshots and saves into temporary location 
        FileUtils.copyFile(scrFile, new File("c:\\screenshot.png"));
//FileUtils.copyFile() is used to copy this temporary file to the wokspace.

Here is the code of taking sceenshots of failed testcase
package test;
import java.io.File;
import org.apache.commons.io.FileUtils;
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.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Chrome {
WebDriver driver;
@BeforeTest
public void openBrowser()
{
driver = new FirefoxDriver();
driver.get("http://facebook.com");
}
@Test
public void getScreenshot()
{
try {
File scrFile =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new
File("e:\\facebook_page.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterTest
public void closeBrowser()
{
driver.close();
}
}

//You need to append the path Eveytime

Popular Posts

 
subscribe
Subscribe Us
emailSubscribe to our mailing list to get the updates to your email inbox... We can't wait more to have your email in our subscribers email list. Just put your nice email in below box: