Selenium有两种工作模式,Selenium Test Runner (Selenium Core) 和 Selenium Driven (Selenium Rremote Control, 简称RC )。

 

(一)Selenium Test Runner模式

 

该模式下,可以直接录制脚本,也可以用HTML表布局来编写测试用例:

 

 <table border="1">

   <tr>

     <td>open</td>

     <td>\test\hello.html</td>

     <td></td>

   </tr>

    <tr>

     <td>verifyTextPresent</td>

     <td>Target</td>

     <td>Hello World</td>

   </tr>

  </table>

 

(二)Selenium RC (Remote Control) 模式

 

该模式下,用各种语言来编写测试脚本,如Java, Python, C#等。

 

Selenium RC原理: 客户端向Selenium Server发送指令,SeleniumServer接收到测试指令后,启动浏览器并将自己的JavaScript文件嵌入网页中,通过JavaScript调用实现对Html页面的全面追踪,然后把执行结果返回给客户端。

 

下面是一段Java的测试脚本:

package Selenium.Test;  
import com.thoughtworks.selenium.*;  
  
public class seleniumTest {  
    private Selenium selenium;  
public void setUp() {  
     selenium = new DefaultSelenium("10.5.41.55",  
            4444, "*iexplore", "http://www.google.com/");  
     selenium.start();  
    }  
    public void testGoogle() {  
         selenium.open("/");        
  selenium.type("q", "selenium");  
  selenium.click("btnG");  
  selenium.waitForPageToLoad("30000");  
  boolean testResult = (selenium.isTextPresent("Selenium web application testing system"));  
        if (testResult){  
         //用例成功  
         System.out.print("Search selenium web is ok!");  
        } else {  
         //用例失败  
         System.out.print("selenium web not found!");       
        }  
    }  
    public static void main(String[] args) {  
     seleniumTest st = new seleniumTest();  
     st.setUp();  
     st.testGoogle();    
    }  

}