Selenium:解決Failed to start new browser session

來源:互聯網
上載者:User

因為項目需要,最近開始學習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:)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.