| 
			
		 | 
		
			
				
					
	
		
		
		先确保你的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或其它. 
源码可在我的网盘下载.
		 
		
	 
	 
	
	
	    
    
 
				
			 
		 |