Category Archives: Automatic Testing

Sending an email with attachments using Outlook and PowerShell

A few days ago, I had to find a way to automatically send an email with attachments, at a predetermined time, using Outlook and PowerShell. I decided to use a Windows Scheduled Task, that would wake up at that time and invoke a PowerShell script.

The script below is the result of a collection of snippets and bits of code I found in various places around the Internet that allowed me to solve this problem.

For this example, I only want to send the files which have a specific extension (.html) and that exist in a specific folder (passed as parameter to the PowerShell script). Of course, you are free to remove this extension filtering for your own purposes.

Also, note that Outlook must have a configured email account for the sending process to succeed.

# Check to see we have all the arguments
if($args.Count -lt 1)
{
Write-Host "Use: SendMail.ps1 <Path>"
Write-Host
Write-Host " <Path>: Full path for the folder which contains the files"
Write-Host
exit
}

$FullPath=$args[0]

#Get an Outlook application object

$o = New-Object -com Outlook.Application

$mail = $o.CreateItem(0)

#2 = High importance message
$mail.importance = 2

$mail.subject = "This is the subject of the mail"
$mail.body = "This is the body of the email. It has been automatically generated by a script."

#separate multiple recipients with a ";"
$mail.To = <INSERT THE RECIPIENT HERE>
#$mail.CC = <OTHER RECIPIENT 1>;<OTHER RECIPIENT 2>

# Iterate over all files and only add the ones that have an .html extension
$files = Get-ChildItem $FullPath

for ($i=0; $i -lt $files.Count; $i++) {

$outfileName = $files[$i].FullName
$outfileNameExtension = $files[$i].Extension

# if the extension is the one we want, add to attachments
if($outfileNameExtension -eq ".html")
{
$mail.Attachments.Add($outfileName);
}
}

$mail.Send()

# give time to send the email
Start-Sleep 20

# quit Outlook
$o.Quit()

#end the script
exit

You can invoke the script presented above, by using a simple command window and giving the following command:

PowerShell Command

I hope this helps!

Automatic Testing with People Picker in SharePoint 2007

I have been working on automatic testing for SharePoint 2007 web applications. In our tests, user interaction with the web interface is simulated by clicking links, selecting entries in combo boxes, typing text and so on, just as a normal user would do.
Initially, it is up to a human user to interact with the web application while an external program records all his steps. If all goes well, it is possible to “play” the recorded steps and thus simulate another interaction.
It is also the automatic test’s responsibility to assert that all the expected conditions are met in the end of the test. These assertions determine if a test has passed or if it has failed. This way, repeating previously run tests to ensure backwards compatibility becomes trivial.

To do this, we’ve been using Telerik’s WebUI Test Studio. Although this tool is not exactly SharePoint-oriented, it still allows us to test our SharePoint components in a very satisfactory way.
This does not mean, however, that we are able to use all the recorded steps in an unmodified way. In fact, sometimes we have to tweak the recorded steps manually in order to select the proper UI element. For instance, sometimes the recording tool will assume that we are clicking on an html div element instead of the table cell that is contained in it. Thus, what we usually do is try to narrow down the search criteria of the automation framework by using specific attributes of that table cell or if all else fails, we convert that particular recorded step into manual C# code, which we can manipulate the way we see fit.

In this case, I was trying to simulate a text insertion into the SharePoint people picker control, but the recording tool was not capturing the events correctly.

People Picker

After exhausting all automatic possibilities (by tweaking the automation framework’s search logic of element attributes) I decided to use manual C# code to solve the problem.

Step 1: I recorded an automatic step, by clicking anywhere on the page. This gave me a step that I could transform into code. Then I converted the recorded step into a coded step.

 

Convert to code

 

This creates an automatic C# code file which we can edit to suit our needs. The automatic generated code looks something like this:

[CodedStep(@"Click 'td_73'")]
public void Click_td_73_CodedStep()
{
    // Click 'td_73'
    HtmlTableCell td_73 = Pages.Home__AutoTests.td_73;
    td_73.Wait.ForExists(10000);
    td_73.Click(false);
}

Step 2: In this case, I wanted to simulate the insertion of text into the people picker. Thus, I used WebUI Test Studio’s Overlay Surface feature to correctly identify the element I was trying to manipulate.

 

Overlay Surface and Dom Explorer

 

By checking the page’s DOM tree, I was able to determine that the element I was trying to manipulate was an HTML textarea, with a div before it. However, when we try to insert text into the textarea element, nothing happens.

Step 3: After a few tests and after discussing the issue in the Telerik support forums, I was able to work around the problem.

In fact, the element that we want to manipulate isn’t the textarea itself, but the div above it. So, we start by adding the div element to the project Elements pane so we can reference it freely in the C# code.

 Add to Project Elements

Step 4: After that, we must insert the following code into the recorded step that was converted to manual C# code ( the idea is to replace the automatically generated C# code that was presented in Step 1).

 

[CodedStep(@"Insert into people picker")]
public void InsertItemInPeoplePicker_CodedStep()
{
    //Note: the div_PlaceHolder__UpLevelDiv object represents the div we want to 
    //manipulate. It was added to the Project Elements in Step 3.
    HtmlControl peoplePicker = Pages.Customize_Workflow.div_PlaceHolder__UpLevelDiv;
    peoplePicker.Wait.ForExists(10000);
    peoplePicker.ScrollToVisible(ArtOfTest.WebAii.Core.ScrollToVisibleType.ElementTopAtWindowTop);
    ActiveBrowser.Window.SetFocus();
    peoplePicker.MouseClick();
    Manager.Desktop.KeyBoard.TypeText("testDeveloper", 200);
}

 

After the replacement of the automatic code with the code presented above, it is possible to insert the text “testDeveloper” into the people picker. And that’s it.

Hope this helps.