Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver works but SLOW (Java)

I am using Selenium WebDriver to take a screenshot of webpages. It runs great. However, from the time I hit run in eclipse to the time the screenshot shows up in my local drive is 7-10 seconds. Most of the latency seems to be launching Firefox.

Code:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.cnn.com");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\test\\screenshot.png"));

How can I speed up this process? Is there a way that I can use an already opened Firefox browser to save on opening a new one? Is this code somehow heavy?

Details: Tried on CentOS box and Win7 box both using eclipse. myspeedtest.net shows 22Mbps down and 1 Mbps up.

like image 887
Chris Avatar asked Oct 22 '12 22:10

Chris


3 Answers

What you are asking for (allowing WebDriver to attach to a running browser) has been an issue now for almost 3 years. To date, this feature has still not been added. As far as I am aware, there is no way to make Firefox load any faster. You can try a few other tricks though.

fp.setPreference("webdriver.load.strategy", "unstable")

Quoting the caveat from this page:

There is beta feature to make firefox not wait for the full page to load after calling .get or .click. This may cause immediate find's to break, so please be sure to use an implicit or explicit wait too. This is only available for Firefox and not other browsers.

You can also try loading with a profile and see if that helps at all.

like image 95
Mike Kwan Avatar answered Oct 12 '22 21:10

Mike Kwan


From the things I learned elsewhere -

  1. Turn off automatic updates for your browser/plugins
  2. Set your IIS (or equivalent) app timeouts to zero
  3. Create a base Selenium Fixture for use in your tests
  4. Update to the latest version of Selenium
  5. Warm up your apps prior to testing
  6. Short tests
  7. Pre-populate cookies.

References:

https://sqa.stackexchange.com/questions/1988/selenium-reuse-existing-browser-session-instead-of-opening-new-windows

http://www.codeweavers.net/6-ways-to-speed-up-selenium-tests/

http://sauceio.com/index.php/2011/02/speed-up-your-selenium-tests/

Hope this helps!

like image 31
some_other_guy Avatar answered Oct 12 '22 21:10

some_other_guy


This thread shows a possible explanation.

To summarize, Selenium loops through all network cards and calls a method (getHostName) that takes a long time for virtual network cards. Disabling them solved the problem of the OP in the thread.

(In my own case, I am unable to disable any of them and have to wait at the start of each test case...)

like image 33
Timores Avatar answered Oct 12 '22 20:10

Timores