0
    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?

Chris
  • 18,075
  • 15
  • 59
  • 77
  • 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 Nov 15 '13 at 00:14
  • Thank you @Richard how can I force close the browser after 15 sec if it just hangs using Selenium? – Chris Nov 15 '13 at 00:16
  • You could kill the firefox process after n seconds if firefox is still running. – Faiz Nov 15 '13 at 02:01
  • @Faiz could you throw some Java into an answer that does this after n seconds? Thats where Im stumped... – Chris Nov 15 '13 at 03:45
  • @Chris - added an answer with java code to kill process. – Faiz Nov 15 '13 at 05:20

4 Answers4

2

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.

Amey
  • 8,470
  • 9
  • 44
  • 63
  • thank you for this. I dont know how to use junit with java. I thought junit was a way to unit test small pieces of code in eclipse that eventually gets written into larger classes, methods,etc – Chris Nov 15 '13 at 04:24
  • Junit is meant for that, originally atleast, but it can expanded to other levels of testing as well. I would recommend following the note and reading the generated Junit/Java file, the syntax is quite self explanatory. Also, what I have mentioned in the answer, basically indicates that "@after" all tests are complete run this method (which includes `driver.quit()`). Its like a destructor. – Amey Nov 15 '13 at 18:19
2

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.

Faiz
  • 3,216
  • 1
  • 19
  • 25
  • thank you! Im a little stuck on the poll loop part so Ill dig deeper later this evening. – Chris Nov 15 '13 at 18:23
0

To follow up on Ardesco's comment, an example would look as follows:

    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile ff = profile.getProfile("ScreenCapture");
    WebDriver driver = new FirefoxDriver(ff);
    driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
    try {
        driver.get(url);
    } catch (TimeoutException e) {
        System.out.println("15 seconds were over, force continue!");
    } finally {
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        driver.quit();
    }

The try part will run the request but when the timeout time set with pageLoadTimeout has been exceeded an exception is thrown which we catch. The finally part will be run regardless if the requested page was loaded properly in less than 15 seconds or whether an exception was thrown.

Muhammad Tariq
  • 3,318
  • 5
  • 38
  • 42
-1

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

Ardesco
  • 7,281
  • 26
  • 49