Blame view

src/test/java/pl/labno/bernard/htmlunified/ScheduleTest.java 7.24 KB
bernard authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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;
bernard authored
18 19 20
/**
 * NicelyResynchronizingAjaxController is EVIL! Don't use it!
 */
bernard authored
21 22
public class ScheduleTest {
bernard authored
23
    @Test
bernard authored
24 25
    public void accessEventTitles() throws IOException {
        WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
bernard authored
26 27
        HtmlPage page = login(client);
        @SuppressWarnings("unchecked")
bernard authored
28 29 30 31 32
        final List<HtmlSpan> titleElements = (List<HtmlSpan>) page.getElementById("results:schedule").getByXPath(".//span[@class='fc-event-title']");
        List<String> titles = new ArrayList<String>(titleElements.size());
        for (HtmlSpan o : titleElements) {
            titles.add(o.asText());
        }
bernard authored
33
        Assert.assertEquals(6, titles.size());
bernard authored
34 35 36
    }

    @Test
bernard authored
37
    public void clickStudentSuggestionBox() throws IOException {
bernard authored
38
        WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
bernard authored
39 40 41 42
        HtmlPage page = login(client);
        page.addDomChangeListener(new DomChangeLogger());
        page.getElementById("searchClass:sS").addHtmlAttributeChangeListener(new HtmlAttributeChangeLogger());
        HtmlInput studentInput = (HtmlInput) page.getElementById("searchClass:student_i");
bernard authored
43 44 45 46
        studentInput.type('w');
        studentInput.type('i');
        Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
//        Here the suggestionBox is still hidden
bernard authored
47
        WebClientUtils.waitForJSJob(client, 0, 30000);
bernard authored
48 49 50 51 52 53 54 55
//        Here suggestionBox is visible
        Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
        Map<String, HtmlTableCell> 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();
bernard authored
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        WebClientUtils.waitForJSJob(client, 0, 30000);
        Assert.assertEquals("Willis Bruce", ((HtmlElement) page.getByXPath("//*[@id='searchClass:searchStudent']").get(0)).asText());
        System.out.println("Success!");


        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);
//        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<String, HtmlTableCell> 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");
bernard authored
139
        ((HtmlInput) page.getElementById("loginForm:email")).setValueAttribute("it.crowd.test@gmail.com");
bernard authored
140 141 142 143 144 145 146
        ((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;
bernard authored
147 148 149
    }

}