Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: chrome/test/webdriver/session.cc

Issue 5572001: Send screenshots back to the client for debugging (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: for review Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/test/webdriver/session_manager.h" 5 #include "chrome/test/webdriver/session_manager.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/base64.h"
9 #include "base/command_line.h" 10 #include "base/command_line.h"
10 #include "base/file_path.h" 11 #include "base/file_path.h"
11 #include "base/file_util.h" 12 #include "base/file_util.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/message_loop_proxy.h" 14 #include "base/message_loop_proxy.h"
14 #include "base/process.h" 15 #include "base/process.h"
15 #include "base/process_util.h" 16 #include "base/process_util.h"
16 #include "base/scoped_ptr.h" 17 #include "base/scoped_ptr.h"
18 #include "base/scoped_temp_dir.h"
17 #include "base/stringprintf.h" 19 #include "base/stringprintf.h"
18 #include "base/string_number_conversions.h" 20 #include "base/string_number_conversions.h"
19 #include "base/string_util.h" 21 #include "base/string_util.h"
20 #include "base/synchronization/waitable_event.h" 22 #include "base/synchronization/waitable_event.h"
21 #include "base/json/json_reader.h" 23 #include "base/json/json_reader.h"
22 #include "base/json/json_writer.h" 24 #include "base/json/json_writer.h"
23 #include "base/test/test_timeouts.h" 25 #include "base/test/test_timeouts.h"
24 #include "base/utf_string_conversions.h" 26 #include "base/utf_string_conversions.h"
25 #include "chrome/app/chrome_command_ids.h" 27 #include "chrome/app/chrome_command_ids.h"
26 #include "chrome/common/chrome_constants.h" 28 #include "chrome/common/chrome_constants.h"
(...skipping 25 matching lines...) Expand all
52 bool success = false; 54 bool success = false;
53 if (thread_.Start()) { 55 if (thread_.Start()) {
54 RunSessionTask(NewRunnableMethod( 56 RunSessionTask(NewRunnableMethod(
55 this, 57 this,
56 &Session::InitOnSessionThread, 58 &Session::InitOnSessionThread,
57 browser_dir, 59 browser_dir,
58 &success)); 60 &success));
59 } else { 61 } else {
60 LOG(ERROR) << "Cannot start session thread"; 62 LOG(ERROR) << "Cannot start session thread";
61 } 63 }
64
62 if (!success) 65 if (!success)
63 delete this; 66 delete this;
67
64 return success; 68 return success;
65 } 69 }
66 70
67 void Session::Terminate() { 71 void Session::Terminate() {
68 RunSessionTask(NewRunnableMethod( 72 RunSessionTask(NewRunnableMethod(
69 this, 73 this,
70 &Session::TerminateOnSessionThread)); 74 &Session::TerminateOnSessionThread));
71 delete this; 75 delete this;
72 } 76 }
73 77
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 bool Session::GetWindowIds(std::vector<int>* window_ids) { 346 bool Session::GetWindowIds(std::vector<int>* window_ids) {
343 bool success = false; 347 bool success = false;
344 RunSessionTask(NewRunnableMethod( 348 RunSessionTask(NewRunnableMethod(
345 automation_.get(), 349 automation_.get(),
346 &Automation::GetTabIds, 350 &Automation::GetTabIds,
347 window_ids, 351 window_ids,
348 &success)); 352 &success));
349 return success; 353 return success;
350 } 354 }
351 355
356 bool Session::CaptureEntirePageAsPNG(const FilePath& path) {
357 bool success = false;
358 RunSessionTask(NewRunnableMethod(
359 automation_.get(),
360 &Automation::CaptureEntirePageAsPNG,
361 current_window_id_,
362 path,
363 &success));
364 return success;
365 }
366
367 bool Session::ScreenshotAsBase64(std::string* screenshot) {
368 ScopedTempDir screenshots_dir;
369
370 // Create a temp directory for screenshots.
371 if (!screenshots_dir.CreateUniqueTempDir()) {
372 LOG(ERROR) << "Could not make a temp directory for screenshots";
373 return false;
374 }
375
376 // Generate a temp file name for the screenshot
kkania 2011/02/24 22:05:02 we no longer need a temp file name if we are using
Joe 2011/03/02 02:21:25 Done.
377 scoped_ptr<char> file(tempnam(screenshots_dir.path().value().c_str(),
378 "screencapture"));
379 if (NULL == file.get()) {
380 LOG(ERROR) << "Temp file name was not allocated";
381 return false;
382 }
383
384 #ifdef OS_POSIX
385 FilePath path = FilePath(file.get());
386 #elif OS_WIN
387 FilePath path = FilePath(ASCIIToWide(file.get()));
388 #endif
389
390 // Capture the current tab as a PNG file.
391 if (!CaptureEntirePageAsPNG(path)) {
392 LOG(ERROR) << "Capturing tab page as PNG failed";
393 return false;
394 }
395
396 std::string raw_bytes;
397 if (!file_util::ReadFileToString(path, &raw_bytes)) {
398 LOG(ERROR) << "Opening PNG file failed";
399 return false;
400 }
401
402 // Make a base64 encoded string for the raw PNG data
403 if (!base::Base64Encode(raw_bytes, screenshot)) {
404 LOG(ERROR) << "Encoding the PNG to base64 format failed";
405 return false;
406 }
407
408 return true;
409 }
410
352 ErrorCode Session::SwitchToWindow(const std::string& name) { 411 ErrorCode Session::SwitchToWindow(const std::string& name) {
353 int switch_to_id = 0; 412 int switch_to_id = 0;
354 int name_no = 0; 413 int name_no = 0;
355 if (base::StringToInt(name, &name_no)) { 414 if (base::StringToInt(name, &name_no)) {
356 bool does_exist = false; 415 bool does_exist = false;
357 RunSessionTask(NewRunnableMethod( 416 RunSessionTask(NewRunnableMethod(
358 automation_.get(), 417 automation_.get(),
359 &Automation::DoesTabExist, 418 &Automation::DoesTabExist,
360 name_no, 419 name_no,
361 &does_exist)); 420 &does_exist));
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 if (result->GetAsString(&xpath)) { 601 if (result->GetAsString(&xpath)) {
543 if (current_frame_xpath_.length()) 602 if (current_frame_xpath_.length())
544 current_frame_xpath_ += "\n"; 603 current_frame_xpath_ += "\n";
545 current_frame_xpath_ += xpath; 604 current_frame_xpath_ += xpath;
546 return kSuccess; 605 return kSuccess;
547 } 606 }
548 return kNoSuchFrame; 607 return kNoSuchFrame;
549 } 608 }
550 609
551 } // namespace webdriver 610 } // namespace webdriver
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698