Execute actions in PowerShell against a browser object

Execute actions in PowerShell against a browser object header image

Using PowerShell as a warm up script can be a handy way to hit pages once every while. And as pointed out in a blog on using PowerShell Warmup script with a browser object, there are a few different approaches. One of those approaches is using the browser object with PowerShell that has as advantage that you will open a real browser object that allows for script loading. When opening a page through the browser object with PowerShell you will not only be able to check the ready state of the document but you can also execute some other tasks. Let’s say you want some basic ‘testing’ done through PowerShell, for instance fill in a form a few times and submitting it. Now you can do that through Visual Studio Ultimate with a web test, but that might not be an option due to licensing.

So let’s have a quick look at how you can create a script that can do some basic actions, let’s say you would like to test if your service returns a value if you click on a button. Like in the warmup sample, you can retrieve a ComObject and pass an URL into it. Having an a document object allows for some manipulation. You can get elements like you would do in JavaScript by and modify them the same way:

$inputbox = $ie.Document.getElementByID('sb_form_q');
$inputbox.value = "My Value";

As you can imagine clicking a button will be easy as well

$button = $ie.Document.getElementByID('sb_form_go');
$button.click();

So stitching it all together let’s assume you will need a script that would open a page, add some text to an input box and then click on a button that then executes some action based on your input.

Function TestPage ($url, $query)
{
    $ie = New-Object -ComObject InternetExplorer.Application
    $ie.visible=$true
    $ie.navigate2($url);

    while($ie.Busy) {Start-Sleep 2}

    if($ie.Document.nameProp -ne "Internet Explorer cannot display the webpage"){
        $inputbox = $ie.Document.getElementByID('sb_form_q');
        if($inputbox -ne $null) {
            $inputbox.value = $query;
            $button = $ie.Document.getElementByID('sb_form_go');
            if($button -ne $null) {
                $button.click();
                while($ie.Busy) {Start-Sleep 2}
            }
            else {
                Write-Host "Button not found on: " $url;
            }
        }
        else {
                Write-Host "Input box not found on: " $url;
            }
    }
    else {
        Write-Host "URL could not be loaded in IE: " $url;
    }
    #Close IE if required, for demo we skip it $ie.Quit();
}

TestPage "https://www.bing.com" "Test";

Obviously you can extend this scenario to read the output after the button is clicked. All you need to do is to read the $ie.Document tag and you can read the rendered HTML.As you probably can imagine this can be used for periodically checks or actions that you need to do on a site but you do not have the option to configure either web tests or other methods. It’s an easy and quick way to test specific calls, though as soon as you need to do multiple calls can become complicated and hard very quickly. However if you just want to do a single action and see if that action works or fails it is a matter of updating a few lines of code and you are ready to go.

Loading comments…