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

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: fixed nit Created 9 years, 9 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 <sstream> 7 #include <sstream>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base64.h"
10 #include "base/command_line.h" 11 #include "base/command_line.h"
11 #include "base/file_path.h" 12 #include "base/file_path.h"
12 #include "base/file_util.h" 13 #include "base/file_util.h"
13 #include "base/json/json_reader.h" 14 #include "base/json/json_reader.h"
14 #include "base/json/json_writer.h" 15 #include "base/json/json_writer.h"
15 #include "base/logging.h" 16 #include "base/logging.h"
16 #include "base/message_loop_proxy.h" 17 #include "base/message_loop_proxy.h"
17 #include "base/process.h" 18 #include "base/process.h"
18 #include "base/process_util.h" 19 #include "base/process_util.h"
19 #include "base/scoped_ptr.h" 20 #include "base/scoped_ptr.h"
21 #include "base/scoped_temp_dir.h"
20 #include "base/stringprintf.h" 22 #include "base/stringprintf.h"
21 #include "base/string_number_conversions.h" 23 #include "base/string_number_conversions.h"
22 #include "base/string_split.h" 24 #include "base/string_split.h"
23 #include "base/string_util.h" 25 #include "base/string_util.h"
24 #include "base/synchronization/waitable_event.h" 26 #include "base/synchronization/waitable_event.h"
25 #include "base/test/test_timeouts.h" 27 #include "base/test/test_timeouts.h"
26 #include "base/threading/platform_thread.h" 28 #include "base/threading/platform_thread.h"
27 #include "base/time.h" 29 #include "base/time.h"
28 #include "base/utf_string_conversions.h" 30 #include "base/utf_string_conversions.h"
29 #include "base/values.h" 31 #include "base/values.h"
(...skipping 28 matching lines...) Expand all
58 bool success = false; 60 bool success = false;
59 if (thread_.Start()) { 61 if (thread_.Start()) {
60 RunSessionTask(NewRunnableMethod( 62 RunSessionTask(NewRunnableMethod(
61 this, 63 this,
62 &Session::InitOnSessionThread, 64 &Session::InitOnSessionThread,
63 browser_dir, 65 browser_dir,
64 &success)); 66 &success));
65 } else { 67 } else {
66 LOG(ERROR) << "Cannot start session thread"; 68 LOG(ERROR) << "Cannot start session thread";
67 } 69 }
70
68 if (!success) 71 if (!success)
69 delete this; 72 delete this;
73
70 return success; 74 return success;
71 } 75 }
72 76
73 void Session::Terminate() { 77 void Session::Terminate() {
74 RunSessionTask(NewRunnableMethod( 78 RunSessionTask(NewRunnableMethod(
75 this, 79 this,
76 &Session::TerminateOnSessionThread)); 80 &Session::TerminateOnSessionThread));
77 delete this; 81 delete this;
78 } 82 }
79 83
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 bool Session::GetWindowIds(std::vector<int>* window_ids) { 352 bool Session::GetWindowIds(std::vector<int>* window_ids) {
349 bool success = false; 353 bool success = false;
350 RunSessionTask(NewRunnableMethod( 354 RunSessionTask(NewRunnableMethod(
351 automation_.get(), 355 automation_.get(),
352 &Automation::GetTabIds, 356 &Automation::GetTabIds,
353 window_ids, 357 window_ids,
354 &success)); 358 &success));
355 return success; 359 return success;
356 } 360 }
357 361
362 bool Session::CaptureEntirePageAsPNG(const FilePath& path) {
363 bool success = false;
364 RunSessionTask(NewRunnableMethod(
365 automation_.get(),
366 &Automation::CaptureEntirePageAsPNG,
367 current_window_id_,
368 path,
369 &success));
370 return success;
371 }
372
373 bool Session::ScreenshotAsBase64(std::string* screenshot) {
374 ScopedTempDir screenshots_dir;
375
376 // Create a temp directory for screenshots.
377 if (!screenshots_dir.CreateUniqueTempDir()) {
378 LOG(ERROR) << "Could not make a temp directory for screenshots";
379 return false;
380 }
381
382 // Generate a temp file name for the screenshot
383 std::string file = screenshots_dir.path().value() + "screen";
kkania 2011/03/07 18:15:17 instead of all this code, you should be able to ju
Joe 2011/03/11 22:12:48 That would keep the file around until the session
384
385 #ifdef OS_POSIX
386 FilePath path = FilePath(file.c_str());
387 #elif OS_WIN
388 FilePath path = FilePath(ASCIIToWide(file.s_ctr()));
389 #endif
390
391 // Capture the current tab as a PNG file.
392 if (!CaptureEntirePageAsPNG(path)) {
393 LOG(ERROR) << "Capturing tab page as PNG failed";
394 return false;
395 }
396
397 std::string raw_bytes;
398 if (!file_util::ReadFileToString(path, &raw_bytes)) {
399 LOG(ERROR) << "Opening PNG file failed";
400 return false;
401 }
402
403 // Make a base64 encoded string for the raw PNG data
404 if (!base::Base64Encode(raw_bytes, screenshot)) {
405 LOG(ERROR) << "Encoding the PNG to base64 format failed";
406 return false;
407 }
408
409 return true;
410 }
411
358 ErrorCode Session::SwitchToWindow(const std::string& name) { 412 ErrorCode Session::SwitchToWindow(const std::string& name) {
359 int switch_to_id = 0; 413 int switch_to_id = 0;
360 int name_no = 0; 414 int name_no = 0;
361 if (base::StringToInt(name, &name_no)) { 415 if (base::StringToInt(name, &name_no)) {
362 bool does_exist = false; 416 bool does_exist = false;
363 RunSessionTask(NewRunnableMethod( 417 RunSessionTask(NewRunnableMethod(
364 automation_.get(), 418 automation_.get(),
365 &Automation::DoesTabExist, 419 &Automation::DoesTabExist,
366 name_no, 420 name_no,
367 &does_exist)); 421 &does_exist));
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 if (!automation_.get()) 607 if (!automation_.get())
554 return true; 608 return true;
555 bool success = false; 609 bool success = false;
556 RunSessionTask(NewRunnableMethod( 610 RunSessionTask(NewRunnableMethod(
557 automation_.get(), 611 automation_.get(),
558 &Automation::WaitForAllTabsToStopLoading, 612 &Automation::WaitForAllTabsToStopLoading,
559 &success)); 613 &success));
560 return success; 614 return success;
561 } 615 }
562 616
617 std::string Session::Id() const {
618 return id_;
619 }
620
621 int Session::ImplicitWait() const {
622 return implicit_wait_;
623 }
624
625 void Session::SetImplicitWait(const int timeout) {
626 implicit_wait_ = timeout > 0 ? timeout : 0;
627 }
628
629 Session::InputSpeed Session::Speed() const {
630 return speed_;
631 }
632
633 void Session::SetSpeed(const InputSpeed& speed) {
634 speed_ = speed;
635 }
636
637 std::string Session::CurrentFrameXPath() const {
638 return current_frame_xpath_;
639 }
640
641 void Session::SetCurrentFrameXPath(const std::string& xpath) {
kkania 2011/03/07 18:15:17 Why did you change these func names?
642 current_frame_xpath_ = xpath;
643 }
644
645 int Session::CurrentWindowId() const {
646 return current_window_id_;
647 }
648
563 void Session::RunSessionTask(Task* task) { 649 void Session::RunSessionTask(Task* task) {
564 base::WaitableEvent done_event(false, false); 650 base::WaitableEvent done_event(false, false);
565 thread_.message_loop_proxy()->PostTask(FROM_HERE, NewRunnableMethod( 651 thread_.message_loop_proxy()->PostTask(FROM_HERE, NewRunnableMethod(
566 this, 652 this,
567 &Session::RunSessionTaskOnSessionThread, 653 &Session::RunSessionTaskOnSessionThread,
568 task, 654 task,
569 &done_event)); 655 &done_event));
570 done_event.Wait(); 656 done_event.Wait();
571 } 657 }
572 658
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 } 846 }
761 } else { 847 } else {
762 LOG(ERROR) << "Location atom returned non-dictionary type"; 848 LOG(ERROR) << "Location atom returned non-dictionary type";
763 code = kUnknownError; 849 code = kUnknownError;
764 } 850 }
765 } 851 }
766 return code; 852 return code;
767 } 853 }
768 854
769 } // namespace webdriver 855 } // namespace webdriver
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698