func TestBrowserForm(t *testing.T) { ut.Run(t) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { fmt.Fprint(w, htmlForm) } else { r.ParseForm() fmt.Fprint(w, r.Form.Encode()) } })) defer ts.Close() bow := &Browser{} bow.headers = make(http.Header, 10) bow.history = jar.NewMemoryHistory() err := bow.Open(ts.URL) ut.AssertNil(err) f, err := bow.Form("[name='default']") ut.AssertNil(err) f.Input("age", "55") f.Input("gender", "male") err = f.Click("submit2") ut.AssertNil(err) ut.AssertContains("age=55", bow.Body()) ut.AssertContains("gender=male", bow.Body()) ut.AssertContains("submit2=submitted2", bow.Body()) }
func TestHeaders(t *testing.T) { ut.Run(t) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { fmt.Fprint(w, req.Header.Get("X-Testing-1")) fmt.Fprint(w, req.Header.Get("X-Testing-2")) })) defer ts.Close() bow := NewBrowser() bow.AddRequestHeader("X-Testing-1", "Testing-1") bow.AddRequestHeader("X-Testing-2", "Testing-2") err := bow.Open(ts.URL) ut.AssertNil(err) ut.AssertContains("Testing-1", bow.Body()) ut.AssertContains("Testing-2", bow.Body()) }
func TestGet(t *testing.T) { ut.Run(t) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { if req.URL.Path == "/page1" { fmt.Fprint(w, htmlPage1) } else if req.URL.Path == "/page2" { fmt.Fprint(w, htmlPage2) } })) defer ts.Close() bow := NewBrowser() err := bow.Open(ts.URL + "/page1") ut.AssertNil(err) ut.AssertEquals("Surf Page 1", bow.Title()) ut.AssertContains("<p>Hello, Surf!</p>", bow.Body()) err = bow.Open(ts.URL + "/page2") ut.AssertNil(err) ut.AssertEquals("Surf Page 2", bow.Title()) ok := bow.Back() ut.AssertTrue(ok) ut.AssertEquals("Surf Page 1", bow.Title()) ok = bow.Back() ut.AssertFalse(ok) ut.AssertEquals("Surf Page 1", bow.Title()) }
func TestClick(t *testing.T) { ut.Run(t) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { fmt.Fprint(w, htmlPage1) } else if r.URL.Path == "/page2" { fmt.Fprint(w, htmlPage1) } })) defer ts.Close() bow := NewBrowser() err := bow.Open(ts.URL) ut.AssertNil(err) err = bow.Click("a:contains('click')") ut.AssertNil(err) ut.AssertContains("<p>Hello, Surf!</p>", bow.Body()) }
func TestBookmarks(t *testing.T) { ut.Run(t) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { fmt.Fprint(w, htmlPage1) })) defer ts.Close() bookmarks := jar.NewMemoryBookmarks() bow := NewBrowser() bow.SetBookmarksJar(bookmarks) bookmarks.Save("test1", ts.URL) bow.OpenBookmark("test1") ut.AssertEquals("Surf Page 1", bow.Title()) ut.AssertContains("<p>Hello, Surf!</p>", bow.Body()) err := bow.Bookmark("test2") ut.AssertNil(err) bow.OpenBookmark("test2") ut.AssertEquals("Surf Page 1", bow.Title()) }