我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          selenium初探

          先確保你的4444端口沒被占用,可以用netstat -an命令查看一下.
          然后確保你的jdk版本在1.5以上.

          第一步:
          找到你下載的selenium解壓目錄下的selenium-server-0.9.2目錄.
          在這個目錄下寫個批處理,內容為:

          java -jar selenium-server.jar
          保存為start.bat,名字隨便啦.雙擊啟動.這個是服務,在一切工作開始之前,
          必須先啟動這個,啟動后的dos窗口不要關閉.

          第二步:
          在MyEclipse建個web工程,把selenium-java-client-driver-0.9.2目錄下的jar包加入
          到web工程的lib目錄下.加入junit4支持.

          第三步:
          寫個測試index.jsp頁:

          <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
          <html>
            
          <head>
              
          <title>test!</title>
            
          </head>
            
            
          <body>
            
          <form action="success.jsp" method="post">
              UserName:
          <input type="text" name="username" /><br />
              
          <select name="select">
                  
          <option value="game">游戲</option>
                  
          <option value="program">編程</option>
              
          </select> <br/>
              
          <input type="submit" name="sub" value="submit"/> <br />
              
          </form>
            
          </body>
          </html>
          還有一個success.jsp:
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
              
          <title>success</title>
            
          </head>
            
          <body>
              ok!
            
          </body>
          </html>

          寫個測試類:
          import org.junit.After;
          import org.junit.Before;
          import org.junit.Test;
          import org.openqa.selenium.server.SeleniumServer;
          import static org.junit.Assert.*;
          import com.thoughtworks.selenium.DefaultSelenium;
          import com.thoughtworks.selenium.Selenium;


          public class TestPage
          {
              
          private Selenium selenium;
              @Before
              
          public void setUp()
              
          {
                  
          //此url必須是Selenium服務器地址
                  String url = "http://localhost:4444";
                  selenium 
          = new DefaultSelenium("localhost",SeleniumServer.getDefaultPort(),"*iexplore",url);
                  selenium.start();
              }

              
              @After
              
          public void tearDown()
              
          {
                  
          try
                  
          {
                      selenium.stop();
                  }
           catch (RuntimeException e)
                  
          {
                      System.out.println(e.getMessage());
                  }

              }

              
          //測試標題,文本框輸入,及按鈕點擊
              @Test
              
          public void test1()
              
          {
                  
          //我這里是tomcat的地址,我的tomcat端口是8888,selenium是當前工程,我讓它打開首頁
                  selenium.open("http://localhost:8888/selenium/index.jsp");
                  String title 
          = selenium.getTitle();
                  
          //原來網頁的標題
                  System.out.println(title);
                  selenium.type(
          "xpath=//input[@name='username']""zdw");
                  
          //得到輸入的文本框的值
                  System.out.println("textvalue:" + selenium.getValue("xpath=//input[@name='username']"));
                  selenium.click(
          "xpath=//input[@name='sub']");
                  selenium.waitForPageToLoad(
          "4000");
                  assertEquals(title, 
          "test!");
                  
          //輸出新頁的標題
                  System.out.println(selenium.getTitle());
              }

              
          //測試選擇框
              @Test
              
          public void testSelect()
              
          {
                  selenium.open(
          "http://localhost:8888/selenium/index.jsp");
                  selenium.select(
          "xpath=//select[@name='select']""index=1");
                  
          //得到選擇的id
                  System.out.println("selectid:" + selenium.getSelectedIndex("xpath=//select[@name='select']"));
                  
          //得到選擇的值
                  System.out.println("selectvalue:" + selenium.getSelectedValue("xpath=//select[@name='select']"));
                  selenium.click(
          "xpath=//input[@type='submit']");
                  selenium.waitForPageToLoad(
          "3000");
              }

              
              
          }










          注釋已經很詳細了,感覺selenium很好用,完全可以模擬瀏覽器操作.我這里用的是ie,你當然可以用firefox或其它.
          源碼可在我的網盤下載.


          posted on 2007-11-24 16:13 々上善若水々 閱讀(1895) 評論(0)  編輯  收藏 所屬分類: 軟件測試

          主站蜘蛛池模板: 宜阳县| 新余市| 金乡县| 绥德县| 蛟河市| 康定县| 新化县| 景宁| 松阳县| 磐石市| 武邑县| 荃湾区| 特克斯县| 凌云县| 宿松县| 读书| 榆林市| 长寿区| 内乡县| 海伦市| 陆良县| 苏州市| 安多县| 南安市| 西林县| 蓝田县| 商水县| 叙永县| 凌源市| 中牟县| 龙陵县| 道真| 页游| 讷河市| 云霄县| 商丘市| 资溪县| 新野县| 新余市| 牙克石市| 全南县|