Tuesday, April 15, 2008

Thursday, April 03, 2008

Using The Microsoft UI Automation Library to Drive UI Automation

The UI Automation Library is included in the .NET Framework 3.0. The concept seems to be straightforward. Every UI control is an "AutomationElement" which can be found through searching from the Parent "AutomationElement". The top AutomationElement is defined as the "RootElement", which is the Desktop. Another way to get the AutomationElement Object to a given Window is by starting a "Process", and then using the Process MainWindowHandle to get the AutomationElement object.

The code shown below is basically a test that Invoke Notepad, Enters an input text, and then it compares with the text which is read from Text property of the Notepad Text Field, and then closes Notepad without saving.
using System;
using System.Windows.Automation;
using System.Diagnostics;
using System.Threading;
using System.Collections.Generic;
using System.Text;

namespace UIAuto_01
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting Notepad!!!");

// #1 Start Notepad
//
Process notepadProcess = Process.Start("notepad.exe");
Thread.Sleep(500);
AutomationElement aeDesktop = AutomationElement.RootElement;

// #2 Find Notepad
//
AutomationElement aeNotepad = AutomationElement.FromHandle(notepadProcess.MainWindowHandle);

if (null == aeNotepad)
{
throw new Exception("Notepad not found!");
}
Console.WriteLine("Found Notepad !!!!");

// #3 Find Notepad Text Field Control
//
AutomationElementCollection aeAllTextBoxes =
aeNotepad.FindAll(TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty,
ControlType.Document));

if (0 == aeAllTextBoxes.Count)
{
throw new Exception("Notepad Edit field not found!!");
}

Console.WriteLine("Found Notepad Edit Field!!!!");

AutomationElement aeNotepadDoc = aeAllTextBoxes[0];

// #4 Type the input value
//
aeNotepadDoc.SetFocus();
string InputValue = "Hello World!";
System.Windows.Forms.SendKeys.SendWait(InputValue);

// #5 Get the Text from the Text Field
//
TextPattern tpNotepadDoc = (TextPattern)aeNotepadDoc.GetCurrentPattern(TextPattern.Pattern);
string ActualValue = tpNotepadDoc.DocumentRange.GetText(-1).Trim();

// #6 Compare input and actual
//
if (ActualValue == InputValue)
{
Console.WriteLine("Test Passed!");
}
else
{
Console.WriteLine("Test Failed: Expected={0} Actual={1}", InputValue, ActualValue);
}

// #7 Now close Notepad
//
WindowPattern wpNotepad = (WindowPattern)aeNotepad.GetCurrentPattern(WindowPattern.Pattern);
wpNotepad.Close();

// #8 Find The Save, Don't Save, Cancel Dailog
//
AutomationElement aeSaveDailog = findElement(aeNotepad, "Notepad", 10);
if (null == aeSaveDailog)
{
throw new Exception("Notepad Save dailog was not found!");
}

// #9 Find the "Don't Save" button and Click it
//
AutomationElement aeDontSaveBtn = findElement(aeSaveDailog, "Don't Save", 10);
if (null == aeDontSaveBtn)
{
throw new Exception("Notepad Don't Save Button was not found!");
}
InvokePattern ipDontSaveBtn = (InvokePattern)aeDontSaveBtn.GetCurrentPattern(InvokePattern.Pattern);
ipDontSaveBtn.Invoke();

// #10 Done
//
Console.ReadLine();
}

// Returns a Automation Element based on the parent AutomationElement, and caption.
//
private static AutomationElement findElement(AutomationElement parent, string caption, int timeout)
{
AutomationElement aeNew = null;
int numWaits = 0;
do
{
aeNew = parent.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, caption)
);
++numWaits;
Thread.Sleep(100);
} while (aeNew == null && numWaits < timeout);
return (aeNew);
}
}
}

Wednesday, April 02, 2008

iPhone full Arabic support is here



Requirements:
1- Firmware version 1.1.3/1.1.4
2- Arabic Keyboard setup following instructions provided by Tariq @ http://www.iphoneislam.com/
Setup instructions:
1- Add the following Source in your Installer. This can be done by going to the Sources Tab, and selecting the Edit button. Then, type the following URL in the source field: http://repo.aakqtr.com/
2- After the sources are refreshed, navigate to BiOsS Modz category in the Install Tab, and select: iPhone Arabizer 1.0 and choose Install.
3- Reboot the Phone
4- At this point Safari can render the Arabic text to any Arabic website, Email, Contacts, and Calander also support Arabic text at this point
5- Go to Settings > General > International > Language: and choose Arabic (this will change your UI Language)
6- Go to Settings > General > International > Region Format and Select Arabic and the Country.
Now you have Full Arabic support in the iPhone.