1. debugId for Vaadin component need to be set such as
Button :
saveButton.setDebugId("DenormalizedProductFilterForm_save");
Select :
sl_fetchSize.setDebugId("DenormalizedProductFilterForm_sl_fetchSize");
2. We need to use waitForBackgroundJavaScriptStartingBefore method to wait for DOM components .
3. Sometimes, click() does not do the job. We need to use {e.click(); e.mouseDown(); e.mouseUp();} or {div.dblClick();} to simulate click.
Sample code
package com.wapice.epid.test;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.util.logging.Level;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class TestSearchProduct implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Before
public void before() {
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.NoOpLog");
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit")
.setLevel(Level.OFF);
java.util.logging.Logger.getLogger("org.apache.commons.httpclient")
.setLevel(Level.OFF);
}
public void testWithStress() {
}
@Test
public void test() {
final WebClient webClient = new WebClient();
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
HtmlPage page;
try {
System.out.println("Starting test");
page = webClient
.getPage("http://localhost:1123/someapp/application/");
// login
{
HtmlForm form = page.getFormByName("loginform");
HtmlSubmitInput button = form.getInputByName("submitButton");
HtmlTextInput userName = form.getInputByName("j_username");
userName.setValueAttribute("jaacco");
HtmlPasswordInput password = form.getInputByName("j_password");
password.setValueAttribute("jaacco");
button.click();
}
// search for product
{
page = webClient
.getPage("http://localhost:1123/someapp/application/#listdenormalizedproductview");
webClient.waitForBackgroundJavaScriptStartingBefore(5000);
//select from Select
{
HtmlDivision div = page
.getHtmlElementById("DenormalizedProductFilterForm_sl_fetchSize");
for (HtmlElement e : div.getElementsByTagName("div")) {
e.click();
}
webClient.waitForBackgroundJavaScriptStartingBefore(1000);
div = page.getHtmlElementById("VAADIN_COMBOBOX_OPTIONLIST");
for (HtmlElement e : div.getElementsByTagName("td")) {
if (e.asText().contains("200")) {
e.click();
e.mouseDown();
e.mouseUp();
break;
}
}
webClient.waitForBackgroundJavaScriptStartingBefore(1000);
}
//click button
{
HtmlDivision div = page
.getHtmlElementById("DenormalizedProductFilterForm_save");
div.dblClick();
}
webClient.waitForBackgroundJavaScriptStartingBefore(10000);
}
} catch (FailingHttpStatusCodeException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
webClient.closeAllWindows();
}
}