您的位置:首页 > 大数据 > 人工智能

Selenium:解决Failed to start new browser session

2010-01-05 22:16 459 查看
因为项目需要,最近开始学习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:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐