Selenium:解决Failed to start new browser session

因为项目需要,最近开始学习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:)

作者:Shane
出处:http://bluescorpio.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/bluescorpio/p/1639938.html