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 lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint="FindWindowEx", CharSet=CharSet.Auto)] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("user32.dll", EntryPoint="PostMessage", CharSet=CharSet.Auto)] static extern bool PostMessage1(IntPtr hWnd, uint Msg, int wParam, int 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(20, 430); button1.Size = new Size(90, 23); button1.Text = "Load and Test"; button1.Click += new EventHandler(this.button1_Click); // listBox1 listBox1 = new ListBox(); listBox1.Location = new Point(10, 460); listBox1.Size = new Size(460, 200); // wb wb = new WebBrowser(); wb.Location = new Point(10,10); wb.Size = new Size(460, 400); wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ExerciseApp); // Form1 this.Text = "Lightweight Web Application WinForm Test Harness"; this.Size = new Size(500, 710); this.Controls.Add(wb); this.Controls.Add(button1); this.Controls.Add(listBox1); } // Form1() private void button1_Click(object sender, EventArgs 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 sender, EventArgs 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(null, caption); 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.Zero, null, "OK"); ClickOn(hOkBtn ); } } private void ClickOn(IntPtr hControl) { uint WM_LBUTTONDOWN = 0x0201; uint WM_LBUTTONUP = 0x0202; PostMessage1(hControl, WM_LBUTTONDOWN, 0, 0); PostMessage1(hControl, WM_LBUTTONUP, 0, 0); }
댓글
댓글 쓰기