Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium HtmlUnitDriver hangs randomly in random places

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("[email protected]");
        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) {
            }
        }
    }
}
like image 734
Rohit Malish Avatar asked Mar 02 '13 22:03

Rohit Malish


1 Answers

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.

like image 109
Łukasz Rzeszotarski Avatar answered Sep 20 '22 17:09

Łukasz Rzeszotarski