Skip to content

Commit

Permalink
Merge pull request #1917 from michael2to3/feat/wait-after-screenshot
Browse files Browse the repository at this point in the history
Added Idle Time Before Screenshot Capture
  • Loading branch information
Mzack9999 committed Sep 16, 2024
2 parents 230f59e + 2455adc commit d6082fd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions runner/headless.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewBrowser(proxy string, useLocal bool, optionalArgs map[string]string) (*B
return engine, nil
}

func (b *Browser) ScreenshotWithBody(url string, timeout time.Duration, headers []string) ([]byte, string, error) {
func (b *Browser) ScreenshotWithBody(url string, timeout time.Duration, idle time.Duration, headers []string) ([]byte, string, error) {
page, err := b.engine.Page(proto.TargetCreateTarget{})
if err != nil {
return nil, "", err
Expand All @@ -126,7 +126,7 @@ func (b *Browser) ScreenshotWithBody(url string, timeout time.Duration, headers
if err := page.WaitLoad(); err != nil {
return nil, "", err
}
_ = page.WaitIdle(1 * time.Second)
_ = page.WaitIdle(idle)

screenshot, err := page.Screenshot(true, &proto.PageCaptureScreenshot{})
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ type ScanOptions struct {
DisableStdin bool
NoScreenshotBytes bool
NoHeadlessBody bool
ScreenshotTimeout int
ScreenshotTimeout time.Duration
ScreenshotIdle time.Duration
}

func (s *ScanOptions) Clone() *ScanOptions {
Expand Down Expand Up @@ -157,6 +158,7 @@ func (s *ScanOptions) Clone() *ScanOptions {
NoScreenshotBytes: s.NoScreenshotBytes,
NoHeadlessBody: s.NoHeadlessBody,
ScreenshotTimeout: s.ScreenshotTimeout,
ScreenshotIdle: s.ScreenshotIdle,
}
}

Expand Down Expand Up @@ -307,7 +309,8 @@ type Options struct {
HttpApiEndpoint string
NoScreenshotBytes bool
NoHeadlessBody bool
ScreenshotTimeout int
ScreenshotTimeout time.Duration
ScreenshotIdle time.Duration
// HeadlessOptionalArguments specifies optional arguments to pass to Chrome
HeadlessOptionalArguments goflags.StringSlice
Protocol string
Expand Down Expand Up @@ -377,7 +380,8 @@ func ParseOptions() *Options {
flagSet.StringSliceVarP(&options.HeadlessOptionalArguments, "headless-options", "ho", nil, "start headless chrome with additional options", goflags.FileCommaSeparatedStringSliceOptions),
flagSet.BoolVarP(&options.NoScreenshotBytes, "exclude-screenshot-bytes", "esb", false, "enable excluding screenshot bytes from json output"),
flagSet.BoolVarP(&options.NoHeadlessBody, "exclude-headless-body", "ehb", false, "enable excluding headless header from json output"),
flagSet.IntVarP(&options.ScreenshotTimeout, "screenshot-timeout", "st", 10, "set timeout for screenshot in seconds"),
flagSet.DurationVarP(&options.ScreenshotTimeout, "screenshot-timeout", "st", 10*time.Second, "set timeout for screenshot in seconds"),
flagSet.DurationVarP(&options.ScreenshotIdle, "screenshot-idle", "sid", 1*time.Second, "set idle time before taking screenshot in seconds"),
)

flagSet.CreateGroup("matchers", "Matchers",
Expand Down Expand Up @@ -630,7 +634,7 @@ func (options *Options) ValidateOptions() error {
msg += fmt.Sprintf("%s flag is", last)
}
msg += " incompatible with silent flag"
return fmt.Errorf("%v", msg)
return errors.New(msg)
}
}

Expand Down
2 changes: 1 addition & 1 deletion runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2144,7 +2144,7 @@ retry:
var pHash uint64
if scanopts.Screenshot {
var err error
screenshotBytes, headlessBody, err = r.browser.ScreenshotWithBody(fullURL, time.Duration(scanopts.ScreenshotTimeout)*time.Second, r.options.CustomHeaders)
screenshotBytes, headlessBody, err = r.browser.ScreenshotWithBody(fullURL, scanopts.ScreenshotTimeout, scanopts.ScreenshotIdle, r.options.CustomHeaders)
if err != nil {
gologger.Warning().Msgf("Could not take screenshot '%s': %s", fullURL, err)
} else {
Expand Down

0 comments on commit d6082fd

Please sign in to comment.