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 scrape(page string, p patron) (items []an_item) { bow := surf.NewBrowser() historyJar := jar.NewMemoryHistory() bow.SetHistoryJar(historyJar) // grab main page and click to login page err := bow.Open(page) if err != nil { panic(err) } fmt.Println(bow.Title()) err = bow.Click("a:contains('My Account')") if err != nil { panic(err) } fmt.Println(bow.Title()) // populate login form and submit form, err := bow.Form("form[action='/Mobile/MyAccount/Logon']") if err != nil { panic(err) } fmt.Println("logging in", p.Name) form.Input("barcodeOrUsername", p.Card) form.Input("password", p.Pin) fmt.Println(form) err = form.Submit() if err != nil { panic(err) } items_out := bow.Find("a[href='/Mobile/MyAccount/ItemsOut']") num_items_out, err := strconv.Atoi(strings.Split(items_out.Text(), " ")[0]) if err != nil { panic(err) } if num_items_out > 0 { items = getItemsOut(bow) return items } return nil }
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()) }