728x90
반응형
728x170
[TestController 프로젝트]
▶ MainWindow.xaml
<Window x:Class="TestController.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="600"
Title="UI 자동화 사용하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Name="startButton" Grid.Row="0"
Width="150"
Height="30"
Content="자동화 시작" />
<ListBox Name="listBox" Grid.Row="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
</Grid>
</Window>
728x90
▶ MainWindow.xaml.cs
using System.Threading;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Threading;
namespace TestController
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
private delegate void SetMessageCallback(string message);
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
this.startButton.Click += startButton_Click;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 자동화 시작 버튼 클릭시 처리하기 - startButton_Click(sender, e)
/// <summary>
/// 자동화 시작 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void startButton_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(new ThreadStart(ProcessAutomation));
thread.Start();
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 로그 쓰기 (코어) - WriteLogCore(message)
/// <summary>
/// 로그 쓰기 (코어)
/// </summary>
/// <param name="message">메시지</param>
private void WriteLogCore(string message)
{
this.listBox.Items.Add(message);
}
#endregion
#region 로그 쓰기 - WriteLog(message)
/// <summary>
/// 로그 쓰기
/// </summary>
/// <param name="message">메시지</param>
private void WriteLog(string message)
{
Dispatcher.BeginInvoke
(
DispatcherPriority.Normal,
new SetMessageCallback(WriteLogCore),
message
);
}
#endregion
#region 에러 로그 쓰기 - WriteErrorLog()
/// <summary>
/// 에러 로그 쓰기
/// </summary>
private void WriteErrorLog()
{
WriteLog("ERROR");
}
#endregion
#region 엘리먼트 구하기 - GetElement(parentElement, automationID)
/// <summary>
/// 엘리먼트 구하기
/// </summary>
/// <param name="parentElement">부모 엘리먼트</param>
/// <param name="automationID">자동화 ID</param>
/// <returns>자동화 엘리먼트</returns>
private AutomationElement GetElement(AutomationElement parentElement, string automationID)
{
System.Windows.Automation.Condition condition = new PropertyCondition(AutomationElement.AutomationIdProperty, automationID);
AutomationElement element = parentElement.FindFirst(TreeScope.Descendants, condition);
return element;
}
#endregion
#region 자동화 처리하기 - ProcessAutomation()
/// <summary>
/// 자동화 처리하기
/// </summary>
private void ProcessAutomation()
{
WriteLog("루트 요소 획득...");
AutomationElement rootElement = AutomationElement.RootElement;
if(rootElement != null)
{
System.Windows.Automation.Condition condition = new PropertyCondition
(
AutomationElement.NameProperty,
"UI 자동화 테스트 타겟"
);
WriteLog("UI 자동화 테스트 타겟 프로그램 탐색...");
AutomationElement applicationElement = rootElement.FindFirst(TreeScope.Children, condition);
if(applicationElement != null)
{
WriteLog("A 텍스트 박스 탐색...");
AutomationElement aTextBoxElement = GetElement(applicationElement, "aTextBox");
if(aTextBoxElement != null)
{
WriteLog("A 텍스트 박스 값 설정...");
try
{
ValuePattern valuePattern = aTextBoxElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
valuePattern.SetValue("10");
}
catch
{
WriteErrorLog();
}
}
else
{
WriteErrorLog();
}
WriteLog("B 텍스트 박스 탐색...");
AutomationElement bTextBoxElement = GetElement(applicationElement, "bTextBox");
if(bTextBoxElement != null)
{
WriteLog("B 텍스트 박스 값 설정...");
try
{
ValuePattern valuePattern = bTextBoxElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
valuePattern.SetValue("5");
}
catch
{
WriteErrorLog();
}
}
else
{
WriteErrorLog();
}
}
else
{
WriteErrorLog();
}
}
}
#endregion
}
}
300x250
[TestTarget 프로젝트]
▶ MainWindow.xaml
<Window x:Class="TestTarget.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="600"
Title="UI 자동화 테스트 타겟"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Row="1" Grid.Column="1"
Width="100"
Height="25"
Content="Value A" />
<Label Grid.Row="3" Grid.Column="1"
Width="100"
Height="25"
Content="Value B" />
<TextBox Name="aTextBox" Grid.Row="1" Grid.Column="3"
Width="300"
Height="25" />
<TextBox Name="bTextBox" Grid.Row="3" Grid.Column="3"
Width="300"
Height="25" />
</Grid>
</Window>
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] 애니메이션 메시지 컨트롤 사용하기 (0) | 2021.02.11 |
---|---|
[C#/WPF] 3D 애니메이션 시계 사용하기 (0) | 2021.02.11 |
[C#/WPF] SVG 파일을 XAML로 변환하기 (0) | 2021.02.10 |
[C#/WPF] 이미지 물방울 애니메이션(Drip Animation) 사용하기 (0) | 2021.02.09 |
[C#/WPF] 이미지 롤 애니메이션(Roll Animation) 사용하기 (0) | 2021.02.08 |
[C#/WPF] 모니터 추가/제거시 이벤트 처리하기 (0) | 2021.02.08 |
[C#/WPF] Shape 클래스 : 커스텀 호(Arc) 만들기 (0) | 2021.02.07 |
[C#/WPF] DrawingContext 클래스 : DrawGlyphRun 메소드를 사용해 텍스트 그리기 (0) | 2021.02.07 |
[C#/WPF] Shape 클래스 : DefiningGeometry 속성을 사용해 커스텀 도형 만들기 (0) | 2021.02.07 |
[C#/WPF] TextBlock 엘리먼트 : Text 속성에서 개행 문자 사용하기 (0) | 2021.02.07 |
댓글을 달아 주세요