title: Selenium HtmlUnitDriver hangs randomly in random places
body: <p>I used <strong>SeleniumHQ</strong> to record my actions and then exported them to <strong>Java Unity WebDrive</strong>. Then I edited exported code and added many small extra things like looping over array, time-stamps, etc.</p>
  
  <p>My code does following:</p>
  
  <ol>
  <li>Log into my site.</li>
  <li>Goto my profile.</li>
  <li>Delete my previous announcement.</li>
  <li>Post new announcement.</li>
  <li>Log out.</li>
  </ol>
  
  <p>I have tried using <code>FirefoxDriver</code> and <code>HtmlUnitDriver</code>, but every single of them gives me this weird problem. My code start doing its work and randomly stops in random spot and hangs there forever. </p>
  
  <p>For example it could log in -> goto profile -> delete previous and then stop, or it could hang right in the login. I loop over those steps over and over again, and more I loop more likely it is to get stuck. </p>
  
  <p>First loops success rate is 90% second loop is around 40% etc. Also which <code>Driver</code> I use also affects this. It is most likely to hang with <code>HtmlUnitDriver</code> and I would really want to use <code>HtmlUnitDrive</code> because I want to run my code headless on Ubuntu Server.</p>
  
  <p>Has anyone else had similar problems?</p>
  
  <p><strong>EDIT :</strong> Now after many hours of testing, I noticed that its only <code>HtmlUnitDriver</code> that hangs and not Firefox. When using Firefox I can see what it is doing and it is doing everything as it should. Problem occurs with <code>HtmlUnitDriver</code>.</p>
  
  <p>And here is the code itself:</p>
  
  <pre><code>import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.concurrent.TimeUnit;
  import org.junit.*;
  import static org.junit.Assert.*;
  import org.openqa.selenium.*;
  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
  
  public class WebUpdater {
  
      private WebDriver driver;
      private String baseUrl;
      private boolean acceptNextAlert = true;
      private StringBuffer verificationErrors = new StringBuffer();
  
      @Before
      public void setUp() throws Exception {
          driver = new HtmlUnitDriver(true); // JavaScript enabled.
  
          baseUrl = "http://exampleurl.com";
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }
  
      @Test
      public void testUnity() throws Exception {
          openAndLogin();
          gotoProfile();
          deletePreviousPost();
          uploadPost();
          logOut();
          System.out.println("Done!");
      }
  
      private void openAndLogin() {
          driver.get(baseUrl);
  
          driver.findElement(By.linkText("Login")).click();
          driver.findElement(By.id("jsid-login-id")).clear();
          driver.findElement(By.id("jsid-login-id")).sendKeys("bilgeis.babayan@gmail.com");
          driver.findElement(By.id("jsid-login-password")).clear();
          driver.findElement(By.id("jsid-login-password")).sendKeys("volume1991");
          driver.findElement(By.cssSelector("input.right")).click();
  
      }
  
      private void gotoProfile() {
          driver.findElement(By.cssSelector("img[alt=\"Profile\"]")).click();
      }
  
      private void deletePreviousPost() {
          try {
              driver.findElement(By.cssSelector("img[alt=\"ExampleName\"]")).click();
              driver.findElement(By.linkText("Delete")).click();
              assertTrue(closeAlertAndGetItsText().matches("^Confirm to delete this post[\\s\\S]$"));
          } catch (Exception e) {
              System.out.println(e);
          }
      }
  
      private void uploadPost() {
          driver.findElement(By.linkText("ExampleAction")).click();
          driver.findElement(By.id("example_url")).clear();
          driver.findElement(By.id("example_url")).sendKeys("Example text that gets typed in textfield.");
          driver.findElement(By.cssSelector("input[name=\"example\"]")).clear();
          driver.findElement(By.cssSelector("input[name=\"example\"]")).sendKeys("ExampleName");
          driver.findElement(By.linkText("ExampleAction2")).click();
          System.out.println("Done");
      }
  
      private void logOut() {
          driver.get("http://exampleurl.com/logout");
          System.out.println("Logged out.");
      }
  
      @After
      public void tearDown() throws Exception {
          driver.quit();
          String verificationErrorString = verificationErrors.toString();
          if (!"".equals(verificationErrorString)) {
              fail(verificationErrorString);
          }
      }
  
      private boolean isElementPresent(By by) {
          try {
              driver.findElement(by);
              return true;
          } catch (NoSuchElementException e) {
              return false;
          }
      }
  
      private String closeAlertAndGetItsText() {
          try {
              Alert alert = driver.switchTo().alert();
              if (acceptNextAlert) {
                  alert.accept();
              } else {
                  alert.dismiss();
              }
              return alert.getText();
          } finally {
              acceptNextAlert = true;
          }
      }
  }
  </code></pre>
  
  <p>in my main class I call WebUpdater class like this:</p>
  
  <pre><code>import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  
  public class Main {
  
      public static void main(String[] args) {
  
          Logger logger = Logger.getLogger("");
          logger.setLevel(Level.OFF);
  
          scan();
  
      }
  
      private static void scan() {
          while (true) {
              try {
              // Test if connection is available and target url is up.
                  URL url = new URL("http://exampleurl.com");
                  HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
                  urlConn.connect();
  
              // Start tests.
                  WebUpdater updater = new WebUpdater();
                  updater.setUp();
                  updater.testUnity();
                  updater.tearDown();
              } catch (Exception ex) {
                  System.out.println(ex);
              }
              try {
                  Thread.sleep(12000);
              } catch (InterruptedException e) {
              }
          }
      }
  }
  </code></pre>