Selenium 與Junit 結合完成Web autmation


Selenium 可以和Junit 結合,Junit的assertxxx方法做驗證,selenium用來操作web頁面。

下文 轉載自 http://www.cnblogs.com/keeping/archive/2011/12/21/2296447.html


前提:必須安裝了eclipse,且在eclipse中新建了project,如project名稱爲test

1.對test加載selenium-server-standalone-2.15.0.jar包及Junit4包

2.將Selenium IDE中錄製的腳本轉換成Junit 4(options-->format-->Junit 4(Remote control)),對轉換後的腳本進行復制,拷貝至eclipse的某個新建測試類中

3.修改代碼:在setup()方法中增加Selenium RC啓動方法,否則運行失敗

public class SampleTest extends SeleneseTestCase {

SeleniumServer SELENIUM_SERVER;
 @Before
 public void setUp() throws Exception {
  RemoteControlConfiguration rcc = new RemoteControlConfiguration();
  rcc.setPort(4444); 
  SELENIUM_SERVER = new SeleniumServer(rcc);
  SELENIUM_SERVER.start();

   //若Firefox是默認安裝,則不需要指定路徑“*firefox”即可;最後參數爲URL
  selenium = new DefaultSelenium("localhost", 4444, "*firefox D:/Program Files/Mozilla Firefox 4.0.1/firefox.exe", "http://ip:8085/xx/aa.jsf/");
  selenium.start();
 }

@Test
 public void testUntitled() throws Exception {
  selenium.open("/xx/aa.jsf");
  assertEquals("xx系統", selenium.getTitle());//獲取URL標題
  selenium.type("id=ext-comp-12", "admin");//輸入用戶名
  assertTrue(selenium.isElementPresent("xpath=//input[@id='ext-comp-1012']"));
  selenium.type("id=ext-comp-13", "123");//輸入密碼
  Thread.sleep(2000);//等待2S裝載數據
  selenium.clickAt("id=ext-gen4","登錄");//點擊登錄按鈕
  selenium.waitForPageToLoad("30000");

  .....

  .....
 

 }

 @After
 public void tearDown() throws Exception {
  selenium.stop();
  SELENIUM_SERVER.stop();
 }

 

 注:運行Testcase時出現錯誤,建議在https://groups.google.com/forum/#!forum/selenium-users中查找問題


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章