Connect Selenium WebDriver to an existing browser session

I work with selenium and I would like to add an instance of webdriver if there is currently an existing browser session (Chrome for me). I do not want to open a new browser window / session. I have googled and saw that there are several ways to do this with the description on these sites:

I am using ChromeDriver 2.29 in the latest version.

My code is as follows:

public static void main(String[] args) throws Exception {
    // starting Chrome Webdriver server
    ChromeDriverService service = new ChromeDriverService.Builder()
            .usingDriverExecutable(new File("D:\\Development\\chromedriver\\chromedriver.exe"))
            .usingAnyFreePort()
            .build();
    service.start();

    WebDriver driver = new CustomRemoteWebDriver(service.getUrl(),DesiredCapabilities.chrome());
    driver.get("http://www.google.com");
    WebDriver driver2 = new CustomRemoteWebDriver(service.getUrl(),DesiredCapabilities.chrome());
    // here I am expecting www.google.com from last driver instance, because it should have the same session
    System.out.println(driver2.getCurrentUrl());
    driver.quit();
    driver2.quit();
}

An extended RemoteWebDriver that checks if a session exists:

public class CustomRemoteWebDriver extends RemoteWebDriver {
    public static String sessiondIdPath = "c:\\automation\\sessionid";

    public CustomRemoteWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
        super(remoteAddress, desiredCapabilities);
    }

    @Override
    protected void startSession(Capabilities desiredCapabilities) {
        String sid = getPreviousSessionIdFromSomeStorage();
        if (sid != null) {
            setSessionId(sid);
            try {
                getCurrentUrl();
            } catch (WebDriverException e) {
                // session is not valid
                e.printStackTrace();
                sid = null;
            }
        }

        if (sid == null) {
            super.startSession(desiredCapabilities);
            saveSessionIdToSomeStorage(getSessionId().toString());
        }
    }

    private void saveSessionIdToSomeStorage(String sessionId) {
        try {
            FileUtils.writeStringToFile(new File(sessiondIdPath), sessionId, Charset.defaultCharset());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getPreviousSessionIdFromSomeStorage() {
        String sessionId;
        try {
            List<String> sidText = FileUtils.readLines(new File(sessiondIdPath), Charset.defaultCharset());
            sessionId = sidText.get(0);
        } catch (Exception e) {
            return null;
        }
        return sessionId;
    }
}

and pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.andy.selenium</groupId>
    <artifactId>remotewebdriverexample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>4.1.2</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

, sessionId: driver1 www.google.com , . driver2 WebDriverException CustomRemoteWebDriver#startSession. stacktrace, sessionId . stacktrace :

ChromeDriver 2.29.461591 (62ebf098771772160f391d75e589dc567915b233) 31495 . Mai 25, 2017 6:53:14 PM org.openqa.selenium.remote.ProtocolHandshake createSession : : OSS

org.openqa.selenium.WebDriverException: . . : : "3.4.0", : 'unknown', : 'unknown' : host: "DESKTOP-AGEFV4C", ip: "192.168.134.1", os.name: "Windows 10", os.arch: 'amd64', os.version: '10.0 ', java.version:' 1.8.0_121 ' : driver.version: CustomRemoteWebDriver org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:154)    org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)    org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:694)    org.openqa.selenium.remote.RemoteWebDriver.getCurrentUrl(RemoteWebDriver.java:374)    CustomRemoteWebDriver.startSession(CustomRemoteWebDriver.java:57)    org.openqa.selenium.remote.RemoteWebDriver. (RemoteWebDriver.java:137)    org.openqa.selenium.remote.RemoteWebDriver. (RemoteWebDriver.java:174)    CustomRemoteWebDriver. (CustomRemoteWebDriver.java:22) RemoteBrowserConnector.start(RemoteBrowserConnector.java:40) RemoteBrowserConnector.main(RemoteBrowserConnector.java:26) Mai 25, 2017 6:53:18 org.openqa.selenium.remote.ProtocolHandshake createSession : : OSS

, java-, , . - . , ?

+2
1

, , , , . , , , localhost, , localhost:60003. . #.

  1. , , , .

enter image description here

  1. .

ChromeOptions option = new ChromeOptions();

option.DebuggerAddress="localhost:60422";//we need to add this chrome option to connect the required session

driver = new ChromeDriver(option);

driver.Navigate().GoToUrl("https://www.google.com/");

!! , - .

0

Source: https://habr.com/ru/post/1678229/


All Articles