//go:build browser package main import ( "net/http" "strings" "testing" "github.com/chromedp/chromedp" "amadan.net/rastrillo/rastrillo" "amadan.net/rastrillo/rastrillo/harness" "amadan.net/rastrillo/abono" ) // TestBrowserSubscribeAndCancel drives the example in a real Chromium, // through rastrillo's harness: sign in by emailed link, the plans, Checkout on the // sandbox's page, Checkout's return, the account page, the sandbox's // billing page, cancelling, and the account page saying so. Any console // error, failed request or 4xx/5xx fails it at the screen it happened // on. Without a browser it fails (harness.ChromePath); it never skips. func TestBrowserSubscribeAndCancel(t *testing.T) { box := &inbox{} rig := harness.New(t, func(origin string) http.Handler { opts, err := Options(testConfig(t, origin, box)) if err != nil { t.Fatalf("Options: %v", err) } h, closeApp, err := rastrillo.Handler(opts) if err != nil { t.Fatalf("rastrillo.Handler: %v", err) } t.Cleanup(func() { closeApp() }) return h }) // Ask for a sign-in link, open it from the inbox, and press Sign in. rig.Run(chromedp.Navigate(rig.Origin + "/signin")) rig.Screen(`#rst-signin-email`, "sign in") rig.Run( chromedp.SendKeys(`#rst-signin-email`, "reader@example.com", chromedp.ByQuery), chromedp.Click(`form[action="/signin"] button[type=submit]`, chromedp.ByQuery), ) rig.Screen(`form[action="/signin/forget"]`, "the link was sent") rig.Run(chromedp.Navigate(box.link(t, "reader@example.com"))) rig.Screen(`form[action="/auth/verify"] button[type=submit]`, "the link's confirm page") rig.Run(chromedp.Click(`form[action="/auth/verify"] button[type=submit]`, chromedp.ByQuery)) rig.Screen(`form[action="/subscribe/annual"]`, "the plans, signed in") // Buy the annual plan on the sandbox's Checkout page. rig.Run(chromedp.Click(`form[action="/subscribe/annual"] button[type=submit]`, chromedp.ByQuery)) rig.Screen(`form[action$="/pay"]`, "the sandbox's checkout") rig.Run(chromedp.Click(`form[action$="/pay"] button[type=submit]`, chromedp.ByQuery)) rig.Screen(`main a[href="/account"]`, "Checkout's return") mustSay(t, rig, "main", say("abono.pages.done_paid")) // The account page shows it renewing. rig.Run(chromedp.Click(`main a[href="/account"]`, chromedp.ByQuery)) rig.Screen(`form[action="/account/billing"]`, "the account, paid") mustSay(t, rig, "main", say(abono.StatusRenews)) // Cancel on the sandbox's billing page, and come back. rig.Run(chromedp.Click(`form[action="/account/billing"] button[type=submit]`, chromedp.ByQuery)) rig.Screen(`form[action$="/cancel"] button[type=submit]`, "the sandbox's billing page") rig.Run( chromedp.Click(`form[action$="/cancel"] button[type=submit]`, chromedp.ByQuery), chromedp.WaitNotPresent(`form[action$="/cancel"] button[type=submit]`, chromedp.ByQuery), ) rig.Screen(`a[href$="/account"]`, "the sandbox's billing page, cancelled") rig.Run(chromedp.Click(`a[href$="/account"]`, chromedp.ByQuery)) rig.Screen(`form[action="/account/billing"]`, "the account, cancelling") mustSay(t, rig, "main", say(abono.StatusCancelling)) } // mustSay fails unless sel's text on the current screen contains text. func mustSay(t *testing.T, rig *harness.Rig, sel, text string) { t.Helper() var got string rig.Run(chromedp.Text(sel, &got, chromedp.ByQuery)) if !strings.Contains(got, text) { t.Fatalf("%s does not say %q:\n%s", sel, text, got) } }