Capturing Screenshot in Headless Mode

Capturing Screenshot in Headless Mode
In this blog, we are going to learn how we can capture screenshot while executing our test in a chrome headless browser.


Before Moving further in this blog I would highly recommend you to refer this blog to understand how to execute automation script in Headless mode.

To capture screenshot in the headless mode approach would be similar that we normally use to capture a screenshot. We can use Third party Utility like aShot or Shutterbug that we have already discussed in other blogs.


In this blog for the purpose of learning, we will be using the TakeScreenshot interface to Capture Screenshot.

Below is the Code Snippet for capturing the screenshot of google homepage in headless mode and storing it in a particular location.


Result of execution is:





SHARE THIS

Author:

My Name is Ankur Jain and I am currently working as Automation Test Architect.I am ISTQB Certified Test Manager,Certified UI Path RPA Developer as well as Certified Scrum Master with total 12 years of working experience with lot of big banking clients around the globe.I love to Design Automation Testing Frameworks with Selenium,Appium,Protractor,Cucumber,Rest-Assured, Katalon Studio and currently exploring lot in Dev-OPS as well. I am currently staying in Mumbai, Maharashtra. Please Connect with me through Contact Us page of this website.

Previous Post
Next Post
June 14, 2019 at 12:23 AM

Hi Ankur,
Hope you doing well.

I am working as Associate Software Engineer.
I am trying to take screenshot of full page but sticky header in the website creating problem.
How can I correct it.?
Please help me out.

Thanks,
Vaibhav

Reply
avatar
June 15, 2019 at 8:22 PM

Hi Vaibhav Please share the screenshot of page and the issue you are getting .i will look and based on that i can suggest some solution.My email id ankur.jain.cs@gmail.com

Reply
avatar
April 9, 2021 at 1:47 AM

Hi, I am running my code on Jenkins- Linux system in headless mode. To make browser compatible below is the code i am using:
Open Browser in headless mode:
public boolean openBrowser(String object, String data) throws Exception {
try {
String oSName = System.getProperty("os.name");


if (data.equals("Chrome")) {
if (oSName.toUpperCase().contains("WIN")) {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/Drivers/windows/chromedriver.exe");
//System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver.exe");
Constants.driver = new ChromeDriver();
Constants.driver.manage().window().maximize();
} else {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/Drivers/linux/chromedriver");
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
options.addArguments("--window-size=1920,1080");
options.addArguments("--disable-gpu");
options.addArguments("--disable-extensions");
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
options.addArguments("--start-maximized");
options.addArguments("--headless");
options.addArguments("--whitelisted-ips");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--no-sandbox");
Constants.driver = new ChromeDriver(options);
Constants.driver.manage().window().maximize();
//options.addArguments("--no-sandbox");
}

Capture snapshot code i am using is:
public static void takeSnapShot() throws Exception {

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
String dateString = format.format(new Date());
String vScenarioN = BaseStep.scenarioName;

//Convert web driver object to TakeScreenshot

//String fileWithPath = System.getProperty("user.dir") + "\\Screenshot\\SS"+"_"+vScenarioN+ "_"+ dateString + ".png";
String fileWithPath = System.getProperty("user.dir") + "\\Screenshot\\SC" + "_" + vScenarioN + "_" + dateString + ".png";

;
//String fileWithPath = "C://Framework//CucumberSeleniumProject//CucumberSeleniumProject////SC" + dateString + ".png";
TakesScreenshot scrShot = ((TakesScreenshot) Constants.driver);
//Call getScreenshotAs method to create image file
File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
//Move image file to new destination
File DestFile = new File(fileWithPath);
//Copy file at destination
FileUtils.copyFile(SrcFile, DestFile);
Reporter.addScreenCaptureFromPath(fileWithPath);
}

Still i am unable to see those files on Jenkins workspace
Can you help..!

Reply
avatar