Skip to main content

Capturing the screenshot via selenium

    


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





Now you know very well that if you need to take a screenshot,  when the condition is true what exactly you need to do.


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(srcnew 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(srcnew File("/Users/priyankac/Desktop/screenshot/screen1.png"));


    

}

}




Comments

Popular posts from this blog

Cucumber - Execution of test cases and reporting

Before going through this blog please checkout blog on   Cucumber Fundamentals Cucumber is testing tool which implements BDD(behaviour driven development).It offers a way to write tests that  anybody can understand, regardless of there technical knowledge. It users Gherkin (business readable language) which helps to  describe behaviour without going into details of implementation It's helpful for  business stakeholders who can't easily read code ( Why cucumber tool,  is  called  cucumber , I have no idea if you ask me I could have named it "Potato"(goes well with everything and easy to understand 😂) Well, According to its founder..... My wife suggested I call it  Cucumber  (for no particular reason), so that's how it got its  name . I also decided to give the Given-When-Then syntax a  name , to separate it from the  tool . That's why it's  called  Gherkin ( small variety of a cucumber that's been pickled. I...

Cucumber Datatable testing without using scenario outline and Issues

  Before going through this blog please visit blog The issue you might encounter while running cucumber project- 1.  you have created the cucumber all file in src/test/java and you are trying to  move it in another folder transition might not be smooth, It will give error one fix will led to other.(depends on the setup) 2. If on running a feature file and successfully creating a stepdef file, If application still says that  few steps are still needs to be implemented or all of them(not detecting creation of step definition) then there is a high chance you have mistakenly wrote some other annotation in stepdef when compares to you feature file eg:- feature file @And ( "^user close browser$" ) public   void  user_close_browser()  throws  Throwable { Stepdef:-             @Then ( "^user close browser$" ) public   void  user_close_browser()  throws  Throwable { driver .quit(); 3. Duplicate S...

Beginners tutorial -:working with JMeter in Mac and windows - Part-01

  Prequisite   you should have Java downloaded in your system with Home path set under environment variables.(as of today Java version 8 and higher are required fro jmeter ) for help check out this link Note Always run the jmeter on your secondary browser,  if you give the primary browser for proxy settings then your internet connection will be disrupted for the browser as well as system For ex if you have chrome and firefox and your primary or default browser is chrome then do all the proxy setting in firefox so it won't hamper the system Internet connection  if you have safari as your default browser in your mac os then set proxy in chrome/firefox  MAC Download jmeter from the link  here click on the hypelink under section Binaries  "Apache JMeter( 5.3 ). tgz" file  for Mac   Tar file will get downloaded Double click on the tar file to unzip  once you open the folder  got to bin and search for jmeter.sh file this is a executa...