IE automation. dialog watcher

http://msdn.microsoft.com/en-us/magazine/gg309183.aspx

public class Form1 : Form
  {
    [DllImport("user32.dll"EntryPoint="FindWindow"CharSet=CharSet.Auto)]
    static extern IntPtr FindWindow(string lpClassNamestring lpWindowName);
 
    [DllImport("user32.dll"EntryPoint="FindWindowEx"CharSet=CharSet.Auto)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfterstring lpszClassstring lpszWindow);
 
    [DllImport("user32.dll"EntryPoint="PostMessage"CharSet=CharSet.Auto)]
    static extern bool PostMessage1(IntPtr hWnduint Msgint wParamint lParam); 
 
    private System.Windows.Forms.WebBrowser wb = null;
    private Button button1 = null;
    private ListBox listBox1 = null;
 
    public Form1()
    {
      // button1
      button1 = new Button();
      button1.Location = new Point(20430);
      button1.Size = new Size(9023);
      button1.Text = "Load and Test";
      button1.Click += new EventHandler(this.button1_Click);
 
      // listBox1
      listBox1 = new ListBox();
      listBox1.Location = new Point(10460);
      listBox1.Size = new Size(460200);
 
      // wb
      wb = new WebBrowser();
      wb.Location = new Point(10,10);
      wb.Size = new Size(460400);
      wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ExerciseApp);
     
      // Form1
      this.Text = "Lightweight Web Application WinForm Test Harness";
      this.Size = new Size(500710);
      this.Controls.Add(wb);
      this.Controls.Add(button1);
      this.Controls.Add(listBox1);
    } // Form1()
 
    private void button1_Click(object senderEventArgs e// run test automation
    {
      listBox1.Items.Add("Loading Web app under test into WebBrowser control");
      wb.Url = new Uri("http://localhost/ColorApp/default.html");
    }
 
    private void ExerciseApp(object senderEventArgs e)
    {
      Thread thread = new Thread(new ThreadStart(WatchForAndClickAwayMessageBox));
      thread.Start();
 
      listBox1.Items.Add("Clicking on 'Click Me' button");
      HtmlElement btn1 = wb.Document.GetElementById("button1");
      btn1.InvokeMember("click");
 
      listBox1.Items.Add("Waiting to click away message box");
 
      listBox1.Items.Add("'Typing' roses into TextBox1");
      HtmlElement tb1 = wb.Document.GetElementById("TextBox1");
      tb1.InnerText = "roses";
 
      listBox1.Items.Add("Clicking on 'Click Me' button again");
      btn1 = wb.Document.GetElementById("button1");
      btn1.InvokeMember("click");
 
      //System.Threading.Thread.Sleep(1500); // ineffective!
      listBox1.Items.Add("Looking for 'red' in TextBox2");
      HtmlElement tb2 = wb.Document.GetElementById("TextBox2");
      string response = tb2.OuterHtml;
      if (response.IndexOf("red">= 0) {
        listBox1.Items.Add("Found 'red' in TextBox2");
        listBox1.Items.Add("Test scenario result: PASS");
      }
      else {
        listBox1.Items.Add("Did NOT find 'red' in TextBox2");
        listBox1.Items.Add("Test scenario result: **FAIL**");
      }
 
      
    }
 
    private void WatchForAndClickAwayMessageBox()
    {
      IntPtr hMessBox = IntPtr.Zero;
      bool mbFound = false;
      int attempts = 0;
      string caption = "Message from webpage";
      do {
        hMessBox = FindWindow(nullcaption);
        if (hMessBox == IntPtr.Zero) {
          listBox1.Items.Add("Watching for message box . . . ");
          System.Threading.Thread.Sleep(100);
          ++attempts;
        }
        else {
          listBox1.Items.Add("Message box has been found");
          mbFound = true;
        }
      } while (!mbFound && attempts < 250);
 
      if (!mbFound) {
        listBox1.Items.Add("Did not find message box");
        listBox1.Items.Add("Test scenario result: **FAIL**");
      }
      else { 
        IntPtr hOkBtn = FindWindowEx(hMessBox, IntPtr.Zeronull"OK");
        ClickOn(hOkBtn );
      }
 
    }
   
    private void ClickOn(IntPtr hControl)
    {
      uint WM_LBUTTONDOWN = 0x0201;
      uint WM_LBUTTONUP = 0x0202;
      PostMessage1(hControlWM_LBUTTONDOWN00);
      PostMessage1(hControlWM_LBUTTONUP00);
    }
 

댓글

이 블로그의 인기 게시물

Oracle NLS_DATE_FORMAT 변경

Stop console process using Ctrl+C.

Alternative to IValueConvert, QuickConverter