1

Hello friends. I would like some help.

I've been trying to use the same instance of Firefox window open by Visual Studio at first time.

The question is, how to pickup the same window browser and continue to testing without it close the browser and open again every time i want to do a testing?

I know there is a lot of questions about this topic, but some that I've found is applicable for old versions of selenium and some codes didn't work due to be deprecated.

What i can do with success:

I can work normally with traditional method to test a unit, opening Firefox at beginning and close after the end of test:

[ClassInitialize]
public static void InitializeClass(TestContext testContext) {

    driver = new FirefoxDriver();
}

Then i commented the following lines to keep the window open when test is complete:

//driver.Quit();
//driver.Close();
//driver.Dispose();

What i can't manage to:

With searches I've been made, i read that we can connect to an existing session already open before. So i tried with the following:

[ClassInitialize]
public static void InitializeClass(TestContext testContext) {


    FirefoxOptions options = new FirefoxOptions();
    driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options);

    //driver = new FirefoxDriver();

On the example above, I've commented driver = new FirefoxDriver(); and used the method FirefoxOptions instead of the deprecated DesiredCapabilities.firefox() and when i try to connect to the open firefox window, i got an error and i can't go on. On log, there's a line saying:

Was'n possible to copy the file "..\packages\Selenium.Firefox.WebDriver.0.27.0\build\driver\geckodriver.exe" to "..\bin\debug\geckodriver.exe"

The file geckodriver.exe (61484) is being used by another process

With this, i tried another different ways to reach what i expect like:

[ClassInitialize]
public static void InitializeClass(TestContext testContext) {
    
    
     FirefoxOptions options = new FirefoxOptions();
     driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options);
    
               
     options.AddArgument("--marionette-port " + "2828" + "--connect-existing");
     FirefoxDriverService ffDriverService = FirefoxDriverService.CreateDefaultService();           
     ffDriverService.BrowserCommunicationPort = 4444;
}

for last i tried to open a separated instance of Firefox to try to connect to like below:

firefox.exe --marionette -start-debugger-server 2828 -profile %temp%\FirefoxTEMP

I got success on open a instance of marionette,but i continuing unable to attach the test to this open window.

I appreciate your attention and forgive me for some newbie techniques i made.

Questions I've read:

How to connect Selenium to existing Firefox browser? (Python)

How to connect Selenium Webdriver to existing Firefox/Chrome browser session?

How to connect to an already open browser?

edit:

take a look on this thread to understand that it's a common request:

Allow webdriver to attach to a running browser - GitHub

R4wd0G
  • 41
  • 1
  • 1
  • 6
  • Are you trying to open the browser at the beginning of a test run and then close it at the end of the test run? That is a different (and answerable) question. – Greg Burghardt Sep 06 '22 at 12:59
  • Also, as soon as you dispose of a web driver, it kills the browser. – Greg Burghardt Sep 06 '22 at 13:01
  • Hello Greg. I'm trying to open the browser at the beginning as you said, then closing just when i want by manual form. Thus i have more flexibility keeping the window open if i want to make some fast modification on the webpage or even to access another page in a fast way to make another test. – R4wd0G Sep 06 '22 at 15:19
  • Ok. Which unit testing framework are you using? – Greg Burghardt Sep 06 '22 at 15:47
  • So your *real* question is how to reuse the same browser window for multiple tests. – Greg Burghardt Sep 06 '22 at 15:55
  • Are you running these tests concurrently? – Greg Burghardt Sep 06 '22 at 15:56
  • 1
    "Ok. Which unit testing framework are you using?" : MSTest. "So your real question is how to reuse the same browser window for multiple tests.": yes. "Are you running these tests concurrently?": No, i'm doing some simple tests once per time. – R4wd0G Sep 10 '22 at 13:10
  • Good information. I can write an answer now, but it probably won't be until Monday. In the meantime, try initializing the web driver as a static field in a [TestInitialize] method. Don't re-initialize it if the field is not null. That should spawn one browser window power test run. – Greg Burghardt Sep 10 '22 at 15:38

1 Answers1

0
  1. You can use "detach" and change Firefox to Chrome(because I didn't see that option in Firefox). Link to link. Shortly, it keeps your browser open depending on the option "true"/"false".

    ChromeOptions options = new ChromeOptions();
    
    options.setExperimentalOption("detach", true);
    
  2. You can use bad habits and simply use a global variable. global browser

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92