Blame view

src/test/java/pl/labno/bernard/htmlunified/ScheduleTest.java 4.87 KB
bernard authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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
package pl.labno.bernard.htmlunified;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
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;

public class ScheduleTest {

    //    @Test
    public void accessEventTitles() throws IOException {
        WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
        //login-----------------------------------
        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);
        //----------------------------------------

        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());
        }
        Assert.assertEquals(2, titles.size());
    }

    @Test
    public void clickSuggestionBox() throws IOException {
        WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
        client.setAjaxController(new NicelyResynchronizingAjaxController());
        //login-----------------------------------
        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);
        //----------------------------------------
        final HtmlInput studentInput = (HtmlInput) page.getElementById("searchClass:student_i");
        int initialJobs = client.waitForBackgroundJavaScript(10);

        studentInput.type('w');
        studentInput.type('i');
        Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
//        Here the suggestionBox is still hidden
//        System.out.println(page.asXml());

        int jobs = WebClientUtils.waitForJSJob(client, 0, 30000);
//        Here suggestionBox is visible
//        System.out.println(page.asXml());
        System.out.println(page.getElementById("searchClass:sS").asXml());
        System.out.println("initialJobs:" + initialJobs);
        System.out.println("jobs:" + jobs);
        Assert.assertEquals(client.getCurrentWindow().getEnclosedPage(), page);
         synchronized (page) {
            try {
                page.wait(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        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);
        initialJobs = client.waitForBackgroundJavaScript(10);
        System.out.println("initial jobs:"+initialJobs);
        cell.click();
        System.out.println("Starting jobs:"+client.waitForBackgroundJavaScriptStartingBefore(3000));
        System.out.println("Running jobs:"+client.waitForBackgroundJavaScript(10));
//        studentInput.type('\t');
        jobs = WebClientUtils.waitForJSJob(client,initialJobs, 30000);
        System.out.println("initialJobs:" + initialJobs);
        System.out.println("jobs:" + jobs);
        System.out.println(page.asXml());
        Assert.assertEquals("Willis Bruce",((HtmlElement)page.getByXPath("//*[@id='searchClass:searchStudent']").get(0)).asText());
    }

}