Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(final-screen) CurrentPageMatches use current path #17

Merged
merged 5 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion login/chrome/chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync/atomic"
"time"

"github.com/chromedp/chromedp"
"golang.org/x/oauth2"

"github.com/holyhope/digiposte-go-sdk/login"
Expand All @@ -27,7 +28,7 @@ func (c *chromeLogin) login( //nolint:nonamedreturns
ctx, cancel := WithCancelOnClose(independentChromeCtx, parentCtx.Done())
defer cancel()

defer c.ScreenshotIfNeeded(independentChromeCtx, &finalErr)
defer c.WrapError(independentChromeCtx, &finalErr)

if err := resolve(ctx, &firstScreen{
URL: c.url,
Expand All @@ -40,6 +41,36 @@ func (c *chromeLogin) login( //nolint:nonamedreturns
return c.resolveLogin(ctx, creds)
}

type WithLocationError struct {
Err error
Location string
}

func (e *WithLocationError) Error() string {
return fmt.Sprintf("%v at %q", e.Err, e.Location)
}

func (c *chromeLogin) WrapError(ctx context.Context, errPtr *error) {
if *errPtr == nil {
return
}

var currentLocation string

if err := chromedp.Run(ctx,
chromedp.Location(&currentLocation),
); err != nil {
errorLogger(ctx).Printf("Failed to get current location: %v\n", err)
} else {
*errPtr = &WithLocationError{
Err: *errPtr,
Location: currentLocation,
}
}

*errPtr = c.ScreenshotIfNeeded(ctx, *errPtr)
}

func WithCancelOnClose(ctx context.Context, done <-chan struct{}) (context.Context, context.CancelFunc) {
attachedChromeCtx, cancel := context.WithCancel(ctx)

Expand Down
8 changes: 8 additions & 0 deletions login/chrome/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var chromeOpts = []cu.Option{ //nolint:gochecknoglobals

var errNegativeFreq = errors.New("frequency must be positive")

// WithRefreshFrequency sets the frequency at which the login process will be refreshed.
func WithRefreshFrequency(frequency time.Duration) login.Option { //nolint:ireturn
return &withRefreshFrequency{Frequency: frequency}
}
Expand Down Expand Up @@ -58,6 +59,7 @@ func (o *withRefreshFrequency) Validate() error {

var errNegativeTimeout = errors.New("timeout must be positive")

// WithTimeout sets the timeout for the login process.
func WithTimeout(timeout time.Duration) login.Option { //nolint:ireturn
return &withTimeout{Timeout: timeout}
}
Expand Down Expand Up @@ -89,6 +91,7 @@ func (o *withTimeout) Validate() error {

var errEmptyURL = errors.New("url is empty")

// WithURL sets the URL to which the login process will be directed.
func WithURL(url string) login.Option { //nolint:ireturn
return &withURL{URL: url}
}
Expand Down Expand Up @@ -118,6 +121,7 @@ func (o *withURL) Apply(instance interface{}) error {
return &InvalidTypeOptionError{instance: instance}
}

// WithCookies sets the cookies to be used for the login process.
func WithCookies(cookies []*http.Cookie) login.Option { //nolint:ireturn
return &withCookies{Cookies: cookies}
}
Expand All @@ -136,6 +140,7 @@ func (o *withCookies) Apply(instance interface{}) error {
return &InvalidTypeOptionError{instance: instance}
}

// WithScreenShortOnError sets the option to take a screenshot when an error occurs.
func WithScreenShortOnError() login.Option { //nolint:ireturn
return &withScreenShortOnError{}
}
Expand Down Expand Up @@ -194,6 +199,7 @@ func (e *WithScreenshotError) Unwrap() error {
return e.Err
}

// WithLoggers sets the loggers to be used for the login process.
func WithLoggers(infoLgr, errorLgr *log.Logger) login.Option { //nolint:ireturn
return &withLoggers{
Info: infoLgr,
Expand Down Expand Up @@ -222,6 +228,7 @@ func (o *withLoggers) Apply(instance interface{}) error {
return &InvalidTypeOptionError{instance: instance}
}

// WithChromeVersion sets the version of chrome to be used for the login process.
func WithChromeVersion(ctx context.Context, revision int, client *http.Client) login.Option { //nolint:ireturn
browser := launcher.NewBrowser()
if ctx != nil {
Expand Down Expand Up @@ -264,6 +271,7 @@ func (o *withChromeVersion) Apply(instance interface{}) error {
return &InvalidTypeOptionError{instance: instance}
}

// WithBinary sets the path to the chrome binary to be used for the login process.
func WithBinary(path string) login.Option { //nolint:ireturn
return &withBinary{Path: path}
}
Expand Down
15 changes: 11 additions & 4 deletions login/chrome/screen_final.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"
"net/http"
"net/url"

"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
"golang.org/x/oauth2"
Expand All @@ -25,17 +25,24 @@ func (s *finalScreen) String() string {
}

func (s *finalScreen) CurrentPageMatches(ctx context.Context) bool {
var nodeIDs []cdp.NodeID
var currentLocation string

if err := chromedp.Run(ctx,
chromedp.NodeIDs(`#popin_tc_privacy_button`, &nodeIDs, chromedp.ByID, chromedp.AtLeast(0)),
chromedp.Location(&currentLocation),
); err != nil {
errorLogger(ctx).Printf("run: %v\n", err)

return false
}

return len(nodeIDs) > 0
currentURL, err := url.Parse(currentLocation)
if err != nil {
errorLogger(ctx).Printf("parse current location: %v\n", err)

return false
}

return currentURL.Path == "/home"
}

type InvalidTokenError struct {
Expand Down
8 changes: 5 additions & 3 deletions login/chrome/screenshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"github.com/chromedp/chromedp"
)

func (c *chromeLogin) ScreenshotIfNeeded(ctx context.Context, errPtr *error) {
if c.screenShortOnError && *errPtr != nil {
*errPtr = c.wrapWithScreenshot(ctx, *errPtr)
func (c *chromeLogin) ScreenshotIfNeeded(ctx context.Context, err error) error {
if !c.screenShortOnError {
return err
}

return c.wrapWithScreenshot(ctx, err)
}

func (c *chromeLogin) wrapWithScreenshot(ctx context.Context, rootErr error) error {
Expand Down
23 changes: 0 additions & 23 deletions login/chrome/utils_test.go

This file was deleted.

Loading