diff --git a/browser.go b/browser.go index 01d70b3..8d926a2 100644 --- a/browser.go +++ b/browser.go @@ -203,6 +203,20 @@ func (b *Browser) TakeScreenshot(name string) *Browser { return b } +func (b *Browser) TakeSource(name string) *Browser { + b.Helper() + str, err := b.session.Source() + if err != nil { + b.Fatal(err) + } + + err = ioutil.WriteFile(name, []byte(str), 0644) + if err != nil { + b.Fatal(err) + } + return b +} + func (b *Browser) ExpectTransitTo(rawurl string) *Browser { b.Helper() expect, err := url.Parse(rawurl) diff --git a/example/example_test.go b/example/example_test.go index 1e1f15f..9bacf17 100644 --- a/example/example_test.go +++ b/example/example_test.go @@ -25,15 +25,19 @@ func TestSimple(tt *testing.T) { defer t.TearDown() d := t.OpenBrowser() - d.SetPageLoadTimeout(10 * time.Second) + d.SetPageLoadTimeout(4 * time.Second) + + d.VisitTo("https://tour.golang.org/welcome/1") + // ngのレンダリングを待たなければrunの結果が出てこない + d.TakeSource("./before.html") + time.Sleep(2 * time.Second) + d.TakeSource("./after.html") + + d.WaitFor("id:run") + d.MustFindElement("id:run").Click() - d.VisitTo("https://tour.golang.org/") - d.WaitFor("id:run").Element().Click() d.WaitFor("class:stdout") d.MustFindElement("class:stdout").VerifyText(strings.Contains, "Hello") - //d.MustFindElements("class:stdout").Verify(func(e *Element) { - // strings.Contains("Hello") - //}) d.MustFindElement("class:next-page").Click() d.ExpectTransitTo("/welcome/2").TakeScreenshot("page2.png") @@ -47,7 +51,7 @@ import ( ) func main() { - fmt.Println("Hello, 世界!") + fmt.Println("Hello, go世界!") } ` @@ -56,7 +60,7 @@ func TestPlayground(t *testing.T) { defer ts.TearDown() b := ts.OpenBrowser() - b.SetPageLoadTimeout(10 * time.Second) + b.SetPageLoadTimeout(2 * time.Second) b.VisitTo("https://play.golang.org/") b.WaitFor("id:code").Element().Clear().Input(code) @@ -70,4 +74,7 @@ func TestPlayground(t *testing.T) { text, _ := e.WebElement().Text() fmt.Printf("%+v\n", text) } + + b.TakeScreenshot("./screenshot.png") + b.TakeSource("./source.html") }