C#web自动化测试问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using SHDocVw;
using mshtml;
namespace TestProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Launching IE ...");
Process p = Process.Start("iexplore.exe", "about:blank");
Thread.Sleep(3000);
Console.WriteLine("Attaching to IE ...");
InternetExplorer ie = null;
if (p != null)
{
SHDocVw.ShellWindows allBrowser = new ShellWindows();
if (allBrowser.Count != 0)
{
for (int i = 0; i < allBrowser.Count; i++)
{
InternetExplorer e = (InternetExplorer)allBrowser.Item(i);
if (e.HWND == (int)p.MainWindowHandle)
{
ie = e;
break;
}
}
}
}
else
{
Console.WriteLine("Error: Can not Launch IE");
return;
}
if (ie == null)
{
Console.WriteLine("Error: Can not attach to IE");
return;
}
Console.WriteLine("Navigating ...");
object o = null;
ie.Navigate("www.baidu.com", ref o, ref o, ref o, ref o);
Thread.Sleep(5000);
//得到一个Text Box
Console.WriteLine("Inputing Keyword ...");
HTMLDocument doc = (HTMLDocument)ie.Document;
HTMLInputElement keyword = (HTMLInputElement)doc.getElementById("kw");
keyword.value = "天津";
Thread.Sleep(5000);
//验证
if (keyword.getAttribute("size", 0).ToString().Equals("36"))
Console.WriteLine("Validation Passed! Size is Correct");
else
Console.WriteLine("Validation Failed! Size is wrong");
//得到一个按钮
Console.WriteLine("Clicking Submit ...");
HTMLInputElement submit = (HTMLInputElement)doc.getElementById("su");
submit.click();
Thread.Sleep(4000);
//验证
if (string.Equals(doc.title.Trim(), "百度搜索_天津"))
{
Console.WriteLine("Validation Passed! Title is Corrected");
//Console.ReadLine;
}
else
Console.WriteLine("Validation Failed! Title is wrong");
if (doc.body.innerText.Contains("天津地图"))
{
Console.WriteLine("Validation Passed! Body contains your string");
Console.ReadLine();
}
else
Console.WriteLine("Validation Failed! Body do not contain");
////得到一个链接
//Console.WriteLine("Clicking Login Button ...");
//IHTMLElement userPanel = doc.getElementById("u");
//IHTMLElementCollection HyperLinks = ((IHTMLElement2)userPanel).getElementsByTagName("a");
//IHTMLElement login = (IHTMLElement)HyperLinks.item(null, 0);
//login.click();
}