Selenium 是 ThoughtWorks 專門為 Web 應(yīng)用程序編寫的一個(gè)驗(yàn)收測(cè)試工具。據(jù) Selenium 主頁(yè)所說(shuō),與其他測(cè)試工具相比,使用 Selenium 的最大好處是:
Selenium 測(cè)試直接在瀏覽器中運(yùn)行,就像真實(shí)用戶所做的一樣。Selenium 測(cè)試可以在 Windows、Linux 和 MacintoshAnd 上的 Internet Explorer、Mozilla 和 Firefox 中運(yùn)行。其他測(cè)試工具都不能覆蓋如此多的平臺(tái)。
使用 Selenium 和在瀏覽器中運(yùn)行測(cè)試還有很多其他好處。下面是主要的兩大好處:
- 通過(guò)編寫模仿用戶操作的 Selenium 測(cè)試腳本,可以從終端用戶的角度來(lái)測(cè)試應(yīng)用程序。
- 通過(guò)在不同瀏覽器中運(yùn)行測(cè)試,更容易發(fā)現(xiàn)瀏覽器的不兼容性。
Selenium 的核心,也稱 browser bot ,是用 JavaScript 編寫的。這使得測(cè)試腳本可以在受支持的瀏覽器中運(yùn)行。browser bot 負(fù)責(zé)執(zhí)行從測(cè)試腳本接收到的命令,測(cè)試腳本要么是用 HTML 的表布局編寫的,要么是使用一種受支持的編程語(yǔ)言編寫的。
在下面的情況下,可以選擇SeleniumRC進(jìn)行功能測(cè)試。
- condition statements
- iteration
- logging and reporting of test results
- error handling, particularly unexpected errors
- database testing
- test case grouping
- re-execution of failed tests
- test case dependency
- screenshot capture of test failures
首先要下載SeleniumRC,不用安裝,解壓即可,可以看到這樣幾個(gè)目錄,下圖示:
selenium-server-1.0.1目錄,是服務(wù)器端,他可以接受測(cè)試程序指令,并將測(cè)試結(jié)果返回測(cè)試程序。
在測(cè)試前必須先啟動(dòng)他,啟動(dòng)過(guò)程:開始-運(yùn)行-cmd-cd <服務(wù)器端目錄>-java -jar selenium-server.jar
(服務(wù)器端其實(shí)就是個(gè)Jar文件)
?
然后就可以進(jìn)行客戶端,本文用C#來(lái)進(jìn)行測(cè)試,首先建立一個(gè)C#類庫(kù)工程,添加引用selenium-dotnet-client-driver-1.0.1目錄下的所有DLL,具體如下圖示。
下面,新建類SeleniumTest,具體代碼如下:
?

1 [TestFixture]
2 public class SeleniumTest
3 {
4 private ISelenium selenium;
5 private StringBuilder verificationErrors;
6
7 [SetUp]
8 public void SetupTest()
9 {
10 selenium = new DefaultSelenium( " localhost " , 4444 , " *iexplore " , " http://localhost:2896/WebTestSite/ " );
11 selenium.Start();
12
13 verificationErrors = new StringBuilder();
14 }
15
16 [TearDown]
17 public void TeardownTest()
18 {
19 try
20 {
21 selenium.Stop();
22 }
23 catch (Exception)
24 {
25 // Ignore errors if unable to close the browser
26 ? }
27 Assert.AreEqual( "" , verificationErrors.ToString());
28 }
29
30 [Test]
31 public void TheSeleniumTest()
32 {
33 selenium.Open( " /WebTestSite/ " );
34 selenium.Type( " TextBox1 " , " qeq " );
35 selenium.Type( " TextBox2 " , " qwe " );
36 selenium.Click( " Button1 " );
37
38 // 判斷是否出現(xiàn)alert("fail")
39 ? Assert.AreEqual( " fail " , selenium.GetAlert());
40
41 selenium.Type( " TextBox1 " , " 123 " );
42 selenium.Type( " TextBox2 " , " 123 " );
43 selenium.Click( " Button1 " );
44 Assert.AreEqual( " fail " , selenium.GetAlert());
45
46 // 點(diǎn)擊鏈接
47 ? selenium.Click( " link=2 " );
48 // 等待
49 ? selenium.WaitForPageToLoad( " 30000 " );
50 selenium.Click( " link=3 " );
51 selenium.WaitForPageToLoad( " 30000 " );
52
53 }
54 [Test]
55 public void TestTitle()
56 {
57 selenium.Open( " /WebTestSite/**.aspx " );
58 Assert.AreEqual( " yourtitle " , selenium.GetTitle());
59
60 }
61 }
?
這樣,就建好了,可以打開NUit進(jìn)行測(cè)試,也可以直接寫個(gè)main進(jìn)行測(cè)試。
?
seleniumhq官方文檔:
http://seleniumhq.org/docs/05_selenium_rc.html#introduction
?
參考:
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
