因為項目需要,最近開始學習Selenium,發現Selenium不如它首頁說的那麼好,只是在處理快顯視窗上就很讓人頭痛。
我利用selenium-core-1.0.1來熟悉各種Java方法,但是在實際編碼中遇到問題,不知道如何解決.
代碼package com.thoughtworks.selenium.corebased;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;
public class TestClick extends SeleneseTestCase {
private static final String BASE_SERVER_PATH = "C:\\selenium-core-1.0.1\\tests";
private static final String TEST_FILE =
BASE_SERVER_PATH + "\\html\\test_click_page1.html";
private Selenium browser =
new DefaultSelenium("localhost", 4444, "iexplore", TEST_FILE);
private SeleniumServer seleniumServer;
public void setUp() throws Exception{
seleniumServer = new SeleniumServer();
seleniumServer.start();
browser.start();
}
public void tearDown() throws Exception{
browser.stop();
seleniumServer.stop();
}
public void testClick() throws Throwable {
browser.open(TEST_FILE);
verifyEquals("Click here for next page", browser.getText("link"));
}
}
錯誤碼:
java.lang.RuntimeException: Could not start Selenium session: Failed to start new browser session: Error while launching browser
今天在一個同事的協助下解決了這個問題,原來在定義Selenium browser = new DefaultSelenium("localhost", 4444, "iexplore", TEST_URL);的時候,TEST_URL的不同會導致DefaultSelenium啟動session的時候不一樣的。
比如如果TEST_URL是一個http開頭的網站像:http://www.bitmotif.com,上面貼的代碼就沒問題,可以pass。但是當你試圖開啟本地的html檔案,比如C:\selenium-core-1.0.1\tests\html\test_select_window.html,你就必須在定義的時候,在前面加上file:///,完整的定義如下:
private static final String BASE_SERVER_PATH = "file:///C:\\selenium-core-1.0.1\\tests";
private static final String TEST_FILE =
BASE_SERVER_PATH + "\\html\\test_click_page1.html";
原理我還沒搞清楚,至少這樣改了指令碼可以pass:)