乐乐

乐乐的博客园
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

通过反射设置窗口属性 (自动化测试UI)

Posted on 2008-03-19 18:00  带你去月球  阅读(299)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;

namespace ConsoleApplication1
{
    
/// <summary>
    
/// Program class
    
/// </summary>

    class Program
    
{
        
/// <summary>
        
/// Set form property value handler delegate
        
/// </summary>
        
/// <param name="f"></param>
        
/// <param name="propertyName"></param>
        
/// <param name="newValue"></param>

        delegate void SetFormPropertyValueHandler(Form f, string propertyName, object newValue,int delay);

        
/// <summary>
        
/// Inform the waitting process, event has occur.
        
/// </summary>

        static AutoResetEvent are = new AutoResetEvent(false);
        [STAThread]
        
static void Main(string[] args)
        
{
            
            Console.WriteLine(
"Launching Form");
            Form autForm 
= null;
            
string formName = "AUTForm.Form1";
            
string path = @"E:\___Test\AUT\AUTForm\AUTForm\bin\Debug\AUTForm.exe";

            autForm 
= LaunchApp(path, formName);

            System.Drawing.Point pt 
= new System.Drawing.Point(500500);      
            SetFormPropertyValue(autForm, 
"Location", pt,1500);

            
//while( true)
            
//{
            
//    
            
//}
       }


        
static void SetFormPropertyValue(Form f, string propertyName, object newValue,int delay)
        
{
            Thread.Sleep(delay);

            
if (f.InvokeRequired)
            
{
                Console.WriteLine(
"Invoke Required!");
                Delegate d 
= new SetFormPropertyValueHandler(SetFormPropertyValue);
                
object[] o = new object[] { f, "Location", newValue ,delay};
                f.Invoke(d, o);
                
return;               
            }

            
else
            
{
                Console.WriteLine(
"Set value");
                Type t 
= f.GetType();
                PropertyInfo pi 
= t.GetProperty(propertyName);
                pi.SetValue(f, newValue, 
null);
            }

        }


        
static Form LaunchApp(string path, string formName)
        
{
            Form result 
= null;
            Assembly a 
= Assembly.LoadFrom(path);
            Type t1 
= a.GetType(formName);
            result 
= (Form)a.CreateInstance(t1.FullName);

            AppState aps 
= new AppState(result);
            ThreadStart ts 
= new ThreadStart(aps.RunApp);
            Thread thread 
= new Thread(ts);
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground 
= true;
            thread.Start();
            
return result;
        }
    

        
private class AppState
        
{
            
public readonly Form formToRun;

            
public AppState(Form f)
            
{
                
this.formToRun = f;
            }


            
public void RunApp()
            
{
                Application.Run(formToRun);
            }

        }

    }
    
}

无觅相关文章插件,快速提升流量