我的漫漫程序之旅

          專注于JavaWeb開(kāi)發(fā)
          隨筆 - 39, 文章 - 310, 評(píng)論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          selenium初探

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

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

          java -jar selenium-server.jar
          保存為start.bat,名字隨便啦.雙擊啟動(dòng).這個(gè)是服務(wù),在一切工作開(kāi)始之前,
          必須先啟動(dòng)這個(gè),啟動(dòng)后的dos窗口不要關(guān)閉.

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

          第三步:
          寫個(gè)測(cè)試index.jsp頁(yè):

          <%@ 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>
          還有一個(gè)success.jsp:
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
              
          <title>success</title>
            
          </head>
            
          <body>
              ok!
            
          </body>
          </html>

          寫個(gè)測(cè)試類:
          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服務(wù)器地址
                  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());
                  }

              }

              
          //測(cè)試標(biāo)題,文本框輸入,及按鈕點(diǎn)擊
              @Test
              
          public void test1()
              
          {
                  
          //我這里是tomcat的地址,我的tomcat端口是8888,selenium是當(dāng)前工程,我讓它打開(kāi)首頁(yè)
                  selenium.open("http://localhost:8888/selenium/index.jsp");
                  String title 
          = selenium.getTitle();
                  
          //原來(lái)網(wǎng)頁(yè)的標(biāo)題
                  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!");
                  
          //輸出新頁(yè)的標(biāo)題
                  System.out.println(selenium.getTitle());
              }

              
          //測(cè)試選擇框
              @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");
              }

              
              
          }










          注釋已經(jīng)很詳細(xì)了,感覺(jué)selenium很好用,完全可以模擬瀏覽器操作.我這里用的是ie,你當(dāng)然可以用firefox或其它.
          源碼可在我的網(wǎng)盤下載.


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

          主站蜘蛛池模板: 张家港市| 北流市| 新晃| 闸北区| 望都县| 南澳县| 甘孜县| 疏勒县| 海淀区| 武川县| 德惠市| 牙克石市| 顺义区| 同仁县| 中江县| 招远市| 高邑县| 八宿县| 上高县| 陵川县| 志丹县| 阜阳市| 巴楚县| 榆树市| 普宁市| 新巴尔虎右旗| 宣城市| 余庆县| 武乡县| 东乌珠穆沁旗| 金山区| 安福县| 洛南县| 石景山区| 安龙县| 两当县| 沽源县| 宁蒗| 天等县| 通山县| 扶沟县|