You’re testing a desktop application with Selenium. How do you verify an item on the page after the browser launches?

I recently came up against this use case and couldn’t find a clear answer for C#. Every once in a great while, the need arises to have Selenium communicate to a browser that is already launched, sitting at some page. While the bulk of examples available involve launching the browser with Selenium and then attaching with a different driver, I couldn’t find a way to verify something on the page if Selenium didn’t launch the browser.

Sure, I found a Python and Java example here. There are also a couple of other Java tutorials floating around for how to do this a bit differently via the Canary (and newer) builds of Chrome, but I wanted to bring this goodness to the C# world for current capabilities. In the spirit of DevOps focusing on bringing teams together, both testers and developers could benefit from this. The working example that I’m providing is for testers wanting to test these sorts of scenarios as well as developers so that they have the information they need to launch with settings that will make this easier on their testers.

Let’s get started!

The repo I’m working out of is here.

Cutting right to the chase, the two vital pieces of code are leveraging Chrome’s remote debugging capabilities. Launching Chrome out of a WPF application looks like this:

MainWindow:

The bolded portion is critical. It tells Chrome which port to use for remote debugging and where to find the relevant user data.

PageUnderTest.cs

We point Selenium at a debugger address (port included). Now, we can “attach” to the Chrome instance launched by our desktop app.

You can now drive the Chrome instance started by our small WPF app:

Want more?

Check out my blog, The Feasibility of Test Automation, and discover the benefits of adding simple test automations to the early stages of your project.

Got some other tricky use cases?

Comment below. Let’s solve them together!

Join the Conversation

  1. Mike Curn

2 Comments

Your email address will not be published. Required fields are marked *

    1. For this specific use case, there will be a new browser per test, yes. However, the important part of this exercise is using remote debugging tools for Chrome, with theoretically you could launch on instance of and then run multiple tests against.