Testing a Website with Powershell

Posted by & filed under , .

With powershell, you can easily browse a webpage with IE. You can click or edit HTML elements and run a test case.

Let’s make an example of Facebook Login.

Here is the roadmap.

1-)First create a IE com object.
2-)Set the properties of com object, and put a sleep command while getting the web pages.
3-)Find the related html element ID’s which you will append your username and password. You can use developer mode on IE.

chromedevelop

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://www.facebook.com") # Here is the link.
$ie.Visible=$true #It makes the IE visible. You can run as a background job.
$ie.FullScreen=$true # If you take screenshot it is helpful.
Start-Sleep -Seconds 5
$username="mail@mail.com"
$password="Passed123"
$ie.document.getElementById("email").value= "$ad"
$ie.document.getElementById("pass").value= "$password"
$ie.document.getElementById("login_form").submit()

Now we logged in facebook. We can put an assertion like ‘if this element exists or not’ and complete the test case.

For example I am looking for a search box in Facebook home page and if exists I accept that I successfully logged in.

$kontrol = $ie.getElementByID("pageNav")
if($kontrol.role.StartsWith("sear"))
{
Write-Host "Successful"
}
else
{
Write-Host "Failed"
}

ornekkod