0
votes
    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile ff = profile.getProfile("ScreenCapture");
    WebDriver driver = new FirefoxDriver(ff);
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    driver.get(url);

    Thread.sleep(8000);

    File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

    driver.quit();

Shouldn't driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); force a close of the selenium generated Firefox browser after 15 seconds? The browser just sits and says its transferring data for an hour+. Basically just hangs saying its transferring...

I am capturing ajax heavy pages which is why Im asking everything to wait for 8 seconds after page loads. But that should have nothing to do with the driver forcing a close after 15 seconds.

Where am I going wrong?

Details: Centos x64 6.4 with Firefox 10.0.12 and latest Selenium as of 10 min ago.

Is there something I can do in Java to go around this?

Question: How can I force close the Selenium generated Firefox window after N seconds?

3
The implicit wait functionality is meant to force Selenium to wait (in your case 15 seconds) for an element to appear when using findElement to locate it. In the case of the code you're showing though, there aren't any findElement calls being made. This should create a new firefox instance, wait 8 seconds, take a screenshot, exit firefox, then exit the driver. - Richard
Thank you @Richard how can I force close the browser after 15 sec if it just hangs using Selenium? - Chris
You could kill the firefox process after n seconds if firefox is still running. - Faiz
@Faiz could you throw some Java into an answer that does this after n seconds? Thats where Im stumped... - Chris
@Chris - added an answer with java code to kill process. - Faiz

3 Answers

2
votes

If you use Junit along with Java, then some thing like this :-

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

Note :- To get a full skeleton of how it should be written just download the selenium IDE for FF and export some test case to Java /jUnit.

2
votes

My linux knowledge is limited, but you can kill a process by running the linux command pkill.

driver.quit();
Thread.sleep(15000); //use a poll loop instead to check process running every 1 sec 
Runtime rt = Runtime.getRuntime();
rt.exec("pkill firefox"); 

I think that the java process will need to have enough permissions to kill a process, but haven't tried it.

-1
votes

Implicit waits will not force a close of a browser after 15 seconds.

Implicit waits are used when trying to find elements in the DOM, they are not used when trying to load a page. If you want Selenium to stop trying to load a page after 15 seconds you will need to set a pageLoadTimeout, it's used like this:

driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);

The default page load timeout is 0 (which means wait indefinitely), hence the behaviour that you are seeing.

(There is obviously an assumption here that the driver binary you are using has implemented this method.)

The JavaDoc for timeouts in Selenium is available Here