Home | History | Annotate | Download | only in chromedriver
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/test/chromedriver/session.h"
      6 
      7 #include <list>
      8 
      9 #include "base/values.h"
     10 #include "chrome/test/chromedriver/chrome/chrome.h"
     11 #include "chrome/test/chromedriver/chrome/status.h"
     12 #include "chrome/test/chromedriver/chrome/version.h"
     13 #include "chrome/test/chromedriver/chrome/web_view.h"
     14 #include "chrome/test/chromedriver/logging.h"
     15 
     16 FrameInfo::FrameInfo(const std::string& parent_frame_id,
     17                      const std::string& frame_id,
     18                      const std::string& chromedriver_frame_id)
     19     : parent_frame_id(parent_frame_id),
     20       frame_id(frame_id),
     21       chromedriver_frame_id(chromedriver_frame_id) {}
     22 
     23 const int Session::kDefaultPageLoadTimeoutMs = 5 * 60 * 1000;
     24 
     25 Session::Session(const std::string& id)
     26     : id(id),
     27       quit(false),
     28       detach(false),
     29       sticky_modifiers(0),
     30       mouse_position(0, 0),
     31       implicit_wait(0),
     32       page_load_timeout(kDefaultPageLoadTimeoutMs),
     33       script_timeout(0) {
     34 }
     35 
     36 Session::Session(const std::string& id, scoped_ptr<Chrome> chrome)
     37     : id(id),
     38       quit(false),
     39       detach(false),
     40       chrome(chrome.Pass()),
     41       sticky_modifiers(0),
     42       mouse_position(0, 0),
     43       implicit_wait(0),
     44       page_load_timeout(kDefaultPageLoadTimeoutMs),
     45       script_timeout(0),
     46       capabilities(CreateCapabilities()) {
     47 }
     48 
     49 Session::~Session() {}
     50 
     51 Status Session::GetTargetWindow(WebView** web_view) {
     52   if (!chrome)
     53     return Status(kNoSuchWindow, "no chrome started in this session");
     54 
     55   Status status = chrome->GetWebViewById(window, web_view);
     56   if (status.IsError())
     57     status = Status(kNoSuchWindow, "target window already closed", status);
     58   return status;
     59 }
     60 
     61 void Session::SwitchToTopFrame() {
     62   frames.clear();
     63 }
     64 
     65 void Session::SwitchToSubFrame(const std::string& frame_id,
     66                                const std::string& chromedriver_frame_id) {
     67   std::string parent_frame_id;
     68   if (!frames.empty())
     69     parent_frame_id = frames.back().frame_id;
     70   frames.push_back(FrameInfo(parent_frame_id, frame_id, chromedriver_frame_id));
     71 }
     72 
     73 std::string Session::GetCurrentFrameId() const {
     74   if (frames.empty())
     75     return std::string();
     76   return frames.back().frame_id;
     77 }
     78 
     79 scoped_ptr<base::DictionaryValue> Session::CreateCapabilities() {
     80   scoped_ptr<base::DictionaryValue> caps(new base::DictionaryValue());
     81   caps->SetString("browserName", "chrome");
     82   caps->SetString("version", chrome->GetVersion());
     83   caps->SetString("chrome.chromedriverVersion", kChromeDriverVersion);
     84   caps->SetString("platform", chrome->GetOperatingSystemName());
     85   caps->SetBoolean("javascriptEnabled", true);
     86   caps->SetBoolean("takesScreenshot", true);
     87   caps->SetBoolean("handlesAlerts", true);
     88   caps->SetBoolean("databaseEnabled", true);
     89   caps->SetBoolean("locationContextEnabled", true);
     90   caps->SetBoolean("applicationCacheEnabled", false);
     91   caps->SetBoolean("browserConnectionEnabled", false);
     92   caps->SetBoolean("cssSelectorsEnabled", true);
     93   caps->SetBoolean("webStorageEnabled", true);
     94   caps->SetBoolean("rotatable", false);
     95   caps->SetBoolean("acceptSslCerts", true);
     96   caps->SetBoolean("nativeEvents", true);
     97   return caps.Pass();
     98 }
     99