Пример #1
0
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())
}
Пример #2
0
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())
}
Пример #3
0
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())
}
Пример #4
0
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())
}
Пример #5
0
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())
}
Пример #6
0
func TestBrowserForm2(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, htmlForm2)
		} 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)

	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())
}
Пример #7
0
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())
}
Пример #8
0
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())
}
Пример #9
0
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())
}