Skip to content

Commit

Permalink
feat: add idle screenshot option
Browse files Browse the repository at this point in the history
  • Loading branch information
michael2to3 committed Sep 15, 2024
1 parent d58ad9d commit 4e33c0c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 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
4 changes: 4 additions & 0 deletions runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type ScanOptions struct {
NoScreenshotBytes bool
NoHeadlessBody bool
ScreenshotTimeout int
ScreenshotIdle int
}

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

Expand Down Expand Up @@ -305,6 +307,7 @@ type Options struct {
NoScreenshotBytes bool
NoHeadlessBody bool
ScreenshotTimeout int
ScreenshotIdle int
// HeadlessOptionalArguments specifies optional arguments to pass to Chrome
HeadlessOptionalArguments goflags.StringSlice
Protocol string
Expand Down Expand Up @@ -374,6 +377,7 @@ func ParseOptions() *Options {
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.IntVarP(&options.ScreenshotIdle, "screenshot-idle", "sid", 1, "set idle time before taking screenshot in seconds"),
)

flagSet.CreateGroup("matchers", "Matchers",
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, time.Duration(scanopts.ScreenshotTimeout)*time.Second, time.Duration(scanopts.ScreenshotIdle)*time.Second, r.options.CustomHeaders)
if err != nil {
gologger.Warning().Msgf("Could not take screenshot '%s': %s", fullURL, err)
} else {
Expand Down

0 comments on commit 4e33c0c

Please sign in to comment.