Coded UI Extension(Extension Excel example)
1 CodedUIExtensibilitySample Applicationinternal class ActionFilter : UITestActionFilter
{
// Returns a value that indicates whether the provided mouse action is a left-click action
private static bool IsLeftClick(MouseAction mouseAction)
{
return mouseAction != null &&
mouseAction.ActionType == MouseActionType.Click &&
mouseAction.MouseButton == MouseButtons.Left &&
mouseAction.ModifierKeys == System.Windows.Input.ModifierKeys.None;
}
// Returns a value that indicates whether the two provided actions are applied to the same cell.
private static bool AreActionsOnSameExcelCell(SendKeysAction lastAction, MouseAction secondLastAction)
{
return lastAction != null && secondLastAction != null &&
lastAction.UIElement is CellElement &&
secondLastAction.UIElement is CellElement &&
object.Equals(lastAction.UIElement, secondLastAction.UIElement);
}// Returns a value that indicates whether the provided mouse action is a left-click action
private static bool IsLeftClick(MouseAction mouseAction)
{
return mouseAction != null &&
mouseAction.ActionType == MouseActionType.Click &&
mouseAction.MouseButton == MouseButtons.Left &&
mouseAction.ModifierKeys == System.Windows.Input.ModifierKeys.None;
}
// Returns a value that indicates whether the two provided actions are applied to the same cell.
private static bool AreActionsOnSameExcelCell(SendKeysAction lastAction, MouseAction secondLastAction)
{
return lastAction != null && secondLastAction != null &&
lastAction.UIElement is CellElement &&
secondLastAction.UIElement is CellElement &&
object.Equals(lastAction.UIElement, secondLastAction.UIElement);
}
}
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Microsoft.VisualStudio.Test.Sample.UI.Excel.Communication;
/// <remarks>Must be visible to COM, which is enabled with the ComVisibleAttribute.</remarks>
[ComVisible(true)]
public class Element : UITechnologyElement
{
internal virtual System.Collections.IEnumerator GetChildren(AndCondition condition)
{
string sheetName = condition.GetPropertyValue(PropertyNames.Name) as string;
if (!string.IsNullOrEmpty(sheetName))
{
// Get the worksheet element
UITechnologyElement sheetElement = this.technologyManager.GetExcelElement(
this.WindowHandle,
new WorksheetInformation(sheetName));
// Get and return the enumerator for the array of child elements of the window element
return new UITechnologyElement[] { sheetElement }.GetEnumerator();
}
return null;
}
}
internal class PropertyProvider : UITestPropertyProvider
{
public override object GetPropertyValue(UITestControl uiTestControl, string propertyName)
{
// Simply delegate the call to Excel add-in.
CellInformation cellInfo = GetCellInfo(uiTestControl);
return Communicator.Instance.GetCellProperty(cellInfo, propertyName);
}
[ComVisible(true)]
public sealed class TechnologyManager : UITechnologyManager
{
public override IUITechnologyElement GetElementFromPoint(int pointX, int pointY)
{
// Get the window at the specified location.
IntPtr windowHandle = Utilities.WindowFromPoint(pointX, pointY);
// Verify that the window is an Excel window
Debug.Assert(ExcelUtilities.IsExcelWorksheetWindow(windowHandle));
// Get the Excel UI element at the specified location.
return GetExcelElement(windowHandle, Communicator.Instance.GetElementFromPoint(pointX, pointY));
}
handle.</returns>
public override IUITechnologyElement GetElementFromWindowHandle(IntPtr windowHandle)
{
// Verify that the specified window is an Excel worksheet.
Debug.Assert(ExcelUtilities.IsExcelWorksheetWindow(windowHandle));
// Get the Excel worksheet specified by the window handle.
return GetExcelElement(windowHandle, null);
}
ExcelCodedUIAddinHelper Application
internal static class Communicator
{
/// <summary>
/// Singleton interface used to communicate with Excel through .NET Remoting.
/// </summary>
internal static IExcelUICommunication Instance
{
get
{
if (excelCommunicator == null)
{
// Open a channel of communication with Microsoft Excel
excelCommunicator =
(IExcelUICommunication)Activator.GetObject(
typeof(IExcelUICommunication),
string.Concat("ipc://",
PropertyNames.IPC1, "/",
PropertyNames.IPC2));
}
return excelCommunicator;
}
}
private static IExcelUICommunication excelCommunicator;
/// <summary>
/// Implementation of IExcelUITestCommunication which provides information
/// to the ExcelExtension (loaded in the Coded UI Test process) from the
/// ExcelAddin (loaded in the Excel process) via .NET Remoting.
/// </summary>
internal class UICommunicator : MarshalByRefObject, IExcelUICommunication
{
/// <summary>
/// Default constructor.
/// </summary>
public UICommunicator()
{
if (ThisAddIn.Instance == null || ThisAddIn.Instance.Application == null)
{
throw new InvalidOperationException();
}
// Cache the Excel application of this addin.
this.application = ThisAddIn.Instance.Application;
}
#region IExcelUITestCommunication Implementation
/// <summary>
/// Gets an Excel UI element at the given screen location.
/// </summary>
/// <param name="x">The x-coordinate of the location.</param>
/// <param name="y">The y-coordinate of the location.</param>
/// <returns>The Excel UI element info.</returns>
public ElementInformation GetElementFromPoint(int x, int y)
{
// Use Excel's Object Model to get the required.
Worksheet ws = this.application.ActiveSheet as Worksheet;
if (ws != null && this.application.ActiveWindow != null)
{
Range cellAtPoint = this.application.ActiveWindow.RangeFromPoint(x, y) as Range;
if (cellAtPoint != null)
{
return new CellInformation(cellAtPoint.Row, cellAtPoint.Column, new WorksheetInformation(ws.Name));
}
else
{
return new WorksheetInformation(ws.Name);
}
}
return null;
}
Addin
/// <summary>
/// Singleton instance to this add-in.
/// </summary>
internal static ThisAddIn Instance
{
get;
private set;
}
/// <summary>
/// Register for .NET Remoting on startup of this add-in.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The event arguments.</param>
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Instance = this;
channel = new IpcChannel(PropertyNames.IPC1);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(UICommunicator),
PropertyNames.IPC2, WellKnownObjectMode.Singleton);
}
/// <summary>
/// Unregister for .NET Remoting on shutdown of this add-in.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The event arguments.</param>
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
if (channel != null)
{
ChannelServices.UnregisterChannel(channel);
}
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
/// <summary>
/// The channel for .NET Remoting calls.
/// </summary>
private IChannel channel;
private Excel._Application application; Coded UI extension --------------------编程问答-------------------- 头皮发麻 --------------------编程问答-------------------- UICommunicator class , 操作excel
/// <summary>
/// Sets focus on a given cell.
/// </summary>
/// <param name="cellInfo">The cell info.</param>
public void SetFocus(CellInformation cellInfo)
{
// Use Excel's Object Model to get the required.
Worksheet ws = GetWorksheet(cellInfo.Parent);
if (ws != null)
{
// There could be some other cell under editing. Exit that mode.
ExitPreviousEditing(ws);
ws.Activate();
Range cell = GetCell(cellInfo);
if (cell != null)
{
cell.Activate();
}
}
}
CellElement class
/// <summary>
/// Sets the focus on this element.
/// </summary>
public override void SetFocus()
{
// Use Excel to set the focus and activate the cell.
Communicator.Instance.SetFocus(this.CellInfo);
}
// Sample Extension to test Microsoft Excel worksheets
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UITest.Common;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Microsoft.VisualStudio.TestTools.UITesting;
// Attribute that identifies this assembly as containing UITest extensions.
[assembly: UITestExtensionPackage("Microsoft.VisualStudio.Test.Sample.UI.Excel",
typeof(Microsoft.VisualStudio.Test.Sample.UI.Excel.ExtensionPackage))]
namespace Microsoft.VisualStudio.Test.Sample.UI.Excel
{
/// <summary>
/// Entry class for the Excel extension package.
/// </summary>
internal class ExtensionPackage : UITestExtensionPackage
{
/// <summary>
/// Gets the service object of the specified type.
/// </summary>
/// <param name="serviceType">An object that specifies the type of service object to get.</param>
/// <returns>
/// A service object of type serviceType or null if there is no service object of the specified type.
/// </returns>
public override object GetService(Type serviceType)
{
// Return the appropriate service
if (serviceType == typeof(UITechnologyManager))
{
return technologyManager;
}
else if (serviceType == typeof(UITestPropertyProvider))
{
return propertyProvider;
}
else if (serviceType == typeof(UITestActionFilter))
{
return actionFilter;
}
return null;
}
/// <summary>
/// Performs application-defined tasks of cleaning up resources.
/// </summary>
public override void Dispose()
{
}
/// <summary>
/// Gets the short description of the package.
/// </summary>
public override string PackageDescription
{
get { return "Sample Excel Extension for Coded UI Test"; }
}
/// <summary>
/// Gets the name of the package.
/// </summary>
public override string PackageName
{
get { return "Coded UI Extension Sample Extension"; }
}
/// <summary>
/// Gets the name of the vendor of the package.
/// </summary>
public override string PackageVendor
{
get { return "Sample by Microsoft Corporation"; }
}
/// <summary>
/// Gets the version of the package.
/// </summary>
public override Version PackageVersion
{
get { return new Version(1, 0); }
}
/// <summary>
/// Gets the version of Visual Studio supported by this package.
/// </summary>
public override Version VSVersion
{
get { return new Version(10, 0); }
}
// Create and cache the services.
private TechnologyManager technologyManager = new TechnologyManager();
private PropertyProvider propertyProvider = new PropertyProvider();
private ActionFilter actionFilter = new ActionFilter();
}
}
补充:.NET技术 , .NET Framework