読者です 読者をやめる 読者になる 読者になる

SeleniumServer 起動サンプル


SeleniumServer 起動のサンプル。裏で起動してたらシャットダウンして新たに起動。

package etc9;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;

public class SeleniumRCServer {
    public static final int PORT = 4444;
    private static final int TIMEOUT = 60;
    private static final String STOP_COMMAND =
        "http://localhost:" + PORT + 
        "/selenium-server/driver/?cmd=shutDownSeleniumServer";
    
    private static SeleniumServer seleniumServer;
    
    public static void start() throws Exception {
        RemoteControlConfiguration rcConfig = new RemoteControlConfiguration();
        rcConfig.setTimeoutInSeconds(TIMEOUT);
        rcConfig.setPort(PORT);
        try {
            startNewServer(rcConfig);
        } catch (java.net.BindException bE){
            if(runHTTPCommand(STOP_COMMAND)){
                try {
                    startNewServer(rcConfig);
                } catch (Exception e) {
                    throw new IllegalStateException("Could not stop existing server on blocked port " + PORT, e);
                }
            }
        } catch (Exception e) {
            throw new IllegalStateException("Can't start selenium server", e);
        }
    }
    
    public static void startIfAbsent() throws Exception {
        if(seleniumServer == null) start();
    }

    public static void stop() throws Exception {
        if(seleniumServer != null){
            seleniumServer.stop();
            seleniumServer = null;
        }
    }
    
    private static void startNewServer(RemoteControlConfiguration rcConfig) throws Exception {
        seleniumServer = new SeleniumServer(rcConfig);
        seleniumServer.start();
    }
    
    private static boolean runHTTPCommand(String theCommand) throws IOException {
        URL url = new URL(theCommand);
        URLConnection seleniumConnection = url.openConnection();
        seleniumConnection.connect();
        InputStream inputStream = seleniumConnection.getInputStream();
        ByteArrayOutputStream outputSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[2048];
        int streamLength;
        while ((streamLength = inputStream.read(buffer)) != -1) {
            outputSteam.write(buffer, 0, streamLength);
        }
        inputStream.close();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String stringifiedOutput = outputSteam.toString();
        if (stringifiedOutput.startsWith("OK"))
            return true;
        return false;
    }
}


テストメソッドの例

    @Test
    public void test() throws Exception {
        SeleniumRCServer.start();
        Selenium selenium = new DefaultSelenium("localhost",
                SeleniumRCServer.PORT, "*chrome", "http://www.google.com");
        selenium.start();
        
        selenium.open("http://www.google.com");
        selenium.type("xpath=//input[@name='q']", "Selenium-RC");
        selenium.click("xpath=//input[@name='btnG' and @type='submit']");
        selenium.waitForPageToLoad("10000");
        selenium.close();
        selenium.stop();
        
        SeleniumRCServer.stop();
    }