2019-11-02 21:32:58 +07:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2019-11-25 03:27:59 +07:00
|
|
|
"testing"
|
2020-11-19 08:50:34 +07:00
|
|
|
"time"
|
2019-11-02 21:32:58 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
"github.com/go-rod/rod"
|
|
|
|
"github.com/go-rod/rod/lib/launcher"
|
2019-11-25 03:27:59 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-02 21:32:58 +07:00
|
|
|
)
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
// RodSession binding a chrome session with devtool protocol.
|
|
|
|
type RodSession struct {
|
|
|
|
Launcher *launcher.Launcher
|
|
|
|
WebDriver *rod.Browser
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
// StartRodWithProxy create a rod/chromedp session.
|
|
|
|
func StartRodWithProxy(proxy string) (*RodSession, error) {
|
2020-03-05 06:25:52 +07:00
|
|
|
browserPath := os.Getenv("BROWSER_PATH")
|
|
|
|
if browserPath == "" {
|
|
|
|
browserPath = "/usr/bin/chromium-browser"
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
headless := false
|
|
|
|
trace := true
|
|
|
|
motion := 0 * time.Second
|
2020-01-18 04:06:33 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
if os.Getenv("HEADLESS") != "" {
|
2021-11-05 20:14:42 +07:00
|
|
|
headless = true
|
|
|
|
trace = false
|
|
|
|
motion = 0 * time.Second
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
l := launcher.New().
|
|
|
|
Bin(browserPath).
|
|
|
|
Proxy(proxy).
|
|
|
|
Headless(headless).
|
|
|
|
Devtools(true)
|
|
|
|
url := l.MustLaunch()
|
2019-11-25 03:27:59 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
browser := rod.New().
|
|
|
|
ControlURL(url).
|
|
|
|
Trace(trace).
|
|
|
|
SlowMotion(motion).
|
|
|
|
MustConnect()
|
2019-11-02 21:32:58 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
browser.MustIgnoreCertErrors(true)
|
2020-12-16 08:47:31 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
return &RodSession{
|
|
|
|
Launcher: l,
|
|
|
|
WebDriver: browser,
|
2019-11-02 21:32:58 +07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
// StartRod create a rod/chromedp session.
|
|
|
|
func StartRod() (*RodSession, error) {
|
|
|
|
return StartRodWithProxy("")
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
// Stop stop the rod/chromedp session.
|
|
|
|
func (rs *RodSession) Stop() error {
|
|
|
|
err := rs.WebDriver.Close()
|
2019-11-02 21:32:58 +07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
rs.Launcher.Cleanup()
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
return err
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
// WaitElementLocatedByClassName wait an element is located by class name.
|
|
|
|
func (rs *RodSession) WaitElementLocatedByClassName(t *testing.T, page *rod.Page, className string) *rod.Element {
|
|
|
|
e, err := page.Element("." + className)
|
2019-11-25 03:27:59 +07:00
|
|
|
require.NoError(t, err)
|
2021-11-05 20:14:42 +07:00
|
|
|
require.NotNil(t, e)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
return e
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
// WaitElementLocatedByCSSSelector wait an element is located by class name.
|
|
|
|
func (rs *RodSession) WaitElementLocatedByCSSSelector(t *testing.T, page *rod.Page, cssSelector string) *rod.Element {
|
|
|
|
e, err := page.Element("#" + cssSelector)
|
2019-11-25 03:27:59 +07:00
|
|
|
require.NoError(t, err)
|
2021-11-05 20:14:42 +07:00
|
|
|
require.NotNil(t, e)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
return e
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// WaitElementsLocatedByCSSSelector wait an element is located by CSS selector.
|
2021-11-05 20:14:42 +07:00
|
|
|
func (rs *RodSession) WaitElementsLocatedByCSSSelector(t *testing.T, page *rod.Page, cssSelector string) rod.Elements {
|
|
|
|
e, err := page.Elements("#" + cssSelector)
|
2019-11-25 03:27:59 +07:00
|
|
|
require.NoError(t, err)
|
2021-11-05 20:14:42 +07:00
|
|
|
require.NotNil(t, e)
|
|
|
|
|
|
|
|
return e
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
2021-05-05 05:06:05 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
func (rs *RodSession) waitBodyContains(t *testing.T, page *rod.Page, pattern string) {
|
|
|
|
text, err := page.MustElementR("body", pattern).Text()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, text)
|
2021-05-05 05:06:05 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
if strings.Contains(text, pattern) {
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("body does not contain pattern: %s", pattern)
|
|
|
|
}
|
2021-05-05 05:06:05 +07:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|