func TestBrowserForm(t *testing.T) { ts := setupTestServer(htmlForm, t) 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) v, ok := f.Field("age") ut.AssertTrue(ok) ut.AssertEquals("", v) v, ok = f.Field("ageage") ut.AssertFalse(ok) ut.AssertEquals("", v) f.Input("age", "55") v, ok = f.Field("age") ut.AssertTrue(ok) ut.AssertEquals("55", v) 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 }
func TestBrowserFormClickByValue(t *testing.T) { ts := setupTestServer(htmlFormClick, t) 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") err = f.ClickByValue("submit", "submitted2") ut.AssertNil(err) ut.AssertContains("age=55", bow.Body()) ut.AssertContains("submit=submitted2", bow.Body()) }
func TestBrowserForm2(t *testing.T) { ts := setupTestServer(htmlForm2, t) 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) err = f.Input("age", "54") ut.AssertNil(err) err = f.Input("agee", "54") ut.AssertNotNil(err) err = f.CheckBox("music", []string{"rock", "fusion"}) ut.AssertNil(err) err = f.CheckBox("music2", []string{"rock", "fusion"}) ut.AssertNotNil(err) err = f.Click("submit2") ut.AssertNil(err) ut.AssertContains("company=none", bow.Body()) ut.AssertContains("age=54", bow.Body()) ut.AssertContains("gender=male", bow.Body()) ut.AssertFalse(strings.Contains(bow.Body(), "music=jazz")) ut.AssertContains("music=rock", bow.Body()) ut.AssertContains("music=fusion", bow.Body()) ut.AssertContains("hobby=Dance", bow.Body()) ut.AssertContains("city=NY", bow.Body()) ut.AssertContains("submit2=submitted2", bow.Body()) }