Skip to content

Instantly share code, notes, and snippets.

@bdw429s
Created Oct 6, 2021
Embed
What would you like to do?
CommandBox Task Runner for creating Screenshot of website with Microsoft's Playwright lib
component {
function run( webURL='https://www.ortussolutions.com' ) {
if( !directoryExists( resolvePath( 'lib' ) ) ) {
command( 'install "jar:https://search.maven.org/remotecontent?filepath=com/microsoft/playwright/playwright/1.15.2/playwright-1.15.2.jar"' ).run();
command( 'install "jar:https://search.maven.org/remotecontent?filepath=com/microsoft/playwright/driver-bundle/1.15.2/driver-bundle-1.15.2.jar"' ).run();
command( 'install "jar:https://search.maven.org/remotecontent?filepath=com/microsoft/playwright/driver/1.15.2/driver-1.15.2.jar"' ).run();
command( 'install "jar:https://search.maven.org/remotecontent?filepath=org/netbeans/external/com-google-gson/RELEASE113/com-google-gson-RELEASE113.jar"' ).run();
}
classLoad( resolvePath( 'lib' ) )
try {
playwright = createObject( 'java', 'com.microsoft.playwright.Playwright' ).create()
webkit = playwright.webkit();
browser = webkit.launch();
context = browser.newContext();
page = context.newPage();
page.navigate( webURL );
page.screenshot(
createObject( 'java', 'com.microsoft.playwright.Page$ScreenshotOptions' )
.setPath(
fileSystemUtil.getJavaFile( resolvePath( 'screenshot.png' ) ).toPath()
)
);
} finally {
if( !isNull( browser ) ) browser.close();
if( !isNull( playwright ) ) playwright.close()
}
openPath( resolvePath( 'screenshot.png' ) );
print.greenLine( 'Complete!' );
}
}
@bdw429s

This comment has been minimized.

Copy link
Owner Author

@bdw429s bdw429s commented Oct 6, 2021

Drop this task.cfc in an empty folder and run it like so:

task run :weburl=https://www.forgebox.io
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment