When you execute your code each time a new ChromeDriver instance is created which spawns a new Chrome Browser session. This functionality is as per design.
A better approach would be to invoke driver.quit()
within tearDown(){}
method to close & destroy both the WebDriver and Web Browser instances gracefully after each run of Test Execution. You can find a detailed discussion here.
Though that would solve the issue of multiple Chrome Browser windows still you won't be logged in within your new session. To get logged in you need to use a specific Chrome Profile and store the cookies and reuse them later. You will find a detailed discussion on how to create and open a new Chrome Profile in this discussion.
To use the extension that you have installed you need to pack/unpack them and use ChromeOptions
class.
Chrome extensions can be either packed or unpacked. Packed extensions are a single file with a .crx extension. Unpacked extensions are a directory containing the extension, including a manifest.json
file.
To pack an unpacked extension, use the Pack button in chrome://extensions
or use Chrome (Windows example):
chrome.exe --pack-extension=C:\path\to\unpacked\extension --pack-extension-key=C:\myext.pem
See the extensions docs for other ways to do this that are more automation friendly. To unpack a packed extension, just unzip the file (you may need to rename the file from .crx
to .zip
for your zip utility to recognize it).
Using extensions through ChromeDriver
Packed (.crx file):
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
WebDriver driver = new ChromeDriver(options);
Unpacked (directory):
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=/path/to/extension");
WebDriver driver = new ChromeDriver(options);