Skip to content

Commit 9db27dc

Browse files
committed
implemented elementScreenshot command for web views
1 parent 8ea4e6d commit 9db27dc

File tree

6 files changed

+90
-6
lines changed

6 files changed

+90
-6
lines changed

inc/extension_qt/graphics_web_view_executor.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class GraphicsWebViewCmdExecutor : public ViewCmdExecutor {
4747
virtual void SetBounds(const Rect& bounds, Error** error) NOT_SUPPORTED_IMPL;
4848
virtual void Maximize(Error** error) NOT_SUPPORTED_IMPL;
4949
virtual void GetScreenShot(std::string* png, Error** error);
50-
virtual void GetElementScreenShot(const ElementId& element, std::string* png, Error** error) NOT_SUPPORTED_IMPL;
50+
virtual void GetElementScreenShot(const ElementId& element, std::string* png, Error** error);
5151
virtual void GoForward(Error** error);
5252
virtual void GoBack(Error** error);
5353
virtual void Reload(Error** error);
@@ -137,6 +137,7 @@ class GraphicsWebViewCmdExecutor : public ViewCmdExecutor {
137137

138138
protected:
139139
QGraphicsWebView* getView(const ViewId& viewId, Error** error);
140+
void saveScreenshot(QImage& image, std::string* png, Error** error);
140141

141142
private:
142143
scoped_ptr<QWebkitProxy> webkitProxy_;

inc/extension_qt/q_view_executor.h

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class QViewCmdExecutor : public ViewCmdExecutor {
5858
QTouchDevice touchDevice;
5959
#endif
6060

61+
void saveScreenshot(QPixmap& pixmap, std::string* png, Error** error);
62+
6163
private:
6264
DISALLOW_COPY_AND_ASSIGN(QViewCmdExecutor);
6365
};

inc/extension_qt/web_view_executor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class QWebViewCmdExecutor : public QViewCmdExecutor {
5050
virtual void Reload(Error** error);
5151
virtual void GetSource(std::string* source, Error** error);
5252
virtual void SendKeys(const ElementId& element, const string16& keys, Error** error);
53-
virtual void GetElementScreenShot(const ElementId& element, std::string* png, Error** error) NOT_SUPPORTED_IMPL;
53+
virtual void GetElementScreenShot(const ElementId& element, std::string* png, Error** error);
5454
virtual void MouseDoubleClick(Error** error);
5555
virtual void MouseButtonUp(Error** error);
5656
virtual void MouseButtonDown(Error** error);

src/webdriver/extension_qt/graphics_web_view_executor.cc

+44-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,49 @@ void GraphicsWebViewCmdExecutor::GetScreenShot(std::string* png, Error** error)
136136
qobject_cast<QGraphicsObject*>(view_)->paint(&painter, &styleOption);
137137
painter.end();
138138

139+
saveScreenshot(image, png, error);
140+
}
141+
142+
void GraphicsWebViewCmdExecutor::GetElementScreenShot(const ElementId& element, std::string* png, Error** error) {
143+
CHECK_VIEW_EXISTANCE
144+
145+
Point location;
146+
*error = webkitProxy_->GetElementLocationInView(element, &location);
147+
if (*error)
148+
return;
149+
150+
Size size;
151+
*error = webkitProxy_->GetElementSize(element, &size);
152+
if (*error)
153+
return;
154+
155+
QImage image(view_->boundingRect().size().toSize(), QImage::Format_RGB32);
156+
image.fill(QColor(0, 0, 0).rgb());
157+
QPainter painter(&image);
158+
QStyleOptionGraphicsItem styleOption;
159+
qobject_cast<QGraphicsObject*>(view_)->paint(&painter, &styleOption);
160+
painter.end();
161+
162+
QRect viewRect = image.rect();
163+
QRect elementRect(location.x(), location.y(), size.width(), size.height());
164+
QRect intersectedRect = viewRect.intersected(elementRect);
165+
166+
session_->logger().Log(kFineLogLevel, base::StringPrintf("GetElementScreenShot, view: (%2d;%2d : %4d;%4d)", viewRect.x(), viewRect.y(), viewRect.width(), viewRect.height()));
167+
session_->logger().Log(kFineLogLevel, base::StringPrintf("GetElementScreenShot, elem: (%2d;%2d : %4d;%4d)", elementRect.x(), elementRect.y(), elementRect.width(), elementRect.height()));
168+
session_->logger().Log(kFineLogLevel, base::StringPrintf("GetElementScreenShot, res: (%2d;%2d : %4d;%4d)", intersectedRect.x(), intersectedRect.y(), intersectedRect.width(), intersectedRect.height()));
169+
170+
if ((0 == intersectedRect.width()) || (0 == intersectedRect.height())) {
171+
*error = new Error(kMoveTargetOutOfBounds);
172+
session_->logger().Log(kWarningLogLevel, "GetElementScreenShot, element is not in view.");
173+
return;
174+
}
175+
176+
QImage elementImage = image.copy(intersectedRect);
177+
178+
saveScreenshot(elementImage, png, error);
179+
}
180+
181+
void GraphicsWebViewCmdExecutor::saveScreenshot(QImage& image, std::string* png, Error** error) {
139182
const FilePath::CharType kPngFileName[] = FILE_PATH_LITERAL("./screen.png");
140183
FilePath path = session_->temp_dir().Append(kPngFileName);;
141184

@@ -156,7 +199,7 @@ void GraphicsWebViewCmdExecutor::GetScreenShot(std::string* png, Error** error)
156199
}
157200

158201
if (!file_util::ReadFileToString(path, png))
159-
*error = new Error(kUnknownError, "Could not read screenshot file");
202+
*error = new Error(kUnknownError, "Could not read screenshot file");
160203
}
161204

162205
void GraphicsWebViewCmdExecutor::GoForward(Error** error) {

src/webdriver/extension_qt/q_view_executor.cc

+7-3
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,15 @@ void QViewCmdExecutor::GetScreenShot(std::string* png, Error** error) {
109109
if (NULL == view)
110110
return;
111111

112+
QPixmap pixmap = QPixmap::grabWidget(view);
113+
114+
saveScreenshot(pixmap, png, error);
115+
}
116+
117+
void QViewCmdExecutor::saveScreenshot(QPixmap& pixmap, std::string* png, Error** error) {
112118
const FilePath::CharType kPngFileName[] = FILE_PATH_LITERAL("./screen.png");
113119
FilePath path = session_->temp_dir().Append(kPngFileName);;
114120

115-
QPixmap pixmap = QPixmap::grabWidget(view);
116-
117121
#if defined(OS_WIN)
118122
session_->logger().Log(kInfoLogLevel, "Save screenshot to - " + path.MaybeAsASCII());
119123
#elif defined(OS_POSIX)
@@ -131,7 +135,7 @@ void QViewCmdExecutor::GetScreenShot(std::string* png, Error** error) {
131135
}
132136

133137
if (!file_util::ReadFileToString(path, png))
134-
*error = new Error(kUnknownError, "Could not read screenshot file");
138+
*error = new Error(kUnknownError, "Could not read screenshot file");
135139
}
136140

137141
void QViewCmdExecutor::SendKeys(const string16& keys, Error** error) {

src/webdriver/extension_qt/web_view_executor.cc

+34
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,40 @@ void QWebViewCmdExecutor::CanHandleUrl(const std::string& url, bool* can, Error
108108
*can = QWebViewUtil::isUrlSupported(pWebView->page(), url, error);
109109
}
110110

111+
void QWebViewCmdExecutor::GetElementScreenShot(const ElementId& element, std::string* png, Error** error) {
112+
CHECK_VIEW_EXISTANCE
113+
114+
Point location;
115+
*error = webkitProxy_->GetElementLocationInView(element, &location);
116+
if (*error)
117+
return;
118+
119+
Size size;
120+
*error = webkitProxy_->GetElementSize(element, &size);
121+
if (*error)
122+
return;
123+
124+
QPixmap pixmap = QPixmap::grabWidget(view_);
125+
126+
QRect viewRect = pixmap.rect();
127+
QRect elementRect(location.x(), location.y(), size.width(), size.height());
128+
QRect intersectedRect = viewRect.intersected(elementRect);
129+
130+
session_->logger().Log(kFineLogLevel, base::StringPrintf("GetElementScreenShot, view: (%2d;%2d : %4d;%4d)", viewRect.x(), viewRect.y(), viewRect.width(), viewRect.height()));
131+
session_->logger().Log(kFineLogLevel, base::StringPrintf("GetElementScreenShot, elem: (%2d;%2d : %4d;%4d)", elementRect.x(), elementRect.y(), elementRect.width(), elementRect.height()));
132+
session_->logger().Log(kFineLogLevel, base::StringPrintf("GetElementScreenShot, res: (%2d;%2d : %4d;%4d)", intersectedRect.x(), intersectedRect.y(), intersectedRect.width(), intersectedRect.height()));
133+
134+
if ((0 == intersectedRect.width()) || (0 == intersectedRect.height())) {
135+
*error = new Error(kMoveTargetOutOfBounds);
136+
session_->logger().Log(kWarningLogLevel, "GetElementScreenShot, element is not in view.");
137+
return;
138+
}
139+
140+
QPixmap elementPixmap = pixmap.copy(intersectedRect);
141+
142+
saveScreenshot(elementPixmap, png, error);
143+
}
144+
111145
void QWebViewCmdExecutor::GetTitle(std::string* title, Error **error) {
112146
CHECK_VIEW_EXISTANCE
113147

0 commit comments

Comments
 (0)