testsigma
left-mobile-bg

Executing Tests on Existing Chrome Browser Instances

June 26, 2023
Gayathri Rukmadhavan
right-mobile-bg
Executing Tests on existing Chrome Browser Instance
image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free

When do we want to run tests on existing browser instances?

There are many scenarios where we want Selenium to connect to and use an existing browser that was previously opened manually or by some other program.

Some of the typical use cases are as below.

  • When the user does not want to log in every time, they add a test step while drafting the test case or when they want a plugin to be added in the automation-controlled browser, the Chrome browser launched using the Selenium web driver will not have their log-in information or plugins that they might have installed.
  • When the user has logged into an account that requires two-factor authentication.
  • When the user has done a sequence of manual operations that might be hard to automate have already been done.
  • When the application has a bug, and the user does a manual intervention to bypass a few steps.

Example

The sample sequence of test steps to add a beneficiary and make a transaction are

  1. Go to the net banking website login page.
  2. Enter username and password.
  3. Go to the money transfer page.
  4. Add the beneficiary details.
  5. Verify the details of the bank account and person.
  6. Select from the various methods of online money transfer including RTGS, IMPS, NEFT, UPI, and digital wallet.
  7. Select the beneficiary name, and the amount, and then submit the details.
  8. Verify the amount and other information on the verification page.
  9. Click submit for the final action of the fund transfer.

If you want to add an assertion case where you want to verify the text message displayed after a transaction, you will be looking for an option to run ONLY the test where you assert the success/failure message displayed on the page without repeating the entire test steps from logging into the website.

The default behavior in Selenium is that a new driver instance will be initiated every time when you write a new test. In the above example, if you add the 10th test step to an existing test or write a new test in Selenium, you will be able to verify it only by running from step 1.

You can run only the assertion use case from the above example in a previously opened chrome browser where the transaction case is tested either manually or using selenium script when the browser is opened in debugging mode.

When the Chrome browser application is opened in a remote debugger port, we could easily connect a new Selenium Chrome Driver to that existing chrome instance.

Prerequisites

  1. Chrome Browser — Version 63 or higher
  2. Add Chrome Browser installation directory to Path variable (if you are using windows) — preferable as it would allow opening chrome browser from any directory using terminal/command prompt.

How to launch Chrome with remote debugging enabled?

ChromeDriver will communicate with Chrome via the remote DevTools Protocol, which is not usually enabled when you open Chrome from the list of applications in the system.

So, you will need to enable the protocol by starting Chrome with the –remote-debugging-port command-line switch and choosing a port for it. Port 9222 is used by convention, but you can use any non-reserved port.

On Windows, you can run from the command prompt by appending a custom flag:

or

when the path is not set.

On macOS, you will need to find the path to the Chrome executable. Here’s an example to launch a Chrome executable that is in the default path:

On Linux, launch the browser by running google-chrome --remote-debugging-port=9222

To launch Chrome with the remote DevTools protocol enabled without the need to use the command line:

  • On Windows,

    • Right-click the Chrome shortcut, and select Properties
    • In the “target” field, append --remote-debugging-port=9222
  • On macOS, either of the 3 ways mentioned below works

    • Create an Automator workflow that runs a bash script to open the browser with the command line switch enabled.
    • Use the .dmg file that you can obtain from this GitHub repository.
    • You could create your executable with a tool like Platypus.

If you want to launch the browser using a custom chrome profile, you can do it by adding --user-data-dir flag when you launch the browser.

To enable Chrome to open a port for remote debugging in the new chrome profile, we need to launch it with a custom flag.

On Windows,

On Mac,

For --user-data-dir flag you can either pass the directory where a new folder will be created for storing details of the chrome profile or you can create a folder and pass it as a value for the flag. You can launch the browser in a new profile when you don’t want to pollute your default profile.

How to Connect to an Existing Chrome Browser from Selenium?

Here is the sample snippet to initialize Chrome Driver in Selenium with Java/Python1//Sample Java

*Replace remote-port with the port where you have launched chrome with the remote DevTools protocol enabled

A working example in a sample application is below —

Testsigma Author - Gayathri Rukmadhavan

Gayathri Rukmadhavan

Gayathri is a passionate test engineer with 10+ years of experience in Quality Assurance, Software Testing, Test Automation, and Team management. Expertise in testing and automating web applications, APIs, android and iOS mobile applications using different tools and frameworks. Knowledge in security testing, performance testing and tuning.

image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free
imageimage
Subscribe to get all our latest blogs, updates delivered directly to your inbox.

RELATED BLOGS


Scriptless Test Automation | What , Why it Matters & Examples
TESTSIGMA ENGINEERING TEAM
TEST AUTOMATION
Top 6 Game Testing Tools You Need to Know
TESTSIGMA ENGINEERING TEAM
TEST AUTOMATION
POC in Testing | What , Why it Matters & How to Perform ?
VIJAYARAGHAVAN VASUDEVAN
AUTOMATION TESTINGTEST AUTOMATION