Attaching test to a previously started browser (by a WebDriver)

51 views
Skip to first unread message

Tamas Deak

unread,
Dec 13, 2017, 10:57:32 PM12/13/17
to Selenium Users
Hi everyone, I believe that my expectations are high, so I'm not even sure it can wor. I found some older links where it said it is not possible, but I feel like I'm close to the solution and since those answers (2011) it may have changed:

My configuration:
  • Selenium version: 3.8
  • I have up to dated drivers (just downloaded them)
  • I tried with Chrome and Firefox
  • Testing on Windows 10 Pro
What I want:

1.
I start browser in my app (basically Chrome, but after many unsuccessful tries I also tried it with Firefox):
new ChromeDriver(options)
in case of Firefox:
new FirefoxDriver(options)

2.
After it starts I get the port where it runs and SessionId, I write them to file.

3.
In my tests, I would like to connect to this browser and use it.
var uri = new Uri("http://localhost:" + portItRuns); // if I concat "/wd/hub"  or "/hub" it still doesn't work.
var webDriver = new RemoteWebDriver(uri, options) // or new RemoteWebDriver(uri, DesiredCapabilities.Chrome())

What happens:

1.
Browser = Chrome, Uri = http://localhost:port
Starts a new browser with the previously created driver.

2.
Browser = Chrome, Uri = http://localhost:port/hub or Uri = http://localhost:port/wd/hub
OpenQA.Selenium.WebDriverException: Unexpected error. unknown command: hub/session

3.
Browser = Firefox, Uri = http://localhost:port
OpenQA.Selenium.WebDriverException: Session is already started

4.
Browser = Firefox, Uri = http://localhost:port/wd/hub or http://localhost:port/hub
System.InvalidOperationException: POST /wd/hub/session did not match a known command

What I have also tried:

Override the SessionId: in the Execute method like it writes in the article (link below)
With this, I could create a browser and then attach to to it. But the browser was created in the Test project, and it was a CustomRemoteWebDriver.
If I change the SessionId to the one I passed from the program, instead of the one in this article, it doesn't work:
No exception occurs, the test continues, but if I debug my test, put a breakpoint to after my CustomRemoteWebDriver was instantiated, checking its properties, it says: InvalidOperandExceptions as the values.
My navigation events also doesn't make any effect, but the test continues 

What I'm looking for:
After this long struggling I would like a solution how I could attach to my running browser. Is it even possible with Selenium? Any other solution - without Selenium (the driver is running, so it may be remotable)? 

Tamas Deak

unread,
Dec 14, 2017, 2:39:05 AM12/14/17
to Selenium Users
Solved the problem!

I just messed up the code. Be careful, and use the solution in the article. I set the SessionId in the constructor, but the base class's constructor was running before I set the SessionId, so the overrided Execute method was working wrong.
This code also works with Firefox.

Xiang Dong

unread,
Dec 14, 2017, 3:31:09 AM12/14/17
to Tamas Deak, Selenium Users
If you just want to reuse a pre-initialized web browser, here the browser was initialized by web driver, i think it won't so complicated. You can write a browser instance pool and put the initialized web driver instance into it. Maintain web driver instance's status, such as idle, using or expired. your client can borrow web driver instance from the pool and return once the test finished. If you are running in a grid environment, it could be a little hard because you are not able to maintain the status.

--david 

From: seleniu...@googlegroups.com <seleniu...@googlegroups.com> on behalf of Tamas Deak <tamas....@gmail.com>
Sent: Thursday, December 14, 2017 4:25 AM
To: Selenium Users
Subject: [selenium-users] Attaching test to a previously started browser (by a WebDriver)
 
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/35f9ae0a-45f0-49f2-84f0-a15791dd00b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages