6

I used SeleniumHQ to record my actions and then exported them to Java Unity WebDrive. Then I edited exported code and added many small extra things like looping over array, time-stamps, etc.

My code does following:

  1. Log into my site.
  2. Goto my profile.
  3. Delete my previous announcement.
  4. Post new announcement.
  5. Log out.

I have tried using FirefoxDriver and HtmlUnitDriver, 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.

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.

First loops success rate is 90% second loop is around 40% etc. Also which Driver I use also affects this. It is most likely to hang with HtmlUnitDriver and I would really want to use HtmlUnitDrive because I want to run my code headless on Ubuntu Server.

Has anyone else had similar problems?

EDIT : Now after many hours of testing, I noticed that its only HtmlUnitDriver 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 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 WebUpdater class like this:

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) {
            }
        }
    }
}
Dumpcats
  • 453
  • 5
  • 21
Rohit Malish
  • 3,209
  • 12
  • 49
  • 67
  • Your code looks ok! I'd try the [`pageLoadTimeout()`](http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html#pageLoadTimeout%28long,%20java.util.concurrent.TimeUnit%29) method (if it works, it didn't work in most browsers a few versions ago). My guess is that given the hang-location is random and there's no error thrown, your page is probably not loaded and Selenium waits for it to load all assets. With even one single file left, it will still block and wait. When hung, does Firefox report the page is completely loaded, or not? – Petr Janeček Mar 02 '13 at 23:40
  • Firefox works most of the time its HtmlUnitDriver that hangs most of the time, and since there is no GUI in that one I have to rely on my system out prints. Do you know why it runs first run normally most of the time and then on later runs stops? – Rohit Malish Mar 02 '13 at 23:53
  • Noidea. It might be a good idea to try to debug it and see in which method does it stop and whether it stops in the same method call every time. By the way, you never close your `urlConn`. – Petr Janeček Mar 03 '13 at 00:05
  • I have debugged it, it is totally random when it stops. Thanks for pointing out about urlConn, fixed it. – Rohit Malish Mar 03 '13 at 00:19
  • Even internally? I mean - it can be either `click()` or `sendKeys()`, but internally, in the HtmlUnit, it can still hang in the same method - if you can access the server, that is. Does this issue happen only with this particular page, or does it happen with any other page, too? – Petr Janeček Mar 03 '13 at 00:22
  • Now after many hours of testing, I noticed that its only HtmlUnitDriver 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 HtmlUnitDriver. – Rohit Malish Mar 03 '13 at 09:27
  • What is the test result when the test ends? Also, when you run your code and the test hangs in random spots is there a pattern of where these spots are? As in, are they all within your @Test section? – Nashibukasan Mar 06 '13 at 22:10
  • Why you won't use Firefox Driver instead? Do you really need HtmlUnitDriver? – Łukasz Rzeszotarski Mar 08 '13 at 11:41
  • It wouldn't be difficult to compare ChromeDriver as well, for whatever it's worth. – minopret Mar 11 '13 at 05:29

2 Answers2

1

I had a bad experience with HtmlUnitDriver. Some time ago I wrote testing framework which were supposed to be fired at Hudson, and finally I decided to use Firefox driver which was more predictable and easier to debug. The point is that in my case it was a page full of javascripts - dynamically loaded fields, etc., and working with HtmlUnitDriver was really a can of worms.

If you really need to use HtmlUnitDriver try do debug 'pagesource' which is accessible for Selenium in 'current' (hanging) moment.

Łukasz Rzeszotarski
  • 5,791
  • 6
  • 37
  • 68
1

HtmlUnit 2.11 is flawed (see here and here), and since HtmlUnit 2.12 went live march 6th, the current version of HtmlUnitDriver is probably still based on HtmlUnit 2.11.

If you post your "http://exampleurl.com/" source code (or just give me a working url to the page if it's public) I could run the page with scripts through HtmlUnit 2.12.

OakNinja
  • 2,326
  • 18
  • 20