11

I have a series of Selenium2 test cases in C#/NUnit that are run sequentially. Each test case runs in a new instance of the WebDriver (and this is necessary) - which means that each time a new test case it reached, a new browser window opens over the top of whatever else I'm working on.

Is there any way to configure the WebDriver to automatically open the browser in the background? If I manually place it in the background the tests still run fine... but having to do that every few minutes makes it hard to run the full test and work on something else at the same time.

2
  • This might be useful: stackoverflow.com/questions/5370762/…
    – user246
    Mar 7, 2012 at 15:09
  • Being fairly new to programming (and QA) I was hoping there might be some simple solution within the WebDriver API, and I'd prefer not to use a 3rd-party tool. I guess I might be out of luck though. A useful link - thanks.
    – SteveCZ
    Mar 7, 2012 at 15:33

5 Answers 5

6

A heavier weight solution that may net you more flexibility/control in the end is to run all your tests in a VM on your local machine. With a VM, it's fairly easy to setup snapshots so you can be sure that you're resetting your testing environment to a specific, known point every single test run. That's freakin' nice. You can minimize the VM window and not have it intrude at all.

Also, because the tests are run in a VM and you're using WebDriver, it may be possible to run all the tests concurrently against all your target browsers. (I'm not super familiar with WebDriver and SeleniumServer so I don't know if this can be done. I assume it can.)

1

You may be able to do this by setting driver-specific settings. For instance, let's say you're using Chrome. When you fire up the Chrome driver, you can set Chrome options like so:

DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability("chrome.switches", new List<String>() { 
    "--start-maximized",
    "--disable-popup-blocking" });
driver = new OpenQA.Selenium.Chrome.ChromeDriver(capabilities);

Using this mechanism, you could start Chrome minimized, if such an option exists.

Let me know which browser you're using and I'll see if I can track down window-minimization logic for you.

EDIT:

Here's how to do it in Firefox:

FirefoxProfile profile = new FirefoxProfile();

// See: http://kb.mozillazine.org/About:config_entries for a complete list of profile settings.
profile.SetPreference( "browser.link.open_newwindow.restriction", 1);

// Run driver with this profile this profile:
driver = new OpenQA.Selenium.Firefox.FirefoxDriver(profile);
3
  • Thanks, I'm using Firefox, Chrome and IE9 so I'll give this a try with Chrome and let you know how it goes.
    – SteveCZ
    Mar 12, 2012 at 15:46
  • 1
    I've tried the above both with Chrome and Firefox but unfortunately without success. The Chrome driver doesn't accept DesiredCapabilities as an argument. It does accept ChromeOptions - and I was able to set options that affected the browser (such as --start-maximized), but there isn't an option to minimize or open in background. Thanks for your help though!
    – SteveCZ
    Mar 13, 2012 at 11:25
  • 1
    According to the Mozillazine.org page cited in the code above, setting browser.link.open_newwindow.restriction to 1 means "Let all windows opened by JavaScript open in new windows".
    – Steve HHH
    Feb 1, 2013 at 19:13
1

I've had great success with PhantomJS recently. The only tests that fail are those that involve javascript alerts, but that functionality should be implemented soon anyway.

You can launch PhantomJS with the command

phantomjs.exe --webdriver=4444

And configure selenium to use the remote web driver

var hubUrl = new Uri("http://127.0.0.1:4444/wd/hub");
var capabilities = DesiredCapabilities.PhantomJS();
var driver = new RemoteWebDriver(hubUrl, capabilities);

Then PhantomJS will truck along in the background, never stealing focus from your other work.

1
  • 1
    "should be implemented soon" Ha! :) Almost 3 years later, the issue is still open.
    – Gili
    Nov 25, 2014 at 23:59
0

If you don't care much about the driver you use (Firefox, Chrome, IE), try this one: http://seleniumhq.org/docs/03_webdriver.html#htmlunit-driver

It has no GUI, but uses JS engine different from the browsers listed above, which can result in different behavior and, as a result, in missed or false positive bugs.

0

Having continued to dig through the currently sparse documentation for C# I found that the DriverCommand enum contains the SetBrowserVisible member. It seems as though it's not yet implemented.

This doesn't solve the problem immediately, but it's useful to know...

2
  • 1
    And it won't ever be implemented. It's a legacy left over from when you could start Internet Explorer and not allow it to become visible. Microsoft has made that scenario impossible with the advent of Protected Mode in Windows Vista/IE7. Incidentally, if you'd like to contribute documentation to the project, we happily accept patches.
    – JimEvans
    Mar 28, 2012 at 17:52
  • Thanks - that's useful to know. I'll certainly look into contributing some documentation too.
    – SteveCZ
    Mar 29, 2012 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.