title: tcp/ip port exhaustion with selenium webdriver
body: <p>I'm working on a suite of tests using selenium webdriver (written in Python).  The page being tested contains a form that changes its displayed fields based upon what value is selected in one of its select boxes.  This select box has about 250 options.  I have a test (running via nose, though that's probably irrelevant) that iterates through all the options in the select box, verifying that the form has the correct fields displayed for each selected option.</p>
  
  <p>The problem is that for each option, it calls through selenium:</p>
  
  <ul>
  <li>click for the option to choose</li>
  <li>find_element and is_displayed for 7 fields</li>
  <li>find_elements for the items in the select box</li>
  <li>get_attribute and text for each option in the select box</li>
  </ul>
  
  <p>So that comes out to (roughly) 250 * (7 * 2 + 1 + 2 * 250), or 128,750 distinct requests to the webdriver server the test is running on, all within about 10 or 15 minutes.  This is leading to client port exhaustion on the machine running the test in some instances.  This is all running through a test framework that abstracts away things like how the select box is parsed, when new page objects are created, and a few other things, so optimizations in the test code either mean hacking that all to hell or throwing away the framework for this test and doing everything manually (which, for maintainability of our test code, is a bad idea).</p>
  
  <p>Some ideas I've had for solutions are:</p>
  
  <ul>
  <li>Trying to somehow pool or reuse the connection to the webdriver server</li>
  <li>Somehow tweaking the configuration of <code>urllib2</code> or <code>httplib</code> at runtime so that the connections opened by selenium timeout or are killed more quickly</li>
  <li>System independent (or at least implementable for all systems with an OS switch or some such) mechanism for actively tracking and closing the ports being opened by selenium</li>
  </ul>
  
  <p>As I mentioned above, I can't do much to tweak the way the page is parsed or handled, but I do have control over subclassing/tweaking <code>WebDriver</code> or <code>RemoteConnection</code> any way I please.  Does anyone have any suggestions on how to approach any of the above ideas, or any ideas that I haven't come up with?</p>