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.
import com.workfusion.odf2.compiler.BotTask;
import com.workfusion.odf2.core.task.AdHocTask;
import com.workfusion.odf2.core.task.TaskInput;
import com.workfusion.odf2.core.task.output.TaskRunnerOutput;
import com.workfusion.odf2.core.webharvest.rpa.RpaDriver;
import com.workfusion.odf2.core.webharvest.rpa.RpaFactory;
import com.workfusion.odf2.core.webharvest.rpa.RpaRunner;
import com.workfusion.automation.rpa.utils.ScreenshotUtils;
import com.workfusion.rpa.driver.Driver;
import com.workfusion.rpa.helpers.RPA;
import org.openqa.selenium.OutputType;
import org.slf4j.Logger;
import javax.inject.Inject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static com.workfusion.rpa.helpers.Script.executeGroovyScript;
@BotTask(requireRpa = true)
public class TakeScreenshots implements AdHocTask {
private final RpaRunner rpaRunner;
private final Logger logger;
@Inject
public TakeScreenshots(RpaFactory rpaFactory, Logger logger){
this.rpaRunner = rpaFactory
.builder(RpaDriver.UNIVERSAL)
.closeOnCompletion(true)
.build();
this.logger = logger;
}
@Override
public TaskRunnerOutput run(TaskInput taskInput) {
rpaRunner.execute(driver->{
try {
this.getScreenShotAsExample(driver);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return taskInput.asResult()
.withColumn("example_bot_task_output", "completed_successfully");
}
private void getScreenShotAsExample(Driver driver) throws IOException {
RPA.timeouts(15 * 1000);
RPA.openChrome("https://www.w3schools.com/js/default.asp");
byte[] screen = driver.getScreenshotAs(OutputType.BYTES);
Path path = Paths.get("C:\\Users\\sshukla\\Desktop\\New folder\\W3School.png");
Files.write(path,screen);
S3Bucket s3Bucket = s3Service.getBucket("36807");
String FileName_= "_r2d2/screenshot" + UUID.randomUUID() + ".png";
String URL = s3Bucket.put(screen,FileName_).getDirectUrl();
this.logger.debug(" URL - " + URL);
}
}
User can also use ScreenshotUtils() in-order to capture Screen Shot. Follow the code given below.
import com.workfusion.odf2.compiler.BotTask;
import com.workfusion.odf2.core.task.AdHocTask;
import com.workfusion.odf2.core.task.TaskInput;
import com.workfusion.odf2.core.task.output.TaskRunnerOutput;
import com.workfusion.odf2.core.webharvest.rpa.RpaDriver;
import com.workfusion.odf2.core.webharvest.rpa.RpaFactory;
import com.workfusion.odf2.core.webharvest.rpa.RpaRunner;
import com.workfusion.automation.rpa.utils.ScreenshotUtils;
import com.workfusion.rpa.driver.Driver;
import com.workfusion.rpa.helpers.RPA;
import org.openqa.selenium.OutputType;
import org.slf4j.Logger;
import javax.inject.Inject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static com.workfusion.rpa.helpers.Script.executeGroovyScript;
@BotTask(requireRpa = true)
public class TakeScreenshots implements AdHocTask {
private final RpaRunner rpaRunner;
private final Logger logger;
@Inject
public TakeScreenshots(RpaFactory rpaFactory, Logger logger){
this.rpaRunner = rpaFactory
.builder(RpaDriver.UNIVERSAL)
.closeOnCompletion(true)
.build();
this.logger = logger;
}
@Override
public TaskRunnerOutput run(TaskInput taskInput) {
rpaRunner.execute(driver->{
try {
this.screenShotUtilsExample(driver);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return taskInput.asResult()
.withColumn("example_bot_task_output", "completed_successfully");
}
private void screenShotUtilsExample(Driver driver) throws IOException {
RPA.timeouts(15 * 1000);
RPA.openChrome("https://www.w3schools.com/java/default.asp");
byte[] screenShotFile = new byte[0];
try {
ScreenshotUtils screenshotUtils = new ScreenshotUtils();
screenShotFile = screenshotUtils.getScreenshotAsByteArray();
}
catch (Exception ex)
{
ex.printStackTrace();
}
Path path = Paths.get("C:\\Users\\sshukla\\Desktop\\New folder\\W3SchoolUsingScreenshotUtils.png");
Files.write(path,screenShotFile);
S3Bucket s3Bucket = s3Service.getBucket("36807");
String FileName_= "_r2d2/screenshot_SSUtil" + UUID.randomUUID() + ".png";
String URL = s3Bucket.put(screenShotFile,FileName_).getDirectUrl();
this.logger.debug(" URL - " + URL);
}
}
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
import com.workfusion.odf2.compiler.BotTask;
import com.workfusion.odf2.core.task.AdHocTask;
import com.workfusion.odf2.core.task.TaskInput;
import com.workfusion.odf2.core.task.output.TaskRunnerOutput;
import com.workfusion.odf2.core.webharvest.rpa.RpaDriver;
import com.workfusion.odf2.core.webharvest.rpa.RpaFactory;
import com.workfusion.odf2.core.webharvest.rpa.RpaRunner;
import com.workfusion.automation.rpa.utils.ScreenshotUtils;
import com.workfusion.rpa.driver.Driver;
import com.workfusion.rpa.helpers.RPA;
import org.openqa.selenium.OutputType;
import org.slf4j.Logger;
import javax.inject.Inject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static com.workfusion.rpa.helpers.Script.executeGroovyScript;
@BotTask(requireRpa = true)
public class TakeScreenshots implements AdHocTask {
private final RpaRunner rpaRunner;
private final Logger logger;
@Inject
public TakeScreenshots(RpaFactory rpaFactory, Logger logger){
this.rpaRunner = rpaFactory
.builder(RpaDriver.UNIVERSAL)
.closeOnCompletion(true)
.build();
this.logger = logger;
}
@Override
public TaskRunnerOutput run(TaskInput taskInput) {
rpaRunner.execute(driver->{
this.screenShotOfJavaAppletActiveWindowExample(driver);
});
return taskInput.asResult()
.withColumn("example_bot_task_output", "completed_successfully");
}
private void screenShotOfJavaAppletActiveWindowExample(Driver driver){
String javaScreenshot = "import java.awt.Robot; \n" +
"import java.awt.Rectangle; \n" +
"import java.awt.image.BufferedImage; \n" +
"import javax.imageio.ImageIO; \n" +
"import java.io.File; \n" +
"import org.openqa.selenium.Point; \n" +
"import org.openqa.selenium.Dimension; \n" +
"Point pos = driver.manage().window().getPosition(); \n" +
"Dimension dim = driver.manage().window().getSize(); \n" +
"BufferedImage screenFullImage = new Robot().createScreenCapture(new Rectangle(pos.x, pos.x, dim.width, dim.height)); \n" +
"File file = new File(\"screenshot_${System.currentTimeMillis()}.png\"); \n" +
"ImageIO.write(screenFullImage, \"png\", file); \n" +
"return file.getAbsolutePath();";
RPA.open("notepad.exe");
RPA.sleep(2000);
RPA.switchTo().window("[CLASS:Notepad]");
RPA.sendKeys("About to execute the below groovy script ");
RPA.pressEnter();
RPA.pressEnter();
RPA.sendKeys(javaScreenshot);
String path = executeGroovyScript(javaScreenshot);
RPA.pressEnter();
RPA.pressEnter();
RPA.sendKeys("path of the screenshot - ",path);
RPA.sleep(5 * 1000);
}
}