从selenium docker hub

时间:2015-10-27 16:20:11

标签: java selenium docker

有没有办法从网络驱动程序获取节点端口?我有一个界面,它向我显示在集线器节点中运行的进程,用于调试目的。在最终产品中,我想要一个按钮VIEW,以编程方式打开该节点上的vnc查看器。我试过这个代码只在集线器中运行一个firefox-debug节点:

package testwebscrapinggebgroovy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


public class TestJava {

    public static String getHostName(SessionId session) throws UnknownHostException
    {
        String hostDetail = null;
        String hostName ="localhost";
        int port = 4444;
        String errorMsg = "Failed to acquire remote webdriver node and port info. Root cause: ";

        try {
            HttpHost host = new HttpHost(hostName, port);
            DefaultHttpClient client = new DefaultHttpClient();
            URL sessionURL = new URL("http://" + hostName + ":" + port + "/grid/api/testsession?session=" + session);
            System.out.println("URL is : "+sessionURL);
            BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST", sessionURL.toExternalForm());
            HttpResponse response = client.execute(host, r);
            //JSONObject object = extractObject(response);
            //URL myURL = new URL(object.getString("proxyId"));
            JsonObject myjsonobject =extractObject(response);
            JsonElement url = myjsonobject.get("proxyId");
            System.out.println(url.getAsString());
            URL myURL = new URL(url.getAsString());
            if ((myURL.getHost() != null) && (myURL.getPort() != -1)) {
                hostDetail = myURL.getHost();
            }

        } catch (Exception e) {
            //logger.log(Level.SEVERE, errorMsg, e);
            throw new RuntimeException(errorMsg, e);
        }
        return hostDetail;
    }

    private static JsonObject extractObject(HttpResponse resp) throws IOException {
        BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
        StringBuffer s = new StringBuffer();
        String line;
        while ((line = rd.readLine()) != null) {
            s.append(line);
        }
        rd.close();
        JsonParser parser = new JsonParser();
        JsonObject objToReturn = (JsonObject)parser.parse(s.toString());
        System.out.println(objToReturn.toString());
        System.out.println(objToReturn.get("proxyId"));
        return objToReturn;
    }


    public static void main(String[] args) throws MalformedURLException, UnknownHostException {
        DesiredCapabilities dc = new DesiredCapabilities();
        RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),dc.firefox());
        driver.get("http://www.google.com");
        System.out.println(driver.getTitle());
        String hostname = getHostName(driver.getSessionId());
        System.out.println("hostname" +hostname);
        driver.quit();
    }

}

输出是:

Google
URL is : http://localhost:4444/grid/api/testsession?session=df503977-0778-4ff1-a853-d8508d26a759
{"msg":"slot found !","success":true,"session":"df503977-0778-4ff1-a853-d8508d26a759","internalKey":"cbd2c765-ef86-46c7-859f-ddd117727cba","inactivityTime":46,"proxyId":"http://172.17.0.24:5555"}
"http://172.17.0.24:5555"
http://172.17.0.24:5555
hostname172.17.0.24

哪个是我的集线器的代理ID,而不是运行此代码的节点的ip:port。有没有办法至少获得使用此命令的节点名称?:

 docker port <container-name|container-id> 5900

0 个答案:

没有答案