첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestSolution.zip
0.02MB

[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
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요