PrivateFunctions/New-PuppeteerCodeGenerator.ps1
<#
.SYNOPSIS This class generates snippets of Puppeteer code for UI automation. #> class PuppeteerCodeGenerator { ########### # Members # ########### # This is the folder where the screenshots will be stored, relative to the Node package directory. [String]$ScreenshotFolder = "screenshots" # This selects if screenshots should be taken. [Bool]$ScreenshotsEnabled = $false # This tracks the current screenshot index. [Int32]$ScreenshotIndex = 0 ################# # Basic Methods # ################# # This is the constructor for the code generator. PuppeteerCodeGenerator() {} # This method returns the code for calling a function. [String]CallFunction([String]$functionName) { return Get-Puppeteer_CallFunction -FunctionName $functionName } # This method returns the code for clicking on a selector and waiting for the page to load. [String]ClickOnSelector([String]$selectorName) { return $this.ClickOnSelector($selectorName, "wait") } # This method returns the code for clicking on a selector and choosing whether to wait # for the page to load. [String]ClickOnSelector([String]$selectorName, [String]$waitPolicy) { return Get-Puppeteer_ClickOnSelector -SelectorName $selectorName -WaitForPageToLoad:($waitPolicy -eq "wait") } # This method returns the code for closing the chrome browser. [String]CloseChromeBrowser() { return Get-Puppeteer_CloseChromeBrowser } # This method returns the code for closing a function declaration. [String]CloseFunctionDeclaration() { return Get-Puppeteer_CloseFunctionDeclaration } # This method returns the code for declaring the delay function. [String]DeclareDelayFunction() { return Get-Puppeteer_DeclareDelayFunction } # This method returns the code for declaring username and password variables. [String]DeclareUsernameAndPassword([String]$username, [SecureString]$password) { return Get-Puppeteer_DeclareUsernameAndPassword -Username $username -Password $password } # This method returns the code for declaring username and password variables. [String]DeclareUsernameAndPassword([PSCredential]$credential) { return Get-Puppeteer_DeclareUsernameAndPassword -Credential $credential } # This method returns the code for declaring a non-const variable. [String]DeclareVariable([String]$name, [String]$value) { return $this.DeclareVariable($name, $value, "nonConst") } # This method returns the code for declaring a variable, choosing if it is const or non-const. [String]DeclareVariable([String]$name, [String]$value, [String]$constPolicy) { return Get-Puppeteer_DeclareVariable -Name $name -Value $value -Const:($constPolicy -eq "const") } # This method returns the code for delaying for a specified number of seconds. [String]DelaySeconds([Int32]$seconds) { return $this.DelayMilliseconds($seconds * 1000) } # This method returns the code for delaying for a specified number of milliseconds. [String]DelayMilliseconds([Int32]$milliseconds) { return Get-Puppeteer_Delay -Milliseconds $milliseconds } # This method returns the code for entering text into a selector. [String]EnterTextIntoSelector([String]$selectorName, [String]$text) { return Get-Puppeteer_EnterTextIntoSelector -SelectorName $selectorName -Text $text } # This method returns the code for entering the value of a variable into a selector. [String]EnterVariableIntoSelector([String]$selectorName, [String]$variableName) { return Get-Puppeteer_EnterVariableIntoSelector -SelectorName $selectorName -VariableName $variableName } # This method returns the code for importing the Puppeteer requirements. [String]ImportRequirements() { return Get-Puppeteer_ImportRequirements } # This method returns the code for launching a chrome browser [String]LaunchChromeBrowser() { return Get-Puppeteer_LaunchChromeBrowser } # This method returns the code for logging a message. [String]LogMessage([String]$message) { return Get-Puppeteer_LogMessage -Message $message } # This method returns the code for logging the current url. [String]LogUrl() { return Get-Puppeteer_LogUrl } # This method returns the code for logging the value of a variable. [String]LogVariable([String]$variableName) { return Get-Puppeteer_LogVariable -VariableName $variableName } # This method returns the code for navigating a new browser page to a link. [String]NavigateNewBrowserPageToLink([String]$link) { return Get-Puppeteer_NavigateNewBrowserPageToLink -Link $link } # This method returns the code for retrieving text from a selector. [String]RetrieveTextFromSelector([String]$selectorName, [String]$variableName) { return Get-Puppeteer_RetrieveTextFromSelector -SelectorName $selectorName -VariableName $variableName } # This method returns the code for setting the chrome browser options. [String]SetChromeBrowserOptions([String]$headlessPolicy, [Int32]$width, [Int32]$height) { return Get-Puppeteer_SetChromeBrowserOptions -IsHeadless ($headlessPolicy -eq "headless") -Width $width -Height $height } # This method returns the code for taking a screenshot. [String]TakeScreenshot([String]$filePath) { return Get-Puppeteer_TakeScreenshot -FilePath $filePath } # This method returns the code for throwing an exception upon encountering an unhandled rejection. [String]ThrowExceptionOnUnhandledRejection() { return Get-Puppeteer_ThrowExceptionOnUnhandledRejection } # This method returns the code for opening an async function declaration. [String]OpenAsyncFunctionDeclaration([String]$functionName) { return Get-Puppeteer_OpenAsyncFunctionDeclaration -FunctionName $functionName } #################### # Compound Methods # #################### # This method returns the code for: # Writing a log message # Taking a screen shot using the processed log message as the file name [String]LogMessageWithScreenshot([String]$message) { # Generate the code $code = $this.LogMessage($message) # Add the screenshot code if ($this.ScreenshotsEnabled) { # Replace spaces with '_', remove non alphanumeric characters from the message # and trim to 100 characters $processedMessage = (($message -replace " ", "_") -replace "[^\w]*", "") if ($processedMessage.Length -gt 100) { $processedMessage = $processedMessage.Substring(0, 100) } # Generate the screenshot index string - format the index to be 3 characters wide $screenshotIndexString = $this.ScreenshotIndex.ToString("000") $this.ScreenshotIndex += 1 # Set the screenshot file path if (![String]::IsNullOrWhiteSpace($this.ScreenshotFolder)) { $screenshotFilePath = "$($this.ScreenshotFolder)/$($screenshotIndexString)-$($processedMessage).jpeg" } else { $screenshotFilePath = "$($screenshotIndexString)$($processedMessage).jpeg" } # Add to the code $code += "`r`n" $code += $this.TakeScreenshot($screenshotFilePath) } # Return the code return $code } } <# .SYNOPSIS This function returns a new Puppeteer code generator object. #> function New-PuppeteerCodeGenerator { [CmdletBinding(PositionalBinding=$true)] [OutputType([PuppeteerCodeGenerator])] param () return [PuppeteerCodeGenerator]::new() } |