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.
Hi Ankur,
ReplyHope 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
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
ReplyHi, I am running my code on Jenkins- Linux system in headless mode. To make browser compatible below is the code i am using:
ReplyOpen 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..!