基于C#程序的AJAX 自动化测试

昨天发了一个求助帖,也无人理会,只好自己动手丰衣足食了。

 

结合WatiN源码,和James McCaffrey 博士的 AJAX 测试自动化(http://msdn.microsoft.com/zh-cn/magazine/cc163479.aspx)一文终于搞定了,代码如下:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
ExpandedBlockStart.gifContractedBlock.gif        
{
           
// 
            if (e.uRL.ToString().ToLower().IndexOf("网址.htm"> 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                IHTMLDocument2 doc 
= (IHTMLDocument2)this.axWebBrowser1.Document;
                
if (doc != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    mshtml.HTMLSelectElement sellanguage 
= (mshtml.HTMLSelectElement)doc.all.item("signup_language"null);
                    sel.value 
= "fr";
                    NameValueCollection collection 
= new NameValueCollection();
                    collection.Add(
"button""1");
                    FireEvent((DispHTMLBaseElement)sel, 
"onchange", collection);
                }

            }

            textBox1.Text 
= e.uRL.ToString();
        }
 


public static void FireEvent(DispHTMLBaseElement element, string eventName, NameValueCollection eventObjectProperties)
ExpandedBlockStart.gifContractedBlock.gif        
{
            StringBuilder scriptCode 
= new StringBuilder();
            
//1000为等待的时间
            scriptCode.Append("window.setTimeout('action()', 1000);");
            scriptCode.Append(
"function action(){");

            scriptCode.Append(
"var newEvt = document.createEventObject();");

            
for (int index = 0; index < eventObjectProperties.Count; index++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                scriptCode.Append(
"newEvt.");
                scriptCode.Append(eventObjectProperties.GetKey(index));
                scriptCode.Append(
" = ");
                scriptCode.Append(eventObjectProperties.GetValues(index)[
0]);
                scriptCode.Append(
";");
            }


            scriptCode.Append(
"document.getElementById('" + element.uniqueID + "').fireEvent('" + eventName + "', newEvt);");
            scriptCode.Append(
"}");

            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                IHTMLWindow2 window 
= ((IHTMLDocument2)element.document).parentWindow;
                RunScript(scriptCode.ToString(), 
"javascript", window);
            }

            
catch (Exception RunScriptException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
            }

        }


        
public static void RunScript(string scriptCode, string language, IHTMLWindow2 window)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                window.execScript(scriptCode, language);
            }

            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw ex;
            }

        }

2008年8月14日

 

转载于:https://www.cnblogs.com/yxzyxz/archive/2008/08/14/1268145.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值