您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源功能測(cè)試工具 > Selenium
利用Selenium自動(dòng)化web測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/6/15 16:19:26 ] 推薦標(biāo)簽:Selenium 功能測(cè)試 自動(dòng)化測(cè)試

  清單 4. 自動(dòng)化選擇的 RC 代碼
  //click on the date field by id you defined;
  selenium.clickAt("dateBox","");
  //wait for the drop down date box by id;
  selenium.waitForCondition("selenium.isElementPresent("widget_dateBox_dropdown")",
  "2000");
  //click previous year 2008;
  selenium.clickAt("//span[contains(@class,'dijitCalendarPreviousYear')]", "");
  //click on the month increase;
  //previous month would contains  ‘dijitCalendarIncrease’.
  selenium.clickAt("//img[contains(@class,'dijitCalendarIncrease')]","");
  //click on the date such as 28 of current month; If you do not specify
  //the td with the attribute of current month class, it will click
  on the //first 28 of previous month;
  selenium.click("//td[contains(@class,'dijitCalendarCurrentMonth')]/span[text()='28']");
  如本例所示,Dojo 應(yīng)用程序不能通過簡(jiǎn)單的 IDE 記錄進(jìn)行測(cè)試。這些腳本有可能不能通過測(cè)試。腳本中有一些丟失的操作,或者操作并不真正工作。腳本應(yīng)該調(diào)整成能夠在 IDE 和 RC 中順利地執(zhí)行。對(duì)于復(fù)雜的 Dojo 小部件,一種可能的解決方案是使用 runScript(String) 函數(shù),因?yàn)?Selenium 對(duì) JavaScript 提供很好的支持。清單 5 提供一個(gè) JavaScript 語句來模擬組合框選擇。
  清單 5. 運(yùn)行 JavaScript 語句在組合框上進(jìn)行選擇
  selenium.runScript("dijit.byId("offeringType").setValue("Stack(SWG)");");
  如何利用 Ant 構(gòu)建 Selenium 測(cè)試
  諸如 Ant 這樣的集成工具可以方便地構(gòu)建 Selenium 測(cè)試和順暢地運(yùn)行測(cè)試用例,無需單獨(dú)啟動(dòng) Selenium 服務(wù)器。如果 Selenium 測(cè)試由 TestNG 驅(qū)動(dòng),那么定義清單 6 所示 TestNG Ant 任務(wù)。清單 6 中假設(shè) classpath 是 TestNG.jar 文件的文件路徑。
  清單 6. TestNG Ant 任務(wù)
  <taskdef resource="testngtasks" classpath="testng.jar"/>      
  主要的目標(biāo)是啟動(dòng)服務(wù)器、運(yùn)行測(cè)試,然后停止服務(wù)器。這些任務(wù)按照 bulid.xml 中定義的順序?qū)崿F(xiàn)在清單 7 中。
  清單 7. 啟動(dòng)服務(wù)器、運(yùn)行測(cè)試用例并停止服務(wù)器的 Ant 任務(wù)
  <target name="run_test" description="start,run and stop" depends="dist">
  <parallel>
  <antcall target="start-server" />
  <sequential>
  <echo taskname="waitfor" message="Waitforproxy server launch" />
  <waitfor maxwait="2" maxwaitunit="minute" checkevery="100">
  <http url="http://localhost:4444/selenium-server/driver/?cmd=testComplete" />
  </waitfor>
  <antcall target="runTestNG" />
  <antcall target="stop-server" />
  </sequential>
  </parallel>
  </target>
  代碼更可取的地方是使用 waitfor 任務(wù)來測(cè)試 Selenium 服務(wù)器是否已成功啟動(dòng),而不是暫停一段固定的時(shí)間。如果 URL http://localhost:4444/selenium-server/driver/?cmd=testComplete 可用,意味著 Selenium 已經(jīng)成功啟動(dòng)。在清單 7 中,它多等待兩分鐘,并且每 100 毫秒在本地主機(jī)上檢查一次 Selenium 服務(wù)器,以提供完整的 URL。
  start-server 任務(wù)的詳細(xì)內(nèi)容定義在清單 8 中。Firefox profile 模板位置和其他參數(shù)可以指定在標(biāo)記 <arg> 中。
  清單 8. 詳細(xì)的啟動(dòng)服務(wù)器的 Ant 任務(wù)
  <target name="start-server">
  <java jar="lib/selenium-server.jar" fork="true">
  <arg line="-firefoxProfileTemplate ${selenium}/profile/" />
  </java>
  </target>
  runTestNG 任務(wù)的詳細(xì)內(nèi)容定義在清單 9 中。testng 任務(wù)的常用屬性包括 outputDir 和 xmlfileset。屬性 outputDir 用于設(shè)置輸出報(bào)告位置。屬性 xmlfileset 用于包含啟動(dòng) XML 文件。更多選項(xiàng)請(qǐng)參考 TestNG 正式網(wǎng)站。
  清單 9. 運(yùn)行測(cè)試用例的 Ant 任務(wù)
  <target name="runTestNG">
  <testng outputDir="${testng.report.dir}" sourcedir="${build}"
  classpathref="run.cp" haltOnfailure="true">
  <xmlfileset dir="${build}" includes="testng.xml" />
  </testng>
  </target>
  stop-server 任務(wù)的詳細(xì)內(nèi)容定義在清單 10 中。
  清單 10. 停止 Selenium 服務(wù)器的 Ant 任務(wù)
  <target name="stop-server">
  <get taskname="selenium-shutdown"
  src="http://localhost:4444/selenium-server/driver/?cmd=shutDown" ignoreerrors="true" />
  <echo taskname="selenium-shutdown" message=" Errors during shutdown are expected" />
  </target>
  上面列出了關(guān)鍵任務(wù)。將它們組合到您的構(gòu)建文件,以便利用 Ant 完成良好集成的測(cè)試。
  如何支持測(cè)試 HTTPS 網(wǎng)站
  隨著互聯(lián)網(wǎng)日益強(qiáng)調(diào)信息安全,越來越多的 web 應(yīng)用程序在使用 SSL 身份認(rèn)證。Selenium IDE 默認(rèn)支持 HTTPS,但是 Selenium RC 不是這樣的。Internet Explorer 和 Firefox 中的解決方案各不相同。
  對(duì)于 IE,在 setup 目錄下的 SSL 支持文件夾中在安裝一個(gè)證書。如果使用的版本早于 Selenium-RC 1.0 beta 2,請(qǐng)使用 *iehta 運(yùn)行模式,對(duì)于 Selenium-RC 1.0 beta 2 或更晚的版本,使用 *iexplore 運(yùn)行模式。
  如果測(cè)試 HTTPS 網(wǎng)站時(shí)出現(xiàn)一個(gè)如下所示的安全警告,那么單擊 View Certificate 并安裝 HTTPS 網(wǎng)站的證書。如果繼續(xù)彈出警告,那么考慮在 IE 中進(jìn)行配置。打開 Tool > Internet Options > Advanced,并取消選擇 security 分類下的 Warn about invalid site certificates 和 Check for publisher's certificate revocation。
上一頁12345下一頁
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd