C#web自动化测试问题

yctc_yanwei 2012-01-03 11:45:29
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();

}





如题,上一段代码为打开百度页面输入天津,点击查询按钮。我自己做了一个登录的页面地址为http://localhost/TestWebsite/login.aspx。就在上面程序中修改了 ie.Navigate("www.baidu.com", ref o, ref o, ref o, ref o);中的网址,就出现错误,提示HTMLDocument接口异常。
...全文
304 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
夏布旅人 2013-10-24
  • 打赏
  • 举报
回复
能不能把源代码发给我呀,180332123@qq.com
yctc_yanwei 2012-01-07
  • 打赏
  • 举报
回复
问题已解决,谢谢!
yctc_yanwei 2012-01-05
  • 打赏
  • 举报
回复
在线等答案,不能沉啊。。。。
yctc_yanwei 2012-01-05
  • 打赏
  • 举报
回复
是的,悲剧的毕业设计额。。。老师让我做个自动化测试的东西,我也买了点书(感觉有点过时),然后就总是出错了。
Activer 2012-01-04
  • 打赏
  • 举报
回复
另外问一句; 这样搞测试累吗? 好像不是搞测试用的吧?!
Activer 2012-01-04
  • 打赏
  • 举报
回复
试一试这个:

object o = String.Empty;
ie.Navigate("www.baidu.com", ref o, ref o, ref o, ref o);

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧