Functions/New-PuppeteerCodeGenerator.Tests.ps1
describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" it "returns a Puppeteer code generator" { # Call the function $output = New-PuppeteerCodeGenerator # Verify the output $output.GetType().Name | Should Be "PuppeteerCodeGenerator" } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/CallFunction" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_CallFunction { param ($FunctionName) } mock Get-Puppeteer_CallFunction {} it "calls Get-Puppeteer_CallFunction" { # Call the function (New-PuppeteerCodeGenerator).CallFunction("FooBar") # Verify the mocks Assert-MockCalled Get-Puppeteer_CallFunction -Times 1 -Exactly -ParameterFilter { $FunctionName -eq "FooBar" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/ClickOnSelector" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_ClickOnSelector { param ($SelectorName, [Switch]$WaitForPageToLoad) } mock Get-Puppeteer_ClickOnSelector {} it "calls Get-Puppeteer_ClickOnSelector with wait enabled by default" { # Call the function (New-PuppeteerCodeGenerator).ClickOnSelector("#Selector") # Verify the mocks Assert-MockCalled Get-Puppeteer_ClickOnSelector -Times 1 -Exactly -ParameterFilter { $SelectorName -eq "#Selector" -and $WaitForPageToLoad } -Scope it } it "calls Get-Puppeteer_ClickOnSelector with wait enabled" { # Call the function (New-PuppeteerCodeGenerator).ClickOnSelector("#Selector", "wait") # Verify the mocks Assert-MockCalled Get-Puppeteer_ClickOnSelector -Times 1 -Exactly -ParameterFilter { $SelectorName -eq "#Selector" -and $WaitForPageToLoad } -Scope it } it "calls Get-Puppeteer_ClickOnSelector with wait disabled" { # Call the function (New-PuppeteerCodeGenerator).ClickOnSelector("#Selector", "noWait") # Verify the mocks Assert-MockCalled Get-Puppeteer_ClickOnSelector -Times 1 -Exactly -ParameterFilter { $SelectorName -eq "#Selector" -and !$WaitForPageToLoad } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/CloseChromeBrowser" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_CloseChromeBrowser {} mock Get-Puppeteer_CloseChromeBrowser {} it "calls Get-Puppeteer_CloseChromeBrowser" { # Call the function (New-PuppeteerCodeGenerator).CloseChromeBrowser() # Verify the mocks Assert-MockCalled Get-Puppeteer_CloseChromeBrowser -Times 1 -Exactly -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/CloseFunctionDeclaration" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_CloseFunctionDeclaration {} mock Get-Puppeteer_CloseFunctionDeclaration {} it "calls Get-Puppeteer_CloseFunctionDeclaration" { # Call the function (New-PuppeteerCodeGenerator).CloseFunctionDeclaration() # Verify the mocks Assert-MockCalled Get-Puppeteer_CloseFunctionDeclaration -Times 1 -Exactly -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/DeclareDelayFunction" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_DeclareDelayFunction {} mock Get-Puppeteer_DeclareDelayFunction {} it "calls Get-Puppeteer_DeclareDelayFunction" { # Call the function (New-PuppeteerCodeGenerator).DeclareDelayFunction() # Verify the mocks Assert-MockCalled Get-Puppeteer_DeclareDelayFunction -Times 1 -Exactly -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/DeclareUsernameAndPassword" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_DeclareUsernameAndPassword { param ([PSCredential]$credential) } mock Get-Puppeteer_DeclareUsernameAndPassword {} it "calls Get-Puppeteer_DeclareUsernameAndPassword with the provided credential" { # Call the function (New-PuppeteerCodeGenerator).DeclareUsernameAndPassword([PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))) # Verify the mocks Assert-MockCalled Get-Puppeteer_DeclareUsernameAndPassword -Times 1 -Exactly -ParameterFilter { $Credential.username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/DeclareVariable" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_DeclareVariable { param ($Name, $Value, [Switch]$Const) } mock Get-Puppeteer_DeclareVariable {} it "calls Get-Puppeteer_DeclareVariable with a non-const variable by default" { # Call the function (New-PuppeteerCodeGenerator).DeclareVariable("theAnswer", "42") # Verify the mocks Assert-MockCalled Get-Puppeteer_DeclareVariable -Times 1 -Exactly -ParameterFilter { $Name -eq "theAnswer" -and $Value -eq "42" -and !$Const } -Scope it } it "calls Get-Puppeteer_DeclareVariable with a const variable" { # Call the function (New-PuppeteerCodeGenerator).DeclareVariable("theAnswer", "42", "const") # Verify the mocks Assert-MockCalled Get-Puppeteer_DeclareVariable -Times 1 -Exactly -ParameterFilter { $Name -eq "theAnswer" -and $Value -eq "42" -and $Const } -Scope it } it "calls Get-Puppeteer_DeclareVariable with a non-const variable" { # Call the function (New-PuppeteerCodeGenerator).DeclareVariable("theAnswer", "42", "nonConst") # Verify the mocks Assert-MockCalled Get-Puppeteer_DeclareVariable -Times 1 -Exactly -ParameterFilter { $Name -eq "theAnswer" -and $Value -eq "42" -and !$Const } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/DelaySeconds" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_Delay { param ($Milliseconds) } mock Get-Puppeteer_Delay {} it "calls Get-Puppeteer_Delay with the number of seconds * 1000" { # Call the function (New-PuppeteerCodeGenerator).DelaySeconds(10) # Verify the mocks Assert-MockCalled Get-Puppeteer_Delay -Times 1 -Exactly -ParameterFilter { $Milliseconds -eq 10000 } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/DelayMilliseconds" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_Delay { param ($Milliseconds) } mock Get-Puppeteer_Delay {} it "calls Get-Puppeteer_Delay" { # Call the function (New-PuppeteerCodeGenerator).DelayMilliseconds(10000) # Verify the mocks Assert-MockCalled Get-Puppeteer_Delay -Times 1 -Exactly -ParameterFilter { $Milliseconds -eq 10000 } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/EnterTextIntoSelector" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_EnterTextIntoSelector { param ($SelectorName, $Text) } mock Get-Puppeteer_EnterTextIntoSelector {} it "calls Get-Puppeteer_EnterTextIntoSelector" { # Call the function (New-PuppeteerCodeGenerator).EnterTextIntoSelector("#selector", "text") # Verify the mocks Assert-MockCalled Get-Puppeteer_EnterTextIntoSelector -Times 1 -Exactly -ParameterFilter { $SelectorName -eq "#selector" -and $Text -eq "text" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/EnterVariableIntoSelector" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_EnterVariableIntoSelector { param ($SelectorName, $VariableName) } mock Get-Puppeteer_EnterVariableIntoSelector {} it "calls Get-Puppeteer_EnterVariableIntoSelector" { # Call the function (New-PuppeteerCodeGenerator).EnterVariableIntoSelector("#selector", "answer") # Verify the mocks Assert-MockCalled Get-Puppeteer_EnterVariableIntoSelector -Times 1 -Exactly -ParameterFilter { $SelectorName -eq "#selector" -and $VariableName -eq "answer" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/ImportRequirements" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_ImportRequirements {} mock Get-Puppeteer_ImportRequirements {} it "calls Get-Puppeteer_ImportRequirements" { # Call the function (New-PuppeteerCodeGenerator).ImportRequirements() # Verify the mocks Assert-MockCalled Get-Puppeteer_ImportRequirements -Times 1 -Exactly -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/LaunchChromeBrowser" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_LaunchChromeBrowser {} mock Get-Puppeteer_LaunchChromeBrowser {} it "calls Get-Puppeteer_LaunchChromeBrowser" { # Call the function (New-PuppeteerCodeGenerator).LaunchChromeBrowser() # Verify the mocks Assert-MockCalled Get-Puppeteer_LaunchChromeBrowser -Times 1 -Exactly -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/LogMessage" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_LogMessage { param ($Message) } mock Get-Puppeteer_LogMessage {} it "calls Get-Puppeteer_LogMessage" { # Call the function (New-PuppeteerCodeGenerator).LogMessage("Hello world!") # Verify the mocks Assert-MockCalled Get-Puppeteer_LogMessage -Times 1 -Exactly -ParameterFilter { $Message -eq "Hello world!" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/LogUrl" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_LogUrl {} mock Get-Puppeteer_LogUrl {} it "calls Get-Puppeteer_LogUrl" { # Call the function (New-PuppeteerCodeGenerator).LogUrl() # Verify the mocks Assert-MockCalled Get-Puppeteer_LogUrl -Times 1 -Exactly -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/LogVariable" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_LogVariable { param ($VariableName) } mock Get-Puppeteer_LogVariable {} it "calls Get-Puppeteer_LogVariable" { # Call the function (New-PuppeteerCodeGenerator).LogVariable("answer") # Verify the mocks Assert-MockCalled Get-Puppeteer_LogVariable -Times 1 -Exactly -ParameterFilter { $VariableName -eq "answer" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/NavigateNewBrowserPageToLink" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_NavigateNewBrowserPageToLink { param ($Link) } mock Get-Puppeteer_NavigateNewBrowserPageToLink {} it "calls Get-Puppeteer_NavigateNewBrowserPageToLink" { # Call the function (New-PuppeteerCodeGenerator).NavigateNewBrowserPageToLink("https://bittitan.com") # Verify the mocks Assert-MockCalled Get-Puppeteer_NavigateNewBrowserPageToLink -Times 1 -Exactly -ParameterFilter { $Link -eq "https://bittitan.com" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/OpenAsyncFunctionDeclaration" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_OpenAsyncFunctionDeclaration { param ($FunctionName) } mock Get-Puppeteer_OpenAsyncFunctionDeclaration {} it "calls Get-Puppeteer_OpenAsyncFunctionDeclaration" { # Call the function (New-PuppeteerCodeGenerator).OpenAsyncFunctionDeclaration("FooBar") # Verify the mocks Assert-MockCalled Get-Puppeteer_OpenAsyncFunctionDeclaration -Times 1 -Exactly -ParameterFilter { $FunctionName -eq "FooBar" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/RetrieveTextFromSelector" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_RetrieveTextFromSelector { param ($SelectorName, $VariableName) } mock Get-Puppeteer_RetrieveTextFromSelector {} it "calls Get-Puppeteer_RetrieveTextFromSelector" { # Call the function (New-PuppeteerCodeGenerator).RetrieveTextFromSelector("#selector", "answer") # Verify the mocks Assert-MockCalled Get-Puppeteer_RetrieveTextFromSelector -Times 1 -Exactly -ParameterFilter { $SelectorName -eq "#selector" -and $VariableName -eq "answer" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/SetChromeBrowserOptions" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_SetChromeBrowserOptions { param ($IsHeadless, $Width, $Height) } mock Get-Puppeteer_SetChromeBrowserOptions {} it "calls Get-Puppeteer_SetChromeBrowserOptions with headless enabled" { # Call the function (New-PuppeteerCodeGenerator).SetChromeBrowserOptions("headless", 1920, 1080) # Verify the mocks Assert-MockCalled Get-Puppeteer_SetChromeBrowserOptions -Times 1 -Exactly -ParameterFilter { $IsHeadless -and $Width -eq 1920 -and $Height -eq 1080 } -Scope it } it "calls Get-Puppeteer_SetChromeBrowserOptions with headless disabled" { # Call the function (New-PuppeteerCodeGenerator).SetChromeBrowserOptions("notHeadless", 1920, 1080) # Verify the mocks Assert-MockCalled Get-Puppeteer_SetChromeBrowserOptions -Times 1 -Exactly -ParameterFilter { !$IsHeadless -and $Width -eq 1920 -and $Height -eq 1080 } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/SetValueForSelector" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_SetValueForSelector { param ($SelectorName, $Value) } mock Get-Puppeteer_SetValueForSelector {} it "calls Get-Puppeteer_SetValueForSelector" { # Call the function (New-PuppeteerCodeGenerator).SetValueForSelector("#selector", "value") # Verify the mocks Assert-MockCalled Get-Puppeteer_SetValueForSelector -Times 1 -Exactly -ParameterFilter { $SelectorName -eq "#selector" -and $Value -eq "value" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/TakeScreenshot" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_TakeScreenshot { param ($FilePath) } mock Get-Puppeteer_TakeScreenshot {} it "calls Get-Puppeteer_TakeScreenshot" { # Call the function (New-PuppeteerCodeGenerator).TakeScreenshot("screenshot.jpeg") # Verify the mocks Assert-MockCalled Get-Puppeteer_TakeScreenshot -Times 1 -Exactly -ParameterFilter { $FilePath -eq "screenshot.jpeg" } -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/ThrowExceptionOnUnhandledRejection" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_ThrowExceptionOnUnhandledRejection {} mock Get-Puppeteer_ThrowExceptionOnUnhandledRejection {} it "calls Get-Puppeteer_ThrowExceptionOnUnhandledRejection" { # Call the function (New-PuppeteerCodeGenerator).ThrowExceptionOnUnhandledRejection() # Verify the mocks Assert-MockCalled Get-Puppeteer_ThrowExceptionOnUnhandledRejection -Times 1 -Exactly -Scope it } } describe "BitTitan.Runbooks.UIAutomation/New-PuppeteerCodeGenerator/LogMessageWithScreenshot" -Tag "module", "unit" { # Import the function to test . "$($PSScriptRoot)\New-PuppeteerCodeGenerator.ps1" # Declare mocks function Get-Puppeteer_TakeScreenshot { param ($FilePath) } mock Get-Puppeteer_LogMessage {} function Get-Puppeteer_LogMessage { param ($Message) } mock Get-Puppeteer_TakeScreenshot {} # Create the code generator object $codeGenerator = New-PuppeteerCodeGenerator $codeGenerator.ScreenshotsEnabled = $true it "logs messages with corresponding screenshots to the specified screenshots folder" { $codeGenerator.ScreenshotsFolder = "screenshotsFolder" # Call the function $codeGenerator.LogMessageWithScreenshot("screenshot message") # Verify the mocks Assert-MockCalled Get-Puppeteer_TakeScreenshot -Times 1 -Exactly -ParameterFilter { $FilePath -eq "screenshotsFolder/000-screenshot_message.jpeg" } -Scope it Assert-MockCalled Get-Puppeteer_LogMessage -Times 1 -Exactly -ParameterFilter { $Message -eq "screenshot message" } -Scope it # Call the function $codeGenerator.LogMessageWithScreenshot("extremely long screenshot message which will be trimmed before it becomes a filepath for the screenshot jpeg file") # Verify the mocks Assert-MockCalled Get-Puppeteer_TakeScreenshot -Times 1 -Exactly -ParameterFilter { $FilePath -eq "screenshotsFolder/001-extremely_long_screenshot_message_which_will_be_trimmed_before_it_becomes_a_filepath_for_the_screens.jpeg" } -Scope it Assert-MockCalled Get-Puppeteer_LogMessage -Times 1 -Exactly -ParameterFilter { $Message -eq "extremely long screenshot message which will be trimmed before it becomes a filepath for the screenshot jpeg file" } -Scope it } it "logs messages with corresponding screenshots to the root folder" { $codeGenerator.ScreenshotIndex = 0 $codeGenerator.ScreenshotsFolder = "" # Call the function $codeGenerator.LogMessageWithScreenshot("screenshot message") # Verify the mocks Assert-MockCalled Get-Puppeteer_TakeScreenshot -Times 1 -Exactly -ParameterFilter { $FilePath -eq "000-screenshot_message.jpeg" } -Scope it Assert-MockCalled Get-Puppeteer_LogMessage -Times 1 -Exactly -ParameterFilter { $Message -eq "screenshot message" } -Scope it # Call the function $codeGenerator.LogMessageWithScreenshot("extremely long screenshot message which will be trimmed before it becomes a filepath for the screenshot jpeg file") # Verify the mocks Assert-MockCalled Get-Puppeteer_TakeScreenshot -Times 1 -Exactly -ParameterFilter { $FilePath -eq "001-extremely_long_screenshot_message_which_will_be_trimmed_before_it_becomes_a_filepath_for_the_screens.jpeg" } -Scope it Assert-MockCalled Get-Puppeteer_LogMessage -Times 1 -Exactly -ParameterFilter { $Message -eq "extremely long screenshot message which will be trimmed before it becomes a filepath for the screenshot jpeg file" } -Scope it } } |