代碼十分簡(jiǎn)單,作用是初始化一個(gè)Selenium對(duì)象。其中:
url :是你要測(cè)試的網(wǎng)站
localhost: 可以不是localhost,但是必須是selenium server啟動(dòng)的地址
*iexplore : 可以是其它瀏覽器類(lèi)型,可以在網(wǎng)站上看都支持哪些。
下面我要講講怎么使用selenium這個(gè)對(duì)象來(lái)進(jìn)行測(cè)試。
1、測(cè)試文本輸入框
假設(shè)頁(yè)面上有一個(gè)文本輸入框,我們要測(cè)試的內(nèi)容是在其中輸入一些內(nèi)容,然后點(diǎn)擊一個(gè)按鈕,看看頁(yè)面的是否跳轉(zhuǎn)到需要的頁(yè)面。
1. public void test1() {
2.
3. selenium.open("http://xxx.xxx.xxx/yyy");
4.
5. selenium.type("xpath=//input[@name='userID']", "test-user");
6. selenium.click("xpath=//input[@type='button']");
7. selenium.waitForPageToLoad("2000");
8. assertEquals(selenium.getTitle(), "Welcome");
9. }
上面的代碼是這個(gè)意思:
1)調(diào)用selenium.open方法,瀏覽器會(huì)打開(kāi)相應(yīng)的頁(yè)面
2)使用type方法來(lái)給輸入框輸入文字
3)等待頁(yè)面載入
4)看看新的頁(yè)面標(biāo)題是不是我們想要的。
2、測(cè)試下拉框
1. public void test1() {
2.
3. selenium.open("http://xxx.xxx.xxx/yyy");
4.
5. selenium.select("xpath=//SELECT[@name='SBBUSYO']", "index=1");
6. selenium.click("xpath=//input[@type='button']");
7. selenium.waitForPageToLoad("2000");
8. assertEquals(selenium.getTitle(), "Welcome");
9. }
可以看到,我們可以使用select方法來(lái)確定選擇下拉框中的哪個(gè)選項(xiàng)。
select方法還有很多用法,具體去看看文檔吧。
3、測(cè)試check box
1. public void test1() {
2.
3. selenium.open("http://xxx.xxx.xxx/yyy");
4.
5. selenium.check("xpath=//input[@name='MEICK_000']");
6. selenium.click("xpath=//input[@type='button']");
7. selenium.waitForPageToLoad("2000");
8. assertEquals(selenium.getTitle(), "Welcome");
9. }
我們可以使用check方法來(lái)確定選擇哪個(gè)radio button。
4、得到文本框里的文字
1. assertEquals(selenium.getValue("xpath=//input[@name='WNO']"), "1");
getValue方法是得到文本框里的數(shù)值,可不是getText方法,用錯(cuò)了可郁悶了。
5、判斷頁(yè)面是否存在一個(gè)元素
1. assertTrue(selenium.isElementPresent("xpath=//input[@name='MEICK_000']"));
一般這個(gè)是用來(lái)測(cè)試當(dāng)刪除一些數(shù)據(jù)后,頁(yè)面上有些東西不會(huì)顯示的情況。