qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          測試框架:利用WatiN自動化網站功能測試

           網站到結束階段,發現每改一個bug,都要重新打開每個頁面測試頁面能否打開,并且打開頁面后進行一些操作測試能否正常進行。每次部署后,發現新Bug修復后,都要做一遍功能測試
            先普及一下什么是功能測試吧。簡單的說功能測試主要是參照用戶手冊,看看能不能完成所預先設計的功能狀態,有點類似于從用戶使用的角度來做測試。比如一個網站登陸的功能,做功能測試,我們需要驗證能否打開登陸的頁面,輸入用戶名,密碼,看是否成功….
            WatiN是開源的C#庫,利用它我們可以把需要手工做的功能測試自動化。
            watiN官方下載地址:
            http://sourceforge.net/project/showfiles.php?group_id=167632
            下載后解壓,假設解壓到: c:\Program Files\WatiN\,
            在c:\Program Files\WatiN\bin\目錄下有個watiN.Core.dll文件,這個是最主要的動態鏈接庫。
            咱們先用VS新建一個Test Project吧,不會的請看我博客上的前一篇文章
            利用VisualStudio白盒測試入門
            當然我們也可以不建Test Project,建其他的Console Application或者Windows Application都是可以的。
            建好工程后,添加對WatiN.Core.dll的引用(Add Reference).
            在代碼中添加命名控件的引用:
            using WatiN.Core;
            然后新加一個Test Method,如下:
          public void TestLoginFailed()
          {
          using (IBrowser ie = BrowserFactory.Create(BrowserType.InternetExplorer))
          {
          ie.GoTo("http://www.google.com");
          ie.TextField(Find.ByName("q")).Value = "WatiN";
          ie.Button(Find.ByName("btnG")).Click();
          Assert.IsTrue(ie.ContainsText("WatiN"));
          }
          }
            呵呵,運行一下,看看效果吧,是不是自動打開IE,然后打開google在搜索框上填入了關鍵詞,點擊搜索,看到搜索結果了呢?
            控制瀏覽器就是這么簡單的幾句話就完成了。



            估計有人會說了,你怎么知道google頁面的輸入框的名字是“q”,按鈕名字是“btnG”呢? 看網頁源代碼去吧。
            呵呵,有個很好用的工具,對我們分析網頁很方便。他就是 IE Developer Toolbar
            官方下載地址:
            http://www.microsoft.com/downloads/details.aspx?FamilyID=e59c3964-672d-4511-bb3e-2d5e1db91038&displaylang=en
            安裝完之后,打開IE,可以看到如下圖所示的東東:
            用這個我們就可以很方便的得到自己所需要的網頁元素的各種屬性了。
            我最近就愛上它了,看上哪個網站的網頁做得好,自己想照搬,就用這個工具,很簡單就搞定了。比起自己去研究他的網頁源代碼方便多了。
            廢話少說,有了IE Developer ToolBar之后,我們剩下的用WatiN的測試工作就是打開網頁,找到網頁標簽,對標簽進行控制,然后驗證結果。這個是最基本的操作了。下面大概看一下做這些操作用到的幾個類和方法吧。
            1. 打開網頁
            BrowserFactory.Create() 創建一個瀏覽器,可以是IE,可以是Firefox,很可惜不支持Opera,
            IBrowser Interface, 瀏覽器接口。
            IBrowser.GoTo() 轉到某個URL去
            示例代碼請看上面的代碼

          字體:        | 上一篇 下一篇 | 打印  | 我要投稿 

            2. 找到網頁上的各種元素
            我們利用IE Developer ToolBar可以找到各種元素,那么HTML里的網頁元素如何和WatiN里的類聯系上來呢,請看下表:
          Html elementWatiN ClassWatiN CollectionExampleVersion

          <a />

          Link

          LinkCollection

          Ie.Link(linkId)

          0.7

          <area />

          Area

          AreaCollection

          ie.Area(Find.ByAlt(alttext))

          1.2

          <button />

          Button

          ButtonCollection

          Ie.Button(buttonId)

          0.9

          <div />

          Div

          DivCollection

          Ie.Div(divId)

          0.7

          <form />

          Form

          FormCollection

          Ie.Form(formId)

          0.7

          <frame />

          Frame

          FrameCollection

          Ie.Frame(frameId)

          0.7

          <frameset />

          -

          FrameCollection

          Ie.Frames

          0.7

          <iframe />

          Frame

          FrameCollection

          Ie.Frame(iframeId)

          0.9

          <img />

          Image

          ImageCollection

          Ie.Image(imageId)

          0.7

          <input type=button/>

          Button

          ButtonCollection

          Ie.Button(buttonId)

          0.7

          <input type=checkbox/>

          CheckBox

          CheckBoxCollection

          Ie.CheckBox(checkboxId)

          0.7

          <input type=file/>

          FileUpload

          FileUploadCollection

          Ie.FileUpload(fileuploadId)

          0.9

          <input type=hidden/>

          TextField

          TextFieldCollection

          Ie.TextField(hiddenId)

          0.7

          <input type=image/>

          Button

          ButtonCollection

          Ie.Button(imageId)

          0.7

          <input type=image/>

          Image

          ImageCollection

          Ie.Image(imageId)

          0.9.5

          <input type=password/>

          TextField

          TextFieldCollection

          Ie.TextField(passwordId)

          0.7

          <input type=radio/>

          RadioButton

          RadioButtonCollection

          Ie.RadioButton(radioId)

          0.7

          <input type=reset/>

          Button

          ButtonCollection

          Ie.Button(resetId)

          0.7

          <input type=submit/>

          Button

          ButtonCollection

          Ie.Button(submitId)

          0.7

          <input type=text/>

          TextField

          TextFieldCollection

          Ie.TextField(textId)

          0.7

          <label />

          Label

          LabelCollection

          Ie.Label(elementId)

          0.7

          <option />

          Option

          OptionCollection

          Ie.Select(selectId).Options

          1.0

          <p />

          Para

          ParaCollection

          Ie.Para(pId)

          0.7

          <select />

          Select

          SelectCollection

          Ie.Select(selectId)

          0.7

          <span />

          Span

          SpanCollection

          Ie.Span(spanId)

          0.7

          <table />

          Table

          TableCollection

          Ie.Table(tableId)

          0.7

          <tbody />

          TableBody

          TableBodyCollection

          Ie.TableBody(tablebodyId)

          Ie.Table(tableid).TableBodies

          1.0

          <td />

          TableCell

          TableCellCollection

          Ie.TableCell(tablecellId) or

          Ie.Table(TableId).TableRows[0].TableCells[0]

          0.7

          <textarea />

          TextField

          TextFieldCollection

          Ie.TextField(textareaId)

          0.7

          <tr />

          TableRow

          TableRows

          Ie.TableRow(tablerowId) or

          Ie.Table(TableId).TableRows[0]

          0.7

          All elements, also the ones not mentioned in this list


          Element and

          ElementsContainer

          ElementCollection

          Ie.Element(elementId)

          Ie.Element(tagname, elementId)

          0.9


          1.2

            有這個表之后就很容易得到網頁里各種元素對應的類了
            得到類之后查WatiN的幫助,就能知道每個類里有些什么函數,方法,怎么控制就看具體的類及其方法了。
            3.驗證結果
            有很多種方法驗證結果,因人而異了。不過我用得最多的就是ie.ContainsText()方法了。
            有了上面的基礎,基本上可以用WatiN完成一些常用的,基本的功能測試了。

          posted on 2013-12-04 11:16 順其自然EVO 閱讀(254) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2013年12月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 莆田市| 饶平县| 华坪县| 罗定市| 克拉玛依市| 仙桃市| 白沙| 青岛市| 宽甸| 赞皇县| 义马市| 石泉县| 格尔木市| 唐山市| 北安市| 勃利县| 嵊州市| 莒南县| 武鸣县| 苏尼特左旗| 都兰县| 甘泉县| 高要市| 宜春市| 连江县| 新巴尔虎右旗| 桂东县| 铅山县| 汤原县| 宣汉县| 通许县| 文安县| 榆林市| 大港区| 福建省| 阳信县| 区。| 巨鹿县| 五原县| 进贤县| 平阴县|