Most of the time we think to Capture Screenshot in WebDriver when some kind of error or exception surfaces while practicing testing, to resolve the same WebDriver has provided us one interface TakesScreenshot for capturing the screenshot of web application and This interface provides one method names as getScreenshotAs() to capture screenshot in instance of driver. This getScreenshotAs() method takes argument of type OutputType.File or OutputType.BASE64 or Output.BYTES. So that it could return captured screenshot in File type, or Base 64 string type or in raw bytes.
So this would look like this
For File type
File scrnshot= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
For Base64 string above code would be like
((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
For BYTES
((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
We have taken the screenshot with the help of getScreenshotsAs() method and and now its time to copy this file somewhere in our file system or in our desktop. So for this purpose we further use copyFile() method of the FileUtils class from theorg.apache.commons.io.FileUtils class.
Placing file in file system by using this line
FileUtils.copyFile(scrFile, new File(“e:\\main_page.png”));
As I have told you that copyFile is a method of Class FileUtils and to call this method we need to write class.method() and in above code copyFile is taking argument from getScreenShotsAs() and new location where we want to save this captured Screenshot with name and with extension.
Now we would use write small running code that would open Google page and it would take a snap shot and that snap shot would be saved as google.png in my e:\ driver. One thing we need to remember whenever we work with File system or java.io then chances of exception is more so we would use try and catch in our code.
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;
@BeforeMethod
public void launchChrome()
{
driver = new FirefoxDriver();
driver.get("http://google.co.in");
}
@Test
public void googleScreenshot()
{
try {
File scrnsht =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrnsht, new File("e:\\google_page.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterTest
public void kill()
{
driver.close();
}
}
So this would look like this
For File type
File scrnshot= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
For Base64 string above code would be like
((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
For BYTES
((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
We have taken the screenshot with the help of getScreenshotsAs() method and and now its time to copy this file somewhere in our file system or in our desktop. So for this purpose we further use copyFile() method of the FileUtils class from theorg.apache.commons.io.FileUtils class.
Placing file in file system by using this line
FileUtils.copyFile(scrFile, new File(“e:\\main_page.png”));
As I have told you that copyFile is a method of Class FileUtils and to call this method we need to write class.method() and in above code copyFile is taking argument from getScreenShotsAs() and new location where we want to save this captured Screenshot with name and with extension.
Now we would use write small running code that would open Google page and it would take a snap shot and that snap shot would be saved as google.png in my e:\ driver. One thing we need to remember whenever we work with File system or java.io then chances of exception is more so we would use try and catch in our code.
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;
@BeforeMethod
public void launchChrome()
{
driver = new FirefoxDriver();
driver.get("http://google.co.in");
}
@Test
public void googleScreenshot()
{
try {
File scrnsht =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrnsht, new File("e:\\google_page.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterTest
public void kill()
{
driver.close();
}
}
No comments:
Post a Comment