2019-11-02 14:32:58 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2019-11-24 20:27:59 +00:00
|
|
|
"testing"
|
2020-11-19 01:50:34 +00:00
|
|
|
"time"
|
2019-11-02 14:32:58 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
"github.com/go-rod/rod"
|
|
|
|
"github.com/go-rod/rod/lib/launcher"
|
2019-11-24 20:27:59 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-02 14:32:58 +00:00
|
|
|
)
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
// RodSession binding a chrome session with devtool protocol.
|
|
|
|
type RodSession struct {
|
|
|
|
Launcher *launcher.Launcher
|
|
|
|
WebDriver *rod.Browser
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
// StartRodWithProxy create a rod/chromedp session.
|
2023-01-21 05:02:27 +00:00
|
|
|
func StartRodWithProxy(proxy string) (session *RodSession, err error) {
|
|
|
|
var browserPath string
|
|
|
|
|
|
|
|
if browserPath, err = GetBrowserPath(); err != nil {
|
|
|
|
return nil, err
|
2020-03-04 23:25:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
headless := false
|
|
|
|
trace := true
|
|
|
|
motion := 0 * time.Second
|
2020-01-17 21:06:33 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
if os.Getenv("HEADLESS") != "" {
|
2021-11-05 13:14:42 +00:00
|
|
|
headless = true
|
|
|
|
trace = false
|
|
|
|
motion = 0 * time.Second
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
l := launcher.New().
|
|
|
|
Bin(browserPath).
|
|
|
|
Proxy(proxy).
|
|
|
|
Headless(headless).
|
|
|
|
Devtools(true)
|
|
|
|
url := l.MustLaunch()
|
2019-11-24 20:27:59 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
browser := rod.New().
|
|
|
|
ControlURL(url).
|
|
|
|
Trace(trace).
|
|
|
|
SlowMotion(motion).
|
|
|
|
MustConnect()
|
2019-11-02 14:32:58 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
browser.MustIgnoreCertErrors(true)
|
2020-12-16 01:47:31 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
return &RodSession{
|
|
|
|
Launcher: l,
|
|
|
|
WebDriver: browser,
|
2019-11-02 14:32:58 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
// StartRod create a rod/chromedp session.
|
|
|
|
func StartRod() (*RodSession, error) {
|
|
|
|
return StartRodWithProxy("")
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
// Stop stop the rod/chromedp session.
|
|
|
|
func (rs *RodSession) Stop() error {
|
|
|
|
err := rs.WebDriver.Close()
|
2019-11-02 14:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
rs.Launcher.Cleanup()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
return err
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 10:57:44 +00:00
|
|
|
// CheckElementExistsLocatedByID checks the existence of an element located by an id.
|
|
|
|
func (rs *RodSession) CheckElementExistsLocatedByID(t *testing.T, page *rod.Page, cssSelector string) bool {
|
|
|
|
b, _, err := page.Has("#" + cssSelector)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00: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-24 20:27:59 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-05 13:14:42 +00:00
|
|
|
require.NotNil(t, e)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
return e
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
// WaitElementLocatedByID waits for an element located by an id.
|
|
|
|
func (rs *RodSession) WaitElementLocatedByID(t *testing.T, page *rod.Page, cssSelector string) *rod.Element {
|
2021-11-05 13:14:42 +00:00
|
|
|
e, err := page.Element("#" + cssSelector)
|
2019-11-24 20:27:59 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-05 13:14:42 +00:00
|
|
|
require.NotNil(t, e)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
return e
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
// WaitElementsLocatedByID waits for an elements located by an id.
|
|
|
|
func (rs *RodSession) WaitElementsLocatedByID(t *testing.T, page *rod.Page, cssSelector string) rod.Elements {
|
2021-11-05 13:14:42 +00:00
|
|
|
e, err := page.Elements("#" + cssSelector)
|
2019-11-24 20:27:59 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-05 13:14:42 +00:00
|
|
|
require.NotNil(t, e)
|
|
|
|
|
|
|
|
return e
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
2021-05-04 22:06:05 +00:00
|
|
|
|
2021-11-05 13:14:42 +00: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-04 22:06:05 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
if strings.Contains(text, pattern) {
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("body does not contain pattern: %s", pattern)
|
|
|
|
}
|
2021-05-04 22:06:05 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|