2012-01-17 78 views
1

我刚刚使用WatiN。我的公司使用jQuery显示和隐藏功能来显示错误消息。我想确保这些消息出现在正确的时间。我一直在使用显示和隐藏的jQuery下载测试脚本。我将id值添加到了div标签和附加的扫描标签之一。显示信息时,我一直无法检测到我的代码。如何使用WatiN与jQuery显示和隐藏功能?

我在下面都包括了jQuery脚本和我的代码。该脚本有两个按钮,一个用于显示,另一个用于隐藏消息。我的代码点击隐藏按钮,检查可见性和宽度属性,然后点击显示按钮并查看相同的属性。我可以在屏幕上看到文本确实被隐藏然后显示出来。当我设置断点时,在两种情况下,可见性都设置为“继承”,宽度设置为“自动”。我能做些什么来区分这两种情况?

jQuery code: 
<!DOCTYPE html> 
<html> 
<head> 
     <style> 
     span { background:#D8BFD8; padding:3px; float:left; } 
     </style> 
     <script src="jquery-1.7.1.js" type="text/javascript"></script> 
    </head> 
<body> 
    <button id="hidb">Hide</button> 
    <button id="showb">Show</button> 
    <div id="dynamicOutput"> 

    <span id="jquery">jQuery</span> <span>is</span> <span>easy</span> 
    <span>to</span> <span>use</span> <span>and</span> 
    <span>gives</span> <span>dynamic output..</span> 

    </div> 

<script> 
    $("#hidb").click(function() { 
       $("span:last-child").hide("fast", function() { 
    // use callee so don't have to name the function 
     $(this).prev().hide("fast", arguments.callee); 
     }); 
    }); 
$("#showb").click(function() { 
    $("span").show(2000); 
}); 

</script> 
</body> 
</html> 

Test code: 

[TestMethod] 
     [STAThread] 
     public void lookAtElements() 
     { 

       var browser = new IE("http://localhost/test/jqHIdeShowText.html"); 

       Element el2 = browser.Span(Find.ById("jquery")); 
       Element el = browser.Div(Find.ById("dynamicOutput")); 

       string att; 
       string att2; 
       string width; 
       string width2; 
       string msg; 
       string msg2; 


       Button btn = browser.Button(Find.ById("hidb")); 
       btn.Click(); 


       width = el.Style.GetAttributeValue("width"); 
       width2 = el2.Style.GetAttributeValue("width"); 

       System.Threading.Thread.Sleep(5000); 

       el.Parent.Refresh(); 
       el.Refresh(); 
       el2.Refresh(); 

       width = el.Style.GetAttributeValue("width"); 
       width2 = el2.Style.GetAttributeValue("width"); 
       att = el.Style.GetAttributeValue("visibility"); 
       att2 = el2.Style.GetAttributeValue("visibility"); 
       msg = el.Text; 
       msg2 = el2.Text; 


       btn = browser.Button(Find.ById("showb")); 
       btn.Click(); 
       System.Threading.Thread.Sleep(5000); 

      el.Parent.Refresh(); 
      el.Refresh(); 
      el2.Refresh(); 

       att = el.Style.GetAttributeValue("visibility"); 
       att2 = el2.Style.GetAttributeValue("visibility"); 
       width = el.Style.GetAttributeValue("width"); 
       width2 = el2.Style.GetAttributeValue("width"); 
       msg = el.Text; 
       msg2 = el2.Text; 

       browser.Close(); 


      } 

回答

0

尝试测试display属性改为:

if (el.Style.GetAttributeValue("display") == "none") { 
    // The element was hidden through hide(). 
} 
+0

感谢您的答复。我需要的是查看是否显示与输入字段关联的错误消息。我发现的是,如果出现错误消息,则输入字段的父项具有包含错误消息的文本框,如果没有错误,则没有文本框。 – user1153980 2012-02-24 21:12:00

+0

优秀:)现在你只需要在你自己的答案中发布你的评论的相关部分(可能会详细阐述一下,如你所见),然后接受它。 [在这种情况下,这是允许的和实际上鼓励的](http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-those-where-i-knew-the-回答,提问前,)。 – 2012-02-24 21:21:23