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