Selenium HtmlUnitDriver randomly freezes in random places

I used SeleniumHQ to record my actions and then exported them to Java Unity WebDrive . Then I edited the exported code and added a lot of small additional things like looping through an array, timestamps, etc.

My code does the following:

  • Log in to my site.
  • Go to my profile.
  • Delete my previous ad.
  • Post a new ad.
  • Sign Out.

I tried using FirefoxDriver and HtmlUnitDriver , but each of them gives me this strange problem. My code starts to do its work and accidentally stops in a random place and hangs there forever.

For example, it can log in → goto profile → delete the previous one, and then stop it, or it can hang right in the login. I repeat these steps over and over, and, moreover, I often get hung up on.

The success rate of the first cycles is 90%, the second loop is about 40%, etc. In addition, this affects Driver . Most likely, it will hang with HtmlUnitDriver , and I really would like to use HtmlUnitDrive , because I want to run my code without a header on an Ubuntu server.

Has anyone had similar problems?

EDIT: Now, after many hours of testing, I noticed that its only HtmlUnitDriver that hangs, not Firefox. When using Firefox, I can see what it does, and does whatever it wants. The problem occurs when HtmlUnitDriver .

And here is the code itself:

 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; } } } 

in my main class, I call the WebUpdater class as follows:

 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) { } } } } 
+6
source share
2 answers

I had a bad experience with HtmlUnitDriver. Some time ago, I wrote a test framework that was supposed to be running in Hudson, and finally I decided to use the Firefox driver, which was more predictable and easier to debug. The fact is that in my case it was a page with javascripts - dynamically loaded fields, etc., and working with HtmlUnitDriver was really a worm worm.

If you really need to use HtmlUnitDriver, try debugging the "sourceource", which is available to Selenium in the "current" moment.

+1
source

HtmlUnit 2.11 is erroneous (see here and here ), and since HtmlUnit 2.12 aired on March 6th, the current version of HtmlUnitDriver is probably still based on HtmlUnit 2.11.

If you publish your " http://exampleurl.com/ source code (or just give me a working url on the page if it is open), I could start the script page through HtmlUnit 2.12.

+1
source

All Articles