package pl.labno.bernard.htmlunified;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* NicelyResynchronizingAjaxController is EVIL! Don't use it!
*/
public class ScheduleTest {
@Test
public void accessEventTitles() throws IOException {
WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
HtmlPage page = login(client);
@SuppressWarnings("unchecked")
final List titleElements = (List) page.getElementById("results:schedule").getByXPath(".//span[@class='fc-event-title']");
List titles = new ArrayList(titleElements.size());
for (HtmlSpan o : titleElements) {
titles.add(o.asText());
}
Assert.assertEquals(4, titles.size());
}
@Test
public void clickStudentSuggestionBox() throws IOException {
WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
HtmlPage page = login(client);
page.addDomChangeListener(new DomChangeLogger());
page.getElementById("searchClass:sS").addHtmlAttributeChangeListener(new HtmlAttributeChangeLogger());
HtmlInput studentInput = (HtmlInput) page.getElementById("searchClass:student_i");
studentInput.type('w');
studentInput.type('i');
Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
// Here the suggestionBox is still hidden
WebClientUtils.waitForJSJob(client, 0, 30000);
// Here suggestionBox is visible
Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
Map suggestions = WebClientUtils.getSuggestions(page.getElementById("searchClass:sS"), 0);
Assert.assertEquals(1, suggestions.size());
System.out.println(suggestions);
HtmlTableCell cell = suggestions.get("Willis Bruce");
Assert.assertNotNull(cell);
cell.click();
WebClientUtils.waitForJSJob(client, 0, 30000);
Assert.assertEquals("Willis Bruce", ((HtmlElement) page.getByXPath("//*[@id='searchClass:searchStudent']").get(0)).asText());
System.out.println("Success!");
// page = (HtmlPage) client.getPage("http://localhost:8080/schoolmanager/view/class/current.seam?networkId=salsafactory");
WebClientUtils.waitForJSJob(client, 0, 30000);
Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
studentInput = (HtmlInput) page.getElementById("searchClass:student_i");
studentInput.setValueAttribute("");
studentInput.type('l');
studentInput.type('i');
Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
// WebClientUtils.forceWait(2000);
// Here the suggestionBox is still hidden
WebClientUtils.waitForJSJob(client, 0, 30000);
// Here suggestionBox is visible
Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
suggestions = WebClientUtils.getSuggestions(page.getElementById("searchClass:sS"), 0);
Assert.assertEquals(1, suggestions.size());
System.out.println(suggestions);
cell = suggestions.get("Linda Bogusław");
Assert.assertNotNull(cell);
cell.click();
WebClientUtils.waitForJSJob(client, 0, 30000);
Assert.assertEquals("Linda Bogusław", ((HtmlElement) page.getByXPath("//*[@id='searchClass:searchStudent']").get(0)).asText());
System.out.println("Success2!");
}
@Test
public void switchBetwenScheduleAndTableMode() throws IOException {
WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
HtmlPage page = login(client);
page.getElementById("j_id2118:j_id2119:0").click();
page.getElementById("j_id2118:j_id2119:0").blur();
WebClientUtils.waitForJSJob(client, 0, 30000);
// System.out.println(page.getElementById("results").asXml());
Assert.assertNotNull(page.getElementById("results:table"));
page.getElementById("j_id2118:j_id2119:1").click();
page.getElementById("j_id2118:j_id2119:1").blur();
WebClientUtils.waitForJSJob(client, 0, 30000);
// System.out.println(page.getElementById("results").asXml());
Assert.assertNotNull(page.getElementById("results:schedule"));
page.getElementById("j_id2118:j_id2119:0").click();
page.getElementById("j_id2118:j_id2119:0").blur();
WebClientUtils.waitForJSJob(client, 0, 30000);
// System.out.println(page.getElementById("results").asXml());
Assert.assertNotNull(page.getElementById("results:table"));
}
@Test
public void selectStudentSuggestionByTab() throws IOException {
final WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
final RequestResponseLogger requestResponseLogger = new RequestResponseLogger(client);
requestResponseLogger.off();
HtmlPage page = login(client);
final HtmlInput studentInput = (HtmlInput) page.getElementById("searchClass:student_i");
studentInput.type('w');
studentInput.type('i');
Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
// Here the suggestionBox is still hidden
WebClientUtils.waitForJSJob(client, 0, 30000);
// Here suggestionBox is visible
Map suggestions = WebClientUtils.getSuggestions(page.getElementById("searchClass:sS"), 0);
Assert.assertEquals(1, suggestions.size());
// System.out.println(suggestions);
HtmlTableCell cell = suggestions.get("Willis Bruce");
Assert.assertNotNull(cell);
studentInput.type('\t');
WebClientUtils.waitForJSJob(client, 0, 30000);
Assert.assertEquals("Willis Bruce", page.getElementById("searchClass:searchStudent").asText());
// requestResponseLogger.on();
// client.setAlertHandler(new AlertLogger());
// page.addDomChangeListener(new DomChangeLogger());
final HtmlElement element = page.getElementById("searchClass:searchStudent").getElementsByTagName("img").get(0);
element.click();
WebClientUtils.waitForJSJob(client, 0, 30000);
Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
Assert.assertEquals("", page.getElementById("searchClass:searchStudent").asText());
}
private HtmlPage login(WebClient client) throws IOException {
HtmlPage page = (HtmlPage) client.getPage("http://localhost:8080/schoolmanager/view/class/current.seam?networkId=salsafactory");
((HtmlInput) page.getElementById("loginForm:email")).setValueAttribute("s4237@pjwstk.edu.pl");
((HtmlInput) page.getElementById("loginForm:password")).setValueAttribute("aaaaa");
HtmlPage oldPage = page;
page = page.getElementById("loginForm:submit").click();
// This assert would fail (when page redidrects than you need to obtain new page)
// Assert.assertEquals(page,oldPage);
Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
return page;
}
}