Skip to content

Instantly share code, notes, and snippets.

@bdw429s
Created October 6, 2021 03:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdw429s/12cf16ae1d232688466897d8d073ec54 to your computer and use it in GitHub Desktop.
Save bdw429s/12cf16ae1d232688466897d8d073ec54 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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