ClassDetailsTest.java 5.54 KB
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 org.junit.Assert;
import org.junit.Test;

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

public class ClassDetailsTest {

    @Test
    public void openDetailsModalPanel() throws IOException {
        WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
        HtmlPage page = login(client);
        @SuppressWarnings("unchecked")
        final List<HtmlSpan> titleElements = (List<HtmlSpan>) page.getElementById("results:schedule").getByXPath(".//span[@class='fc-event-title']");
        Assert.assertNotSame(0, titleElements.size());
        new RequestResponseLogger(client);
        page.addDomChangeListener(new DomChangeLogger());
        WebClientUtils.waitForJSJob("initial javascript to finish", client, 0, 30000);
        System.out.println("item:" + titleElements.get(0).asText());
        ((HtmlElement) titleElements.get(0).getParentNode()).mouseOver();
        ((HtmlElement) titleElements.get(0).getParentNode()).click();
        WebClientUtils.waitForJSJob("classDetailsModalPanel shown after schedule event clicked", client, 0, 30000);
        Assert.assertNotNull(page.getElementById("classDetailsModalPanelContainer"));
    }

    @Test
    public void closeDetailsModalPanel() throws IOException {
        WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
        HtmlPage page = login(client);
        @SuppressWarnings("unchecked")
        final List<HtmlSpan> titleElements = (List<HtmlSpan>) page.getElementById("results:schedule").getByXPath(".//span[@class='fc-event-title']");
        Assert.assertNotSame(0, titleElements.size());
//        new RequestResponseLogger(client);
//        page.addDomChangeListener(new DomChangeLogger());
        WebClientUtils.waitForJSJob("initial javascript to finish", client, 0, 30000);
        System.out.println("item:" + titleElements.get(0).asText());
        ((HtmlElement) titleElements.get(0).getParentNode()).mouseOver();
        ((HtmlElement) titleElements.get(0).getParentNode()).click();
        WebClientUtils.waitForJSJob("classDetailsModalPanel shown after schedule event clicked", client, 0, 30000);
        Assert.assertNotNull(page.getElementById("classDetailsModalPanelContainer"));
        ((HtmlElement) page.getElementById("classDetailsModalPanelContainer").getByXPath("//*[contains(@src,'close.png')]").get(0)).click();
        WebClientUtils.waitForJSJob("classDetailsModalPanel hidden after cancel button", client, 0, 30000);
        Assert.assertNull(page.getElementById("classDetailsModalPanelContainer"));
    }

    @Test
    public void removeClass() throws IOException {
        WebClient client = new WebClient(BrowserVersion.FIREFOX_2);
        final RequestResponseLogger requestResponseLogger = new RequestResponseLogger(client);
        requestResponseLogger.off();
        HtmlPage page = login(client);
        @SuppressWarnings("unchecked")
        final List<HtmlSpan> titleElements = (List<HtmlSpan>) page.getElementById("results:schedule").getByXPath(".//span[@class='fc-event-title']");
        Assert.assertEquals(6, titleElements.size());
        Assert.assertNotSame(0, titleElements.size());
        WebClientUtils.waitForJSJob("initial javascript to finish", client, 0, 30000);

        client.setAlertHandler(new AlertLogger());
        final DomChangeLogger domChangeLogger = new DomChangeLogger();
        domChangeLogger.showDetails("results:j_id2158");
//        page.addDomChangeListener(domChangeLogger);
//        page.addHtmlAttsributeChangeListener(new HtmlAttributeChangeLogger());
//        requestResponseLogger.on();

        ((HtmlElement) titleElements.get(0).getParentNode()).mouseOver();
        ((HtmlElement) titleElements.get(0).getParentNode()).click();
        WebClientUtils.waitForJSJob("classDetailsModalPanel shown after schedule event clicked", client, 0, 30000);
        WebClientUtils.executeAjaxReRenderedScripts(page);
        Assert.assertNotNull(page.getElementById("classDetailsModalPanelContainer"));
        ((HtmlElement) page.getElementById("classDetailsModalPanelContainer").getByXPath("//*[contains(@src,'remove.png')]").get(0)).click();
        WebClientUtils.waitForJSJob("classDetailsModalPanel hidden after remove button", client, 0, 30000);
        WebClientUtils.executeAjaxReRenderedScripts(page);
        WebClientUtils.waitForJSJob("schedule refresh", client, 0, 30000);
        Assert.assertNull(page.getElementById("classDetailsModalPanelContainer"));
        Assert.assertEquals(5, page.getElementById("results:schedule").getByXPath(".//span[@class='fc-event-title']").size());
    }

    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("it.crowd.test@gmail.com");
        ((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;
    }
}