Required Word 2007 UI Automation code (.NET code)

  • Thread starter Nasir Khan - VSTS Team
  • Start date
N

Nasir Khan - VSTS Team

Using UI Automation code I want to drive the Word 2007 UI.

Let me know if any sample available for Word 2007.

Scenario is like UI Testing for Word 2007. I am trying to implement an
application which will generate the script when user/tester open Word 2007.
I want the generated script to be executed by Executor to Drive the Word
2007 UI.

How will I achieve this?

I have gone through some link for UI Automation, but they are helpful only
for Windows Application, WPF.
links:

http://blogs.msdn.com/abhinaba/archive/2006/06/27/648221.aspx?CommentPosted=true#commentmessage
http://msdn2.microsoft.com/en-us/library/ms750582.aspx#Mtps_DropDownFilterText

Waiting for your valuable response.

Cheers!
Nasir
 
N

Nasir Khan - VSTS Team

Scenario:
I am Trying to drive some custom controls on Word 2007 UI.
To achieve this I am using UI Automation Framework provided by .NET.
Namespaces used:
UIAutomationClient.dll
UIAutomationClientsideProviders.dll
UIAutomationProvider.dll
UIAutomationTypes.dll
WindowsBase.dll

I am using following steps to Drive Word UI:
Step 1: Invoking the Word 2007 document and getting the Target Word 2007 as
Automation element.
Step 2: Using AutomationElementCollection object to get all Controls
available on Target window(Word 2007 Document).
Step 3 : Reading each Automation element in collection. Getting the Control
Type, Control UI Pattern.
Step 4: Browsing(Finding) through all child control using TreeScope.
Step 6: Based on the Pattern of control, Invoking the control events(Also
Registering the events for Control Type like Button, TabItem, MenuItem etc),
Setting/getting the values for controls like Edit, Text Pattern, Selection
Items.
Step 7: I am driving the word by making a call to ExecuteAutmationScript()
Method(My code).
Here is the trouble I have one TabItem on Word 2007 document called “Add
Insâ€, Inside these tab item there are some controls(child elements) like
inside the “Review†tab in Word 2007 we have controls like “Spelling &
Grammar†, “Research†buttons etc.
I am trying to get these child elements using AutomationElementCollection or
TreeWalker, but UI Automation doesn’t provide me these child elements. You
can go through the code Labeled - Refer Line no. 276.

Also I have InfoPath form in Word document, I am able to get the values all
elements available on this From, but automation code is not allowing me to
set the values for “ControlType.Edit†or “ControlType.Text†type of control.

Code for your reference(Label - Refer Line no. 276):
(Sample Code)
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Automation;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Text.RegularExpressions;
// Code - Nasir Khan

namespace Script_Executor
{
class UIScriptDriver
{

// The Word UI Root Element
//private AutomationElement rootElement;

// Word UI Contols collection.
public static AutomationElementCollection elementsCollection;

// UI Script Execution Feedback

static StringBuilder fdString;


//Search for the automation elements collection i.e. occurance of a
given type of controls from root
private static AutomationElementCollection
GetElements(AutomationElement root, ControlType controlType)
{
// Set up the conditions for finding the text control.
PropertyCondition documentControl = new PropertyCondition(
AutomationElement.ControlTypeProperty,
controlType);
//PropertyCondition textPatternAvailable = new PropertyCondition(
// AutomationElement.ControlTypeProperty, true);
//AndCondition findControl =
// new AndCondition(documentControl, textPatternAvailable);

// Get the Automation Element for the first text control found.
// For the purposes of this sample it is sufficient to find the
// first text control. In other cases there may be multiple text
// controls to sort through.
return root.FindAll(TreeScope.Descendants,
Automation.RawViewCondition);

//PropertyCondition condType = new
PropertyCondition(AutomationElement.ControlTypeProperty, controlType);
//return root.FindFirst(TreeScope.Descendants, condType);
}

// Method used to get al AutomationElements collection, get the patterns of
the controls and based on the pattern Invoke the pattern, get the pattern
data.
public static string ExecuteAutmationScript(AutomationElement
rootElement)
{
// Temp. code to get the child controls of word doc. Title Pane
and change the value

elementsCollection = GetElements(rootElement, ControlType.Pane);
fdString = new StringBuilder();
//InvokePattern invokePattern =
// targetApp.GetCurrentPattern(InvokePattern.Pattern)
// as InvokePattern;
//invokePattern.Invoke(); // Invode the button event

foreach (AutomationElement element in elementsCollection)
{
try
{
// If we get the Invoke pattern for the child elements,
then Invoke the button event
switch (element.Current.ControlType.ProgrammaticName)
{
case "ControlType.Button":
{
try
{
if (element.Current.Name == "Office
Button")
{
fdString.Append("\n Invoke Button
Event :" + element.Current.ControlType.ProgrammaticName);
InvokePattern invokePattern =
(InvokePattern)

element.GetCurrentPattern(InvokePattern.Pattern);
// as InvokePattern;
invokePattern.Invoke(); // Invode
the button event
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.Message);
}

}
break;
case "ControlType.Edit":
{
try
{
object valueTextPattern = null;
// Get/Set the value of Propety
//AutomationElement childElement =
GetElement(element, ControlType.Edit);
string result =
element.GetCurrentPropertyValue(ValuePattern.ValueProperty, false).ToString();
fdString.Append("\n Property for Control
Type :" + element.Current.ControlType.ProgrammaticName);
fdString.Append("\n Control Name :" +
element.Current.Name);
fdString.Append("\n Old Property value
:" + result);
if
(element.TryGetCurrentPattern(ValuePattern.Pattern, out valueTextPattern))
{

((ValuePattern)valueTextPattern).SetValue("Dominic");
result =
element.GetCurrentPropertyValue(ValuePattern.ValueProperty, false).ToString();
fdString.Append("\n New Property
value :" + result);
}
}
// Exception needs to be handel later
catch (Exception Ex)
{
Console.WriteLine(Ex.Message);
}
}
break;
case "ControlType.Pane":
{
try
{
object valuePattern = null;
object valuePattern1 = null;
AutomationElement childElement =
GetElement(element, "Font:", true);
// Font size
AutomationElement childElement1 =
GetElement(element, "Font Size:", true);

if (childElement != null)
{
string result =
childElement.GetCurrentPropertyValue(ValuePattern.ValueProperty,
false).ToString();
fdString.Append("\n Property for
Control (Font Name) :" + childElement.Current.ControlType.ProgrammaticName);
fdString.Append("\n Property value
:" + result);
fdString.Append("\n Property for
Control (Font Size) :" + childElement1.Current.ControlType.ProgrammaticName);
fdString.Append("\n Property value
:" + childElement1.GetCurrentPropertyValue(ValuePattern.ValueProperty,
false).ToString());

if
(!childElement.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern))
{
fdString.Append("\n The control
with an AutomationID of ")

..Append(element.Current.AutomationId.ToString())
.Append(" does not support
ValuePattern.")
.AppendLine(" Using keyboard
input.\n");

// Set focus for input
functionality and begin.
childElement.SetFocus();

// Pause before sending keyboard
input.
Thread.Sleep(10000);

// Delete existing content in
the control and insert new content.
SendKeys.SendWait("^{HOME}");
// Move to start of control
SendKeys.SendWait("^+{END}");
// Select everything
SendKeys.SendWait("{DEL}");
// Delete selection
SendKeys.SendWait("Times New
Roman");
}
// Control supports the ValuePattern
pattern so we can
// use the SetValue method to insert
content.
else
{
fdString.Append(" \n The control
with an AutomationID of ")

..Append(element.Current.AutomationId.ToString())
.Append((" supports
ValuePattern."))
.AppendLine(" Using
ValuePattern.SetValue().\n");

// Set focus for input
functionality and begin.
//element.SetFocus();


((ValuePattern)valuePattern).SetValue("Times New Roman");
fdString.Append("\n Property for
Control (Font Name) :" + childElement.Current.ControlType.ProgrammaticName);
fdString.Append("\n Property
value Changed to :" + ((ValuePattern)valuePattern).Current.Value);
}
//SelectionItemPattern textPattern =
childElement.GetCurrentPropertyValue((SelectionItemPattern.Pattern, false) as
SelectionItemPattern;
// Font size
if
(!childElement1.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern1))
{

fdString.Append("\n The control
with an AutomationID of ")

..Append(element.Current.AutomationId.ToString())
.Append(" does not support
ValuePattern.")
.AppendLine(" Using keyboard
input.\n");
}
else
{

((ValuePattern)valuePattern1).SetValue("30");
}
}
}
catch (ArgumentNullException exc)
{
fdString.Append("\n" + exc.Message);
}
catch (InvalidOperationException exc)
{
fdString.Append("\n" + exc.Message);
}
finally
{

//fdString.Append(feedbackText.ToString());
}
break;
}

// If detected control is Custom control then
case "ControlType.Custom":
{
if (element.Current.Name == "Office Button")
{

fdString.Append("\n Invoke Button Event
:" + element.Current.ControlType.ProgrammaticName);
InvokePattern invokePattern =
(InvokePattern)

element.GetCurrentPattern(InvokePattern.Pattern);
// as InvokePattern;
invokePattern.Invoke(); // Invode the
button event
}
break;
}
// Here is the code which is giving me trouble
// Refer Line no. 276
case "ControlType.TabItem":
{
try
{
if (element.Current.Name == "Add Ins")
{
fdString.Append("\n Invoked Author
Tab :" + element.Current.ControlType.ProgrammaticName);
InvokePattern invokePattern =
(InvokePattern)

element.GetCurrentPattern(InvokePattern.Pattern);
fdString.Append("\n Author Tab
Opened " + element.Current.ControlType.ProgrammaticName);
// as InvokePattern;
invokePattern.Invoke(); // Invode
the button event
// Get all Items on Authoring Ribbon
AutomationElementCollection
elementchilds = GetElements(element, ControlType.Custom);
foreach (AutomationElement
elementChild in elementsCollection)
{
try
{
fdString.Append("\n Invoke
Authoring Event for :" + elementChild.Current.ControlType.ProgrammaticName);
InvokePattern
invokechildPattern = (InvokePattern)

elementChild.GetCurrentPattern(InvokePattern.Pattern);
fdString.Append("\n
Authoring Tab elemnt " + elementChild.Current.ControlType.ProgrammaticName);
// as InvokePattern;
invokechildPattern.Invoke();
// Invode the button event
}
catch (Exception ex)
{
fdString.Append("\n
Authoring Tab element: " + elementChild.Current.Name);
fdString.Append("\nError in
elements of Authoring Tab: " + ex.Message);

}
}
//AutomationElement elementChild =
GetElement(element, "Metadata Panel", true);
}
}
catch { }
break;
}
default:
{
// codee will be added later
break;
}
}

}
catch (Exception ex)
{
fdString.Append("Error in loading Automation element: "
+ ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
return fdString.ToString();
}
}
}


Thanks and Regards,
Nasir khan
 
C

Cindy M.

Hi =?Utf-8?B?TmFzaXIgS2hhbiAtIFZTVFMgVGVhbQ==?=,
Using UI Automation code I want to drive the Word 2007 UI.

Let me know if any sample available for Word 2007.

Scenario is like UI Testing for Word 2007. I am trying to implement an
application which will generate the script when user/tester open Word 2007.
I want the generated script to be executed by Executor to Drive the Word
2007 UI.

How will I achieve this?

I have gone through some link for UI Automation, but they are helpful only
for Windows Application, WPF.
I don't think this namespace applies to Office applications. The Office
Ribbon is not the same as a .NET Framework Ribbon. The Office Ribbon supports
no object model and cannot be accessed at runtime programmatically.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top