Created
October 6, 2021 03:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Drop this
task.cfc
in an empty folder and run it like so: