Skip to content

Commit

Permalink
add ability to specify a default screnshot size for autogenerated scr…
Browse files Browse the repository at this point in the history
…eenshots
  • Loading branch information
onsi committed Apr 18, 2023
1 parent 33543ae commit 7670a24
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
30 changes: 27 additions & 3 deletions biloba.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,34 @@ func BilobaConfigDisableFailureScreenshots() func(*Biloba) {
}

/*
Pass BilobaConfigWithChromeConnection to [ConnectToChrome] to disable screenshots when Progress Reports are emitted
Pass BilobaConfigFailureScreenshotsSize to [ConnectToChrome] to set the size for the screenshots generated on failure
*/
func BilobaConfigFailureScreenshotsSize(width, height int) func(*Biloba) {
return func(b *Biloba) {
b.failureScreenshotWidth = width
b.failureScreenshotHeight = height
}
}

/*
Pass BilobaConfigDisableProgressReportScreenshots to [ConnectToChrome] to disable screenshots when Progress Reports are emitted
*/
func BilobaConfigDisableProgressReportScreenshots() func(*Biloba) {
return func(b *Biloba) {
b.disableProgressReportScreenshots = true
}
}

/*
Pass BilobaConfigProgressReportScreenshotSize to [ConnectToChrome] to set the size for the screenshots generated when a progress report is requested
*/
func BilobaConfigProgressReportScreenshotSize(width, height int) func(*Biloba) {
return func(b *Biloba) {
b.progressReportScreenshotWidth = width
b.progressReportScreenshotHeight = height
}
}

/*
Call ConnectToChrome(GinkgoT()) to connect to a Chrome browser
Expand Down Expand Up @@ -268,6 +288,10 @@ type Biloba struct {
enableDebugLogging bool
disableFailureScreenshots bool
disableProgressReportScreenshots bool
failureScreenshotWidth int
failureScreenshotHeight int
progressReportScreenshotWidth int
progressReportScreenshotHeight int
}

func (b *Biloba) GomegaString() string {
Expand Down Expand Up @@ -430,7 +454,7 @@ func (b *Biloba) Close() error {

func (b *Biloba) attachScreenshotsIfFailed() {
if b.gt.Failed() {
for _, screenshot := range b.safeAllTabScreenshots() {
for _, screenshot := range b.safeAllTabScreenshots(b.failureScreenshotWidth, b.failureScreenshotHeight) {
if screenshot.failure != "" {
b.gt.AddReportEntryVisibilityFailureOrVerbose(screenshot.failure)
} else {
Expand All @@ -442,7 +466,7 @@ func (b *Biloba) attachScreenshotsIfFailed() {

func (b *Biloba) progressReporter() string {
out := ""
for _, screenshot := range b.safeAllTabScreenshots() {
for _, screenshot := range b.safeAllTabScreenshots(b.progressReportScreenshotWidth, b.progressReportScreenshotHeight) {
if screenshot.failure != "" {
out += b.gt.F("{{red}}" + screenshot.failure + "{{/}}\n")
} else {
Expand Down
7 changes: 6 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,6 @@ var result map[string]int
b.Run("({a:1, b:2})", &result)
```


## Window Size, Screenshots, and Configuration

There are a few other odds and ends to cover, let's dive in
Expand All @@ -1747,6 +1746,8 @@ b.SetWindowSize(width, height)

this also accepts any [`chromedp.EmulateViewportOption`](https://pkg.go.dev/github.com/chromedp/chromedp#EmulateViewportOption) after `height`. `b.SetWindowSize` automatically sets up a Ginkgo `DeferCleanup` to reset the window size when the spec ends. You can query the window size of a tab at any time using `b.WindowSize()`.

One quick hack to speed up a test suite is to use the _smallest_ viable window size to run the tests. You can then pass `BilobaConfigFailureScreenshotsSize(width, height)` to `ConnectToChrome(...)` to configure the size of Biloba's automatically generated screenshots. Biloba will scale the window up on failure, take a screenshot, then scale it back down to proceed with other tests. As an anecdotal data-point a 30% speed-up was observed for a Biloba test suite against a complex web-app running in parallel when the screen-size was minimized in this way.

### Capturing Screenshots

As discussed above, Biloba automatically emits screenshots when a spec fails or a progress report is requested.
Expand Down Expand Up @@ -1792,7 +1793,11 @@ and sit back and watch those windows appear and disappear as you run your specs.
- `BilobaConfigEnableDebugLogging()` will send all Chrome DevTools protocol traffic to the `GinkgoWriter`. This can be useful when debugging specs and/or implementing your own more advanced `chromedp` behavior. Fair warning, though: these logs are verbose!
- `BilobaConfigWithChromeConnection(cc ChromeConnection)` allows you to specify your own Chrome connection settings (typically a `WebSocketURL`)
- `BilobaConfigDisableFailureScreenshots()` disables Biloba's screenshots on failure
- `BilobaConfigDisableFailureScreenshots()` disables Biloba's screenshots on failure
- `BilobaConfigDisableProgressReportScreenshots()` disables Biloba's screenshots when progress reports are requested
- `BilobaConfigFailureScreenshotsSize(width, height)` specifies the window size to use when generating a screenshot on failure
- `BilobaConfigProgressReportScreenshotSize(width, height)` specifies the window size to use when generating a screenshot when progress reports are requested



{% endraw %}
19 changes: 18 additions & 1 deletion screenshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,34 @@ type tabScreenshot struct {
failure string
}

func (b *Biloba) safeAllTabScreenshots() []tabScreenshot {
func (b *Biloba) safeAllTabScreenshots(width int, height int) []tabScreenshot {
out := []tabScreenshot{}
for _, tab := range b.AllTabs() {
ctx, cancel := context.WithTimeout(tab.Context, time.Second)
defer cancel()

var originalWidth, originalHeight int
if width > 0 && height > 0 {
originalWidth, originalHeight = b.WindowSize()
err := chromedp.Run(ctx, chromedp.EmulateViewport(int64(width), int64(height)))
if err != nil {
out = append(out, tabScreenshot{failure: fmt.Sprintf("failed to set window size: %s", err.Error())})
continue
}
}
var img []byte
var title string
err := chromedp.Run(ctx,
chromedp.Title(&title),
chromedp.FullScreenshot(&img, 100),
)
if width > 0 && height > 0 {
err := chromedp.Run(ctx, chromedp.EmulateViewport(int64(originalWidth), int64(originalHeight), chromedp.EmulatePortrait))
if err != nil {
out = append(out, tabScreenshot{failure: fmt.Sprintf("failed to reset window size: %s", err.Error())})
continue
}
}
if ctx.Err() != nil {
out = append(out, tabScreenshot{failure: "Timed out attempting to fetch screenshot for tab"})
continue
Expand Down

0 comments on commit 7670a24

Please sign in to comment.