We are discussing here about capturing a Screenshot via selenium, we have taken an example of FB page where if the title of FB page is different from what we have expected, then the screenshot should be captured
Below is code for your understanding ......
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
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.chrome.ChromeDriver;
public class Screenshotss {
WebDriver driver;
@Test
public void test() throws IOException
{
System.setProperty("Webdriver.chrome.driver","chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//opening a facebook link
driver.get("https://www.facebook.com");
driver.manage().window().maximize();
//reading the title of the page
String title = driver.getTitle();
//if the title of the facebook is similar to the below mentioned content then no screenshot will be captured
if(title.contentEquals("Facebook – log in or sign up"))
{
System.out.println("launch successful");
}
else {
// if the title is different form what is expected then a screenshot will be captured
System.out.println("launch failed");
TakesScreenshot ts=(TakesScreenshot)driver;
File src = ts.getScreenshotAs(OutputType.FILE);
// "screenshot" is the folder name screenshot will be saved with the name "screen1"
FileUtils.copyFile(src, new File("/Users/priyankac/Desktop/screenshot/screen1.png"));
}
}
}
// As the above title is correct the launch should be sucessfull so no screenshot should be captured
change the title to make the launch unsucessfull
Screenshot should be captured
WebDriver driver;
@Test
public void test() throws IOException
{
System.setProperty("Webdriver.chrome.driver","chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.facebook.com");
driver.manage().window().maximize();
String title = driver.getTitle();
if(title.contentEquals("Facebook – log in or sign up"))
{
System.out.println("launch successful");
TakesScreenshot ts=(TakesScreenshot)driver;
File src = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("/Users/priyankac/Desktop/screenshot/screen1.png"));
}
else {
System.out.println("launch failed");
}
}
In either case if you want to take a screenshot Go with the below code.
WebDriver driver;
@Test
public void test() throws IOException
{
System.setProperty("Webdriver.chrome.driver","chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.facebook.com");
driver.manage().window().maximize();
String title = driver.getTitle();
if(title.contentEquals("Facebook – log in or sign up"))
{
System.out.println("launch successful");
}
else {
System.out.println("launch failed");
}
TakesScreenshot ts=(TakesScreenshot)driver;
File src = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("/Users/priyankac/Desktop/screenshot/screen1.png"));
}
}
Comments
Post a Comment