Skip to content

Commit e45a461

Browse files
committed
Updating screenshot code to prevent resizing if window is large enough.
In the previous screenshot update, the driver would incorrectly resize the IE window smaller if the document was smaller in height or width than the current window size. This could cause redraws of the page being viewed such that after resizing, the screenshot still did not display the entire page. This commit resolves that issue, and also takes into account the size of the scroll bars when creating the screenshot image. NOTE: There is a *very* slight chance that resizing the window so there are no longer scroll bars to be displayed *might* still cause layout redraws such that the screenshot does not show the entire DOM after the resize. Since we should now always be expanding the window size, never contracting it, this is a corner case that explicitly will *not* be fixed. Any issue reports describing this corner case will be closed without action. Fixes issue #1085.
1 parent 01cbd65 commit e45a461

File tree

6 files changed

+9398
-7356
lines changed

6 files changed

+9398
-7356
lines changed

cpp/iedriver/CommandHandlers/ScreenshotCommandHandler.h

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ class ScreenshotCommandHandler : public IECommandHandler {
132132
int target_window_height = document_info.height + chrome_height;
133133

134134
// For some reason, this technique does not allow the user to resize
135-
// the browser window to greater than SIZE_LIMIT x SIZE_LIMIT. This is pretty
136-
// big, so we'll cap the allowable screenshot size to that.
135+
// the browser window to greater than SIZE_LIMIT x SIZE_LIMIT. This is
136+
// pretty big, so we'll cap the allowable screenshot size to that.
137137
//
138138
// GDI+ limit after which it may report Generic error for some image types
139139
int SIZE_LIMIT = 65534;
@@ -153,6 +153,30 @@ class ScreenshotCommandHandler : public IECommandHandler {
153153
LOG(DEBUG) << "Initial browser window sizes are (w, h): "
154154
<< original_width << ", " << original_height;
155155

156+
// If the window is already wide enough to accomodate
157+
// the document, don't resize that dimension. Otherwise,
158+
// the window will display a horizontal scroll bar, and
159+
// we need to take the scroll bar height into account when
160+
// we size resulting image.
161+
int horizontal_scrollbar_height = 0;
162+
if (original_width > target_window_width) {
163+
target_window_width = original_width;
164+
} else {
165+
horizontal_scrollbar_height = ::GetSystemMetrics(SM_CYHSCROLL);
166+
}
167+
168+
// If the window is already tall enough to accomodate
169+
// the document, don't resize that dimension. Otherwise,
170+
// the window will display a vertical scroll bar, and
171+
// we need to take the scrollbar width into account when
172+
// we size the resulting image.
173+
int vertical_scrollbar_width = 0;
174+
if (original_height > target_window_height) {
175+
target_window_height = original_height;
176+
} else {
177+
vertical_scrollbar_width = ::GetSystemMetrics(SM_CXVSCROLL);
178+
}
179+
156180
BOOL is_maximized = ::IsZoomed(ie_window_handle);
157181
bool requires_resize = original_width < target_window_width ||
158182
original_height < target_window_height;
@@ -168,6 +192,13 @@ class ScreenshotCommandHandler : public IECommandHandler {
168192
::ShowWindow(ie_window_handle, SW_SHOWNORMAL);
169193
}
170194

195+
// NOTE: There is a *very* slight chance that resizing the window
196+
// so there are no longer scroll bars to be displayed *might* cause
197+
// layout redraws such that the screenshot does not show the entire
198+
// DOM after the resize. Since we should always be expanding the
199+
// window size, never contracting it, this is a corner case that
200+
// explicitly will *not* be fixed. Any issue reports describing this
201+
// corner case will be closed without action.
171202
RECT ie_window_rect;
172203
::GetWindowRect(ie_window_handle, &ie_window_rect);
173204
::SetWindowPos(ie_window_handle,
@@ -180,9 +211,10 @@ class ScreenshotCommandHandler : public IECommandHandler {
180211
}
181212

182213
// Capture the window's canvas to a DIB.
183-
BOOL created = this->image_->Create(document_info.width,
184-
document_info.height,
185-
/*numbers of bits per pixel = */ 32);
214+
BOOL created = this->image_->Create(
215+
document_info.width + vertical_scrollbar_width,
216+
document_info.height + horizontal_scrollbar_height,
217+
/*numbers of bits per pixel = */ 32);
186218
if (!created) {
187219
LOG(WARN) << "Unable to initialize image object";
188220
}

0 commit comments

Comments
 (0)