1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/webdriver/session_manager.h"
#include <vector>
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/message_loop_proxy.h"
#include "base/process.h"
#include "base/process_util.h"
#include "base/string_util.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/test/test_timeouts.h"
#include "base/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/test_launcher_utils.h"
#include "chrome/test/webdriver/utility_functions.h"
#include "third_party/webdriver/atoms.h"
namespace webdriver {
Session::Session(const std::string& id)
: thread_(id.c_str()),
id_(id),
window_num_(0),
implicit_wait_(0),
current_frame_xpath_("") {
}
Session::~Session() {}
bool Session::Init() {
if (!thread_.Start()) {
LOG(ERROR) << "Cannot start session thread";
return false;
}
bool success = false;
RunSessionTask(NewRunnableMethod(
this,
&Session::InitOnSessionThread,
&success));
return success;
}
void Session::Terminate() {
RunSessionTask(NewRunnableMethod(
this,
&Session::TerminateOnSessionThread));
}
ErrorCode Session::ExecuteScript(const std::string& script,
const ListValue* const args,
Value** value) {
std::string args_as_json;
base::JSONWriter::Write(static_cast<const Value* const>(args),
/*pretty_print=*/false,
&args_as_json);
std::string jscript = "window.domAutomationController.send((function(){" +
// Every injected script is fed through the executeScript atom. This atom
// will catch any errors that are thrown and convert them to the
// appropriate JSON structure.
build_atom(EXECUTE_SCRIPT, sizeof EXECUTE_SCRIPT) +
"var result = executeScript(function(){" + script + "}," +
args_as_json + ");return JSON.stringify(result);})());";
// Should we also log the script that's being executed? It could be several KB
// in size and will add lots of noise to the logs.
VLOG(1) << "Executing script in frame: " << current_frame_xpath_;
std::string result;
bool success;
RunSessionTask(NewRunnableMethod(
automation_.get(),
&Automation::ExecuteScript,
current_frame_xpath_,
jscript,
&result,
&success));
if (!success) {
*value = Value::CreateStringValue(
"Unknown internal script execution failure");
return kUnknownError;
}
VLOG(1) << "...script result: " << result;
scoped_ptr<Value> r(base::JSONReader::ReadAndReturnError(
result, true, NULL, NULL));
if (!r.get()) {
*value = Value::CreateStringValue(
"Internal script execution error: failed to parse script result");
return kUnknownError;
}
if (r->GetType() != Value::TYPE_DICTIONARY) {
std::ostringstream stream;
stream << "Internal script execution error: script result must be a "
<< print_valuetype(Value::TYPE_DICTIONARY) << ", but was "
<< print_valuetype(r->GetType()) << ": " << result;
*value = Value::CreateStringValue(stream.str());
return kUnknownError;
}
DictionaryValue* result_dict = static_cast<DictionaryValue*>(r.get());
Value* tmp;
if (result_dict->Get("value", &tmp)) {
// result_dict owns the returned value, so we need to make a copy.
*value = tmp->DeepCopy();
} else {
// "value" was not defined in the returned dictionary, set to null.
*value = Value::CreateNullValue();
}
int status;
if (!result_dict->GetInteger("status", &status)) {
NOTREACHED() << "...script did not return a status flag.";
}
return static_cast<ErrorCode>(status);
}
bool Session::NavigateToURL(const std::string& url) {
bool success = false;
RunSessionTask(NewRunnableMethod(
automation_.get(),
&Automation::NavigateToURL,
url,
&success));
return success;
}
bool Session::GoForward() {
bool success = false;
RunSessionTask(NewRunnableMethod(
automation_.get(),
&Automation::GoForward,
&success));
return success;
}
bool Session::GoBack() {
bool success = false;
RunSessionTask(NewRunnableMethod(
automation_.get(),
&Automation::GoBack,
&success));
return success;
}
bool Session::Reload() {
bool success = false;
RunSessionTask(NewRunnableMethod(
automation_.get(),
&Automation::Reload,
&success));
return success;
}
bool Session::GetURL(std::string* url) {
bool success = false;
RunSessionTask(NewRunnableMethod(
automation_.get(),
&Automation::GetURL,
url,
&success));
return success;
}
bool Session::GetTabTitle(std::string* tab_title) {
bool success = false;
RunSessionTask(NewRunnableMethod(
automation_.get(),
&Automation::GetTabTitle,
tab_title,
&success));
return success;
}
void Session::RunSessionTask(Task* task) {
base::WaitableEvent done_event(false, false);
thread_.message_loop_proxy()->PostTask(FROM_HERE, NewRunnableMethod(
this,
&Session::RunSessionTaskOnSessionThread,
task,
&done_event));
done_event.Wait();
}
void Session::RunSessionTaskOnSessionThread(Task* task,
base::WaitableEvent* done_event) {
task->Run();
delete task;
done_event->Signal();
}
void Session::InitOnSessionThread(bool* success) {
automation_.reset(new Automation());
automation_->Init(success);
}
void Session::TerminateOnSessionThread() {
automation_->Terminate();
automation_.reset();
}
} // namespace webdriver
|