Take screenshots
Screenshot of browser
getScreenshotAs()
See JavaDoc here, the following screenshot types are supported: BYTES, BASE64, FILE.
Alternatively, you can use the screenshotAsImage() method.
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<robotics-flow>
<robot driver="internet explorer" close-on-completion="true" name="webDriver">
<script><![CDATA[
timeouts(15 * 1000)
open('https://www.w3.org/')
screen = driver().getScreenshotAs(OutputType.BYTES)
]]></script>
</robot>
</robotics-flow>
<var-def name="link_url">
<s3 bucket='temp.bucket'>
<s3-put path="_r2d2/${'screenshot' + UUID.randomUUID() + '.png'}" acl="PublicRead">
<script return="screen"/>
</s3-put>
</s3>
</var-def>
<export include-original-data="true">
<single-column name="screenshot_url" value="${link_url}" />
</export>
</config>
Web-Harvest function
That is a generic function which takes browser screen capture and saves it to S3.
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config">
<function name="takeAndStoreScreenshot">
<script><![CDATA[
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import java.io.File;
String hitId = item.get(0).getWrappedObject().getSubmission().getAwsHitId().toString();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH-mm'Z'");
String date = simpleDateFormat.format(new Date());
File screenshotFile = File.createTempFile("rpa-" + date, ".png");
]]></script>
<try>
<body>
<script><![CDATA[
byte[] screenShot = ((TakesScreenshot) driver.get(0).getWrappedObject()).getScreenshotAs(OutputType.BYTES);
FileUtils.writeByteArrayToFile(screenshotFile, screenShot);
//sys.defineVariable("screenShotFileVar", screenshotFile);
]]></script>
<var-def name="screenShotPath">
<s3 bucket="${bucket_name}">
<s3-put path="rpa-screenshots/${bp_name}/${hitId}/rpa-${date}.png" acl="PublicRead"
content-type="image/png" content-disposition="inline">
<file action="read" path='${screenshotFile.getAbsolutePath()}' type="binary"/>
</s3-put>
</s3>
</var-def>
<log level="warn" message="Screenshot generated ${screenShotPath}."/>
</body>
<catch>
<script><![CDATA[
FileUtils.deleteQuietly(screenshotFile);
]]></script>
</catch>
</try>
<script><![CDATA[
FileUtils.deleteQuietly(screenshotFile);
]]></script>
<return>
<var name="screenShotPath"/>
</return>
</function>
</config>
Screenshot of Java applet
Active window - standard Java library
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<var-def name="javaScreenshot"><![CDATA[
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import org.openqa.selenium.Point;
import org.openqa.selenium.Dimension;
Point pos = driver.manage().window().getPosition();
Dimension dim = driver.manage().window().getSize();
BufferedImage screenFullImage = new Robot().createScreenCapture(new Rectangle(pos.x, pos.x, dim.width, dim.height));
File file = new File("screenshot_${System.currentTimeMillis()}.png");
ImageIO.write(screenFullImage, "png", file);
return file.getAbsolutePath();
]]></var-def>
<robotics-flow>
<robot driver="desktop">
<script><![CDATA[
open("notepad.exe");
switchTo().window("[CLASS:Notepad]");
String path = executeGroovyScript(javaScreenshot.toString());
log.warn("file path : ${path}");
]]></script>
</robot>
</robotics-flow>
<export include-original-data="true"></export>
</config>