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 newDefaultTestBrowser() *Browser { bow := &Browser{} bow.SetUserAgent(agent.Create()) bow.SetState(&jar.State{}) bow.SetCookieJar(jar.NewMemoryCookies()) bow.SetBookmarksJar(jar.NewMemoryBookmarks()) bow.SetHistoryJar(jar.NewMemoryHistory()) bow.SetHeadersJar(jar.NewMemoryHeaders()) bow.SetAttributes(AttributeMap{ SendReferer: true, MetaRefreshHandling: true, FollowRedirects: true, }) return bow }
// NewBrowser creates and returns a *browser.Browser type. func NewBrowser() *browser.Browser { bow := &browser.Browser{} bow.SetUserAgent(DefaultUserAgent) bow.SetState(&jar.State{}) bow.SetCookieJar(jar.NewMemoryCookies()) bow.SetBookmarksJar(jar.NewMemoryBookmarks()) bow.SetHistoryJar(jar.NewMemoryHistory()) bow.SetHeadersJar(jar.NewMemoryHeaders()) bow.SetAttributes(browser.AttributeMap{ browser.SendReferer: DefaultSendReferer, browser.MetaRefreshHandling: DefaultMetaRefreshHandling, browser.FollowRedirects: DefaultFollowRedirects, }) return bow }