Skip to content

How do I click on a row in a JQuery DataTable using HtmlUnit? #316

Description

@grkephart

I wrote the problem in StackOverflow at:
https://stackoverflow.com/questions/42538839/how-do-i-click-on-a-row-in-a-jquery-datatable-using-htmlunit

I have a unit test that can be used which directly accesses my website with an example web page. That source code is below.

(import java.io.IOException;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableBody;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;


/**
 * @author gary_kephart
 *
 */
public class DataTablesTestCase
{
  private static final long   BG_JS_WAIT_MS = 15000;
  private static final String TEST_PAGE_URL = "http://photeus.com/campaigner-data/";
  private WebClient           webClient;


  public HtmlPage getPage(
    String url) throws Exception
  {
    HtmlPage htmlPage = this.webClient.getPage(url);

    this.webClient.waitForBackgroundJavaScriptStartingBefore(BG_JS_WAIT_MS);

    return htmlPage;
  }


  /**
   * @param domElement
   * @return
   * @throws IOException 
   */
  public HtmlPage clickOn(
    DomElement domElement) throws IOException
  {
    HtmlPage htmlPage = domElement.click();

    htmlPage.getWebClient().waitForBackgroundJavaScript(BG_JS_WAIT_MS);

    return htmlPage;
  }


  @SuppressWarnings("unchecked")
  protected <E extends HtmlElement> E getElementById(
    HtmlElement parent,
    String tagName,
    String id)
  {
    return (E)parent.getElementsByAttribute(tagName, "id", id).get(0);
  }
)

  @Before
  public void setUp()
  {
    this.webClient = new WebClient();

    this.webClient.getOptions().setCssEnabled(true);
    this.webClient.getOptions().setJavaScriptEnabled(true);
    this.webClient.getOptions().setTimeout(0);
    this.webClient.getOptions().setUseInsecureSSL(true);
  }


  @Test
  public void testSuccess() throws Exception
  {
    HtmlPage htmlPage = getPage(TEST_PAGE_URL);

    Assert.assertEquals("Campaigner Data - Home", htmlPage.getTitleText());

    HtmlDivision div = getElementById(htmlPage.getBody(), HtmlDivision.TAG_NAME,
      "elections_wrapper");
    HtmlTable table = getElementById(div, HtmlTable.TAG_NAME, "elections");
    List<HtmlTableBody> bodies = table.getBodies();
    HtmlTableBody body = bodies.get(0);
    List<HtmlTableRow> bodyRows = body.getRows();
    HtmlTableRow tableRow = bodyRows.get(0);
    HtmlPage htmlPage2 = clickOn(tableRow);

    Assert.assertEquals("Campaigner Data - HtmlUnit Datatable Row Test", htmlPage2.getTitleText());
    
    HtmlSpan elementIdSpan = getElementById(htmlPage2.getBody(), HtmlSpan.TAG_NAME, "elementId");
    String elementId = elementIdSpan.asText();
    
    Assert.assertNotNull(elementIdSpan);
    Assert.assertNotNull(elementId);
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions