Fun with UIAutomation and calc

Last night I got a cool idea how to show some basic ideas of UIAutomation. I did decide to-do a UIAutomation example with calc as my test subject. The idea is to give a mathematical expression to the program, which will then use calc to solve the problem and show you the result. The main thing is to show how UIAutomation is working and not to calculate.

The first thing that we need is a method that can find an AutomationElement for us. An AutomationElement is anything that you can see on the screen as buttons and input fields. The method take a root AutomationElement that define where to start to search, a name and a class name for the object that we are looking for. We then create two property conditions based on the name and class name. These two conditions are then combined in an AndCondition. We also have OrCondition and NotCondition to use. The last thing we do is to search for the first AutomationElement that match the condition.

private static AutomationElement GetElement(AutomationElement root, string name, string className)
{
    PropertyCondition condName = new PropertyCondition(AutomationElement.NameProperty, name);
    PropertyCondition condClassName = new PropertyCondition(AutomationElement.ClassNameProperty, className);
    AndCondition cond = new AndCondition(condName, condClassName);
    return root.FindFirst(TreeScope.Descendants, cond);
}

Next method that we need is one that can return a invoke pattern. Invoke pattern is used for example to click on a button. This will be used to be able to click on the different buttons in calc

private static InvokePattern GetInvokePattern(AutomationElement element)
{
    return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
}

Read more

.NET Cheat Sheets

When I was surfing around today I found this site that contain a bunch of different cheat sheets for .NET. The one that I find really useful was ‘Visual Studio 2005 Built-in Code Snippets‘. So simple have fun with this findings

Easy usage of databases in PHP

When I started with development for PHP I did offend run into writing specific MySQL database code over and over in all my files. After suddenly running into something called Postegres SQL I quickly learned that this was not a good approach. I did also learn that it was really hard to debug database error with default error information, especially after the site was deployed for public usage.

So I decided to write the kill class that would solve all this small problems. The result was a simple database wrapper class container the most important functionality as:
- Connect
- Query
- Get next record in result (Perfect suited to be used with while loops)
- Get number of affected rows for SELECT, UPDATE, DELETE, INSERT
- Optimize table
- Clear result to free up memory
- Close connection
Get next record snippet

function next_record()
{
  $this->Record = mysql_fetch_array($this->Query_ID);
  $this->Errno = mysql_errno();
  $this->Error = mysql_error();
  $stat = is_array($this->Record);
  if (!$stat)
  {
    mysql_free_result($this->Query_ID);
    $this->Query_ID = 0;
  }
  else
  {
    $this->Row += 1;
  }
 
  return $this->Record;
}

I also added some error handling that enable verbose error on the page during development mode and verbose error message with mail after deployment. The error result contains information as:
- General error message
- Error number and error text from the SQL server
- HTTP referrer
- Requested URI
- GET data
- POST data
And since I did use my class a lot for system with user authentication I did add user ID and user name for current user getting the error.

I hope this code will help someone out there to not end up with my problem and reinvent the wheel again.

MySQL version:db_inc.zip
PostrgressSQL version:db_inc_pg.zip

Usage example of the class

$db = new DB();
$db->connect();
$db->query("SELECT * FROM Blog ORDER BY Date");
while($data = $db->next_record())
{
  $id = $data['ID']
  // Do more stuff
}

Zipping files in C#

For a while ago I tried to create zip files to be used by windows built in zip functionality. Did try to use GZipStream but I found out that I needed a GZip expander installed to be able to use it. I don’t understand why we have an inbuilt function in VS to create a compressed format that Windows can’t open natively. Anyway after a lot of search I found DotNetZip. It has a really easy to use API and no dependencies to other DLL’s.

It supports the following scenarios
-creating a zip archive, adding files or directories into the archive
-extracting files from an existing archive
-modifying an existing archive – removing entries from an archive or adding new entries to an archive
-password protecting entries in the archive
-getting entry input from a file or a stream
-reading a zip file from a file or a stream
-extracting an entry into a file or a stream

Example Usage

try
{
  using (ZipFile zip = new ZipFile("MyZipFile.zip")
  {
     zip.AddFile("c:\photos\personal\7440-N49th.png");
     zip.AddFile("c:\Desktop\2005_Annual_Report.pdf");
     zip.AddFile("ReadMe.txt");
     zip.Save();
  }
}
catch (System.Exception ex1)
{
  System.Console.Error.WriteLine("exception: " + ex1);
}

How to delete a element in an array

This problem sounds where simple, but it did take me a lot of effort to figure out that PHP doesn’t have any really good functionallity for it. So did create my own version.

<?php
function in_array_delete($array, $item)
{
  if (isset($array[$item]))
  {
    unset($array[$item]);
  }
  return array_merge($array);
}
?>