自動化測試,何為自動化測試,簡單點說就是機器代替人工操作,那么WatiN實現Web應用在瀏覽器上的自動化就需要模擬人工點擊、輸入、選擇、拖放等等操作,在這些操作中會觸Html元素的onclick、onchange、onmousemove等事件。在WatiN中提供了FireEvent和FireEventNoWait。下面舉一些常見的元素操作例子。
1、文本框輸入
有人說,不就是文本框輸入嗎,可簡單,直接設置元素Value屬性就可以了。是的,如果只是輸入的動作,那么就這么簡單,但是在實際測試中,文本框的onchange、onblur、onfocus等事件就需要考慮了。
實例:
Html
1 <html> 2 <head> 3 <meta charset="utf-8"> 4 <title>WatiN測試頁面</title> 5 </head> 6 <body> 7 <div> 8 <input type="text" id="txtName" onblur="alert('失去焦點')" /> 9 </div> 10 </body> 11 </html>
C#
1 IE ie = IE.InternetExplorers().FirstOrDefault(p => !string.IsNullOrEmpty(p.Title) && p.Title == "WatiN測試頁面"); 2 3 TextField name = ie.TextField(Find.ById("txtName")); 4 name.Focus(); 5 name.Value = "張三"; 6 7 name.FireEvent("onblur");
? 2、選擇框
Html
<html> <head><meta charset="utf-8"><title>WatiN測試頁面</title> </head> <body><div><select id="selState"><option>--請選擇--</option><option>待出庫</option><option>已發貨</option><option>配送中</option><option>已簽收</option></select></div> </body> </html>
1 IE ie = IE.InternetExplorers().FirstOrDefault(p => !string.IsNullOrEmpty(p.Title) && p.Title == "WatiN測試頁面"); 2 3 SelectList stateList = ie.SelectList(Find.ById("selState")); 4 stateList.SelectByValue("已發貨");
?