Agile Software Testing

Helping QA professionals in the world of Agile Software Development
Options:

Archive for the ‘Automation’ Category

Merging Hashes in Powershell

I’m sad to say this, but I was not able to find a default hash.merge command in powershell. So, after spending some time researching, i just had to bite the bullet and make my own function for merging. Hope this helps

function Merge-Hash{
param($parentHash, $childHash)

foreach ($childKey in $childHash.keys){
$parentHash.$childKey = $childHash.$childKey
}

return $parentHash
}

To test it, you can use this simple call in powershell

$hash1 = @{“a”=”1″;”b”=”2″;”c”=”3″}

  • Share/Bookmark

The story:

I have several powershell scripts that I want to trigger via website, these scripts will start doing a lot of things on different computers on my network. We chose to use PowershellASP as the website server app, that allows you to call PS scripts from withing the HTML. This product works great! Check it out.

The Problem:

If you wish to invoke a command with PSASP, you just put in a PS snippet in HTML and your script is ran :)

< % Invoke-Command -ScriptBlock { Get-Services }

You will be redirected to a page, which will have the output from your command. This is all good and great, but what if you want to do multiple tasks on multiple computers at the same time? Then you need to start some background jobs. However, it seems that background jobs only run in the current PS window, and if you close the window down, the job is terminated. This becomes a problem because you do not have a continuous window in PSASP, and as soon as you start the background process the ASP process is terminated and your job with it.

The Solution:

What you need to do is start a process Start-Process cmdlt. Keep in mind that start-process starts executables. So, we have to execute the powershell.exe and pass in a .ps1 file as an argument. This file will do our job for us J

Here is some code:

< %

$ps = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe"

$working_dir = "C:\temp\ " #location of the ps.1 scripts to be executed

$script = ".\process.ps1"

$script2 = ".\process2.ps1"

cd $working_dir #Go into the working dir, for some reason

#-WorkingDirectory Switch did not work for me :(

Start-Process $ps $script -RedirectStandardError “C:\TEMP\error.txt” -RedirectStandardOutput “c:\TEMP\out.txt”

Start-Process $ps $script2 -RedirectStandardError “C:\TEMP\error2.txt” -RedirectStandardOutput “c:\TEMP\out2.txt”

%>

Please notice that I’m redirecting the standard error and standard out into a file, this is the only way you will be able to debug, otherwise the Start-Process will swallow any error you get before you have a chance to actually see it.

Finally, put something inside the 2 scripts you will run, and give this a try J

Invoke-Command -ScriptBlock {

Start-Sleep 15

“Job 1 Complete!” | Out-File “C:\temp\success.txt”

}

This script is super simple, sleep for 15 seconds then output something into file. Both of my scripts did this at different intervals so you can see them as they finish up.

  • Share/Bookmark

How to make selenium wait for AJAX to finish, the right way

Click to continue reading “Selenium wait for AJAX (the right way!!!)”

  • Share/Bookmark