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

Side by Side Diff: chrome/test/chromedriver/commands_unittest.cc

Issue 11884058: [chromedriver] Implement commands: findChildElement, findChildElements. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 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) 2012 The Chromium Authors. All rights reserved. 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 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 <string> 5 #include <string>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/file_path.h" 10 #include "base/file_path.h"
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 scoped_ptr<base::Value> value; 197 scoped_ptr<base::Value> value;
198 ASSERT_EQ(kUnknownError, ExecuteQuit(&map, &session, params, &value).code()); 198 ASSERT_EQ(kUnknownError, ExecuteQuit(&map, &session, params, &value).code());
199 ASSERT_FALSE(map.Has(session.id)); 199 ASSERT_FALSE(map.Has(session.id));
200 ASSERT_FALSE(value.get()); 200 ASSERT_FALSE(value.get());
201 } 201 }
202 202
203 namespace { 203 namespace {
204 204
205 class FindElementChrome : public StubChrome { 205 class FindElementChrome : public StubChrome {
206 public: 206 public:
207 explicit FindElementChrome(bool element_exists) 207 FindElementChrome(bool element_exists, bool only_one, int query_count)
kkania 2013/01/15 22:24:36 this is a bit ugly. just wondering if there's a cl
chrisgao (Use stgao instead) 2013/01/16 19:06:24 Done.
208 : element_exists_(element_exists), called_(false) {} 208 : element_exists_(element_exists), only_one_(only_one),
209 query_count_(query_count), current_count_(0) {
210 if (only_one_) {
211 base::DictionaryValue element;
212 element.SetString("ELEMENT", "1");
213 result_.reset(element.DeepCopy());
214 } else {
215 base::DictionaryValue element1;
216 element1.SetString("ELEMENT", "1");
217 base::DictionaryValue element2;
218 element2.SetString("ELEMENT", "2");
219 base::ListValue list;
220 list.Append(element1.DeepCopy());
221 list.Append(element2.DeepCopy());
222 result_.reset(list.DeepCopy());
223 }
224 }
209 virtual ~FindElementChrome() {} 225 virtual ~FindElementChrome() {}
210 226
227 const base::Value* GetResult() { return result_.get(); }
kkania 2013/01/15 22:24:36 i don't understand the purpose of this
chrisgao (Use stgao instead) 2013/01/16 19:06:24 Used to verified that the returned result is expec
211 const std::string& GetFrame() { return frame_; } 228 const std::string& GetFrame() { return frame_; }
212 const std::string& GetFunction() { return function_; } 229 const std::string& GetFunction() { return function_; }
213 const base::ListValue* GetArgs() { return args_.get(); } 230 const base::ListValue* GetArgs() { return args_.get(); }
214 231
215 // Overridden from Chrome: 232 // Overridden from Chrome:
216 virtual Status CallFunction(const std::string& frame, 233 virtual Status CallFunction(const std::string& frame,
217 const std::string& function, 234 const std::string& function,
218 const base::ListValue& args, 235 const base::ListValue& args,
219 scoped_ptr<base::Value>* result) OVERRIDE { 236 scoped_ptr<base::Value>* result) OVERRIDE {
220 if (element_exists_ && called_) { 237 ++current_count_;
221 base::DictionaryValue element; 238 if (element_exists_ && current_count_ >= query_count_) {
222 element.SetString("ELEMENT", "1"); 239 result->reset(result_->DeepCopy());
223 result->reset(element.DeepCopy());
224 frame_ = frame; 240 frame_ = frame;
225 function_ = function; 241 function_ = function;
226 args_.reset(args.DeepCopy()); 242 args_.reset(args.DeepCopy());
227 } else { 243 } else {
228 result->reset(base::Value::CreateNullValue()); 244 result->reset(base::Value::CreateNullValue());
229 } 245 }
230 called_ = true;
231 return Status(kOk); 246 return Status(kOk);
232 } 247 }
233 private: 248 private:
234 bool element_exists_; 249 bool element_exists_;
235 bool called_; 250 bool only_one_;
251 int query_count_;
252 int current_count_;
253 scoped_ptr<base::Value> result_;
236 std::string frame_; 254 std::string frame_;
237 std::string function_; 255 std::string function_;
238 scoped_ptr<base::ListValue> args_; 256 scoped_ptr<base::ListValue> args_;
239 }; 257 };
240 258
241 } // namespace 259 } // namespace
242 260
243 TEST(CommandsTest, SuccessfulFindElement) { 261 TEST(CommandsTest, SuccessfulFindElement) {
244 FindElementChrome* chrome = new FindElementChrome(true); 262 FindElementChrome* chrome = new FindElementChrome(true, true, 2);
245 Session session("id", scoped_ptr<Chrome>(chrome)); 263 Session session("id", scoped_ptr<Chrome>(chrome));
246 session.implicit_wait = 100; 264 session.implicit_wait = 100;
247 session.frame = "frame_id1"; 265 session.frame = "frame_id1";
248 base::DictionaryValue params; 266 base::DictionaryValue params;
249 params.SetString("using", "id"); 267 params.SetString("using", "id");
250 params.SetString("value", "a"); 268 params.SetString("value", "a");
251 scoped_ptr<base::Value> value; 269 scoped_ptr<base::Value> value;
252 ASSERT_EQ(kOk, ExecuteFindElement(&session, params, &value).code()); 270 ASSERT_EQ(kOk, ExecuteFindElement(&session, params, &value).code());
253 base::DictionaryValue* element; 271 ASSERT_TRUE(value.get());
254 ASSERT_TRUE(value->GetAsDictionary(&element)); 272 ASSERT_TRUE(value->Equals(chrome->GetResult()));
255 ASSERT_EQ(1U, element->size());
256 std::string element_id;
257 ASSERT_TRUE(element->GetString("ELEMENT", &element_id));
258 ASSERT_EQ("1", element_id);
259 ASSERT_EQ("frame_id1", chrome->GetFrame()); 273 ASSERT_EQ("frame_id1", chrome->GetFrame());
260 ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT), 274 ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT),
261 chrome->GetFunction()); 275 chrome->GetFunction());
262 const base::ListValue* args = chrome->GetArgs(); 276 const base::ListValue* args = chrome->GetArgs();
263 ASSERT_TRUE(args); 277 ASSERT_TRUE(args);
264 ASSERT_EQ(1U, args->GetSize()); 278 base::DictionaryValue param;
265 const base::DictionaryValue* dict; 279 param.SetString("id", "a");
266 ASSERT_TRUE(args->GetDictionary(0U, &dict)); 280 base::ListValue expected_args;
267 ASSERT_EQ(1U, dict->size()); 281 expected_args.Append(param.DeepCopy());
268 std::string id; 282 ASSERT_TRUE(expected_args.Equals(args));
269 ASSERT_TRUE(dict->GetString("id", &id));
270 ASSERT_EQ("a", id);
271 } 283 }
272 284
273 TEST(CommandsTest, FailedFindElement) { 285 TEST(CommandsTest, FailedFindElement) {
274 Session session("id", scoped_ptr<Chrome>(new FindElementChrome(false))); 286 Session session("id",
287 scoped_ptr<Chrome>(new FindElementChrome(false, true, 1)));
kkania 2013/01/15 22:24:36 4 spaces; fix others too
chrisgao (Use stgao instead) 2013/01/16 19:06:24 Done.
275 session.implicit_wait = 0; 288 session.implicit_wait = 0;
276 base::DictionaryValue params; 289 base::DictionaryValue params;
277 params.SetString("using", "id"); 290 params.SetString("using", "id");
278 params.SetString("value", "a"); 291 params.SetString("value", "a");
279 scoped_ptr<base::Value> value; 292 scoped_ptr<base::Value> value;
280 ASSERT_EQ(kNoSuchElement, 293 ASSERT_EQ(kNoSuchElement,
281 ExecuteFindElement(&session, params, &value).code()); 294 ExecuteFindElement(&session, params, &value).code());
282 } 295 }
283 296
284 namespace {
285
286 class FindElementsChrome : public StubChrome {
287 public:
288 explicit FindElementsChrome(bool element_exists)
289 : element_exists_(element_exists), called_(false) {}
290 virtual ~FindElementsChrome() {}
291
292 const std::string& GetFrame() { return frame_; }
293 const std::string& GetFunction() { return function_; }
294 const base::ListValue* GetArgs() { return args_.get(); }
295
296 // Overridden from Chrome:
297 virtual Status CallFunction(const std::string& frame,
298 const std::string& function,
299 const base::ListValue& args,
300 scoped_ptr<base::Value>* result) OVERRIDE {
301 if (element_exists_ && called_) {
302 base::DictionaryValue element1;
303 element1.SetString("ELEMENT", "1");
304 base::DictionaryValue element2;
305 element2.SetString("ELEMENT", "2");
306 base::ListValue list;
307 list.Append(element1.DeepCopy());
308 list.Append(element2.DeepCopy());
309 result->reset(list.DeepCopy());
310 frame_ = frame;
311 function_ = function;
312 args_.reset(args.DeepCopy());
313 } else {
314 result->reset(new base::ListValue());
315 }
316 called_ = true;
317 return Status(kOk);
318 }
319 private:
320 bool element_exists_;
321 bool called_;
322 std::string frame_;
323 std::string function_;
324 scoped_ptr<base::ListValue> args_;
325 };
326
327 } //namespace
328
329 TEST(CommandsTest, SuccessfulFindElements) { 297 TEST(CommandsTest, SuccessfulFindElements) {
330 FindElementsChrome* chrome = new FindElementsChrome(true); 298 FindElementChrome* chrome = new FindElementChrome(true, false, 2);
331 Session session("id", scoped_ptr<Chrome>(chrome)); 299 Session session("id", scoped_ptr<Chrome>(chrome));
332 session.implicit_wait = 100; 300 session.implicit_wait = 100;
333 session.frame = "frame_id2"; 301 session.frame = "frame_id2";
334 base::DictionaryValue params; 302 base::DictionaryValue params;
335 params.SetString("using", "name"); 303 params.SetString("using", "name");
336 params.SetString("value", "b"); 304 params.SetString("value", "b");
337 scoped_ptr<base::Value> value; 305 scoped_ptr<base::Value> value;
338 ASSERT_EQ(kOk, ExecuteFindElements(&session, params, &value).code()); 306 ASSERT_EQ(kOk, ExecuteFindElements(&session, params, &value).code());
339 base::ListValue* list; 307 ASSERT_TRUE(value.get());
340 ASSERT_TRUE(value->GetAsList(&list)); 308 ASSERT_TRUE(value->Equals(chrome->GetResult()));
341 ASSERT_EQ(2U, list->GetSize());
342 base::DictionaryValue* element1;
343 ASSERT_TRUE(list->GetDictionary(0U, &element1));
344 ASSERT_EQ(1U, element1->size());
345 std::string element1_id;
346 ASSERT_TRUE(element1->GetString("ELEMENT", &element1_id));
347 ASSERT_EQ("1", element1_id);
348 base::DictionaryValue* element2;
349 ASSERT_TRUE(list->GetDictionary(1U, &element2));
350 ASSERT_EQ(1U, element2->size());
351 std::string element2_id;
352 ASSERT_TRUE(element2->GetString("ELEMENT", &element2_id));
353 ASSERT_EQ("2", element2_id);
354 ASSERT_EQ("frame_id2", chrome->GetFrame()); 309 ASSERT_EQ("frame_id2", chrome->GetFrame());
355 ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS), 310 ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS),
356 chrome->GetFunction()); 311 chrome->GetFunction());
357 const base::ListValue* args = chrome->GetArgs(); 312 const base::ListValue* args = chrome->GetArgs();
358 ASSERT_TRUE(args); 313 ASSERT_TRUE(args);
359 ASSERT_EQ(1U, args->GetSize()); 314 base::DictionaryValue param;
360 const base::DictionaryValue* dict; 315 param.SetString("name", "b");
361 ASSERT_TRUE(args->GetDictionary(0U, &dict)); 316 base::ListValue expected_args;
362 ASSERT_EQ(1U, dict->size()); 317 expected_args.Append(param.DeepCopy());
363 std::string name; 318 ASSERT_TRUE(expected_args.Equals(args));
364 ASSERT_TRUE(dict->GetString("name", &name));
365 ASSERT_EQ("b", name);
366 } 319 }
367 320
368 TEST(CommandsTest, FailedFindElements) { 321 TEST(CommandsTest, FailedFindElements) {
369 Session session("id", scoped_ptr<Chrome>(new FindElementsChrome(false))); 322 Session session("id",
323 scoped_ptr<Chrome>(new FindElementChrome(false, false, 1)));
370 session.implicit_wait = 0; 324 session.implicit_wait = 0;
371 base::DictionaryValue params; 325 base::DictionaryValue params;
372 params.SetString("using", "id"); 326 params.SetString("using", "id");
373 params.SetString("value", "a"); 327 params.SetString("value", "a");
374 scoped_ptr<base::Value> value; 328 scoped_ptr<base::Value> value;
375 ASSERT_EQ(kOk, ExecuteFindElements(&session, params, &value).code()); 329 ASSERT_EQ(kOk, ExecuteFindElements(&session, params, &value).code());
376 base::ListValue* list; 330 base::ListValue* list;
377 ASSERT_TRUE(value->GetAsList(&list)); 331 ASSERT_TRUE(value->GetAsList(&list));
378 ASSERT_EQ(0U, list->GetSize()); 332 ASSERT_EQ(0U, list->GetSize());
379 } 333 }
380 334
335 TEST(CommandsTest, SuccessfulFindChildElement) {
336 FindElementChrome* chrome = new FindElementChrome(true, true, 2);
337 Session session("id", scoped_ptr<Chrome>(chrome));
338 session.implicit_wait = 100;
chrisgao (Use stgao instead) 2013/01/15 20:53:59 I think we need these new testcases, but maybe we
kkania 2013/01/15 22:24:36 I think it'd be better to test it in both.
339 session.frame = "frame_id3";
340 base::DictionaryValue params;
341 params.SetString("using", "tag name");
342 params.SetString("value", "div");
343 params.SetString("id", "1");
344 scoped_ptr<base::Value> value;
345 ASSERT_EQ(kOk, ExecuteFindChildElement(&session, params, &value).code());
346 ASSERT_TRUE(value.get());
347 ASSERT_TRUE(value->Equals(chrome->GetResult()));
348 ASSERT_EQ("frame_id3", chrome->GetFrame());
kkania 2013/01/15 22:24:36 i think a lot of this code is duplicated, right? c
chrisgao (Use stgao instead) 2013/01/16 19:06:24 Done.
349 ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT),
350 chrome->GetFunction());
351 const base::ListValue* args = chrome->GetArgs();
352 ASSERT_TRUE(args);
353 base::DictionaryValue locator_param;
354 locator_param.SetString("tag name", "div");
355 base::DictionaryValue root_element_param;
356 root_element_param.SetString("ELEMENT", "1");
357 base::ListValue expected_args;
358 expected_args.Append(locator_param.DeepCopy());
359 expected_args.Append(root_element_param.DeepCopy());
360 ASSERT_TRUE(expected_args.Equals(args));
361 }
362
363 TEST(CommandsTest, FailedFindChildElement) {
364 Session session("id",
365 scoped_ptr<Chrome>(new FindElementChrome(false, true, 1)));
366 session.implicit_wait = 0;
367 base::DictionaryValue params;
368 params.SetString("using", "id");
369 params.SetString("value", "a");
370 params.SetString("id", "1");
371 scoped_ptr<base::Value> value;
372 ASSERT_EQ(kNoSuchElement,
373 ExecuteFindChildElement(&session, params, &value).code());
374 }
375
376 TEST(CommandsTest, SuccessfulFindChildElements) {
377 FindElementChrome* chrome = new FindElementChrome(true, false, 2);
378 Session session("id", scoped_ptr<Chrome>(chrome));
379 session.implicit_wait = 100;
380 session.frame = "frame_id4";
381 base::DictionaryValue params;
382 params.SetString("using", "class name");
383 params.SetString("value", "c");
384 params.SetString("id", "1");
385 scoped_ptr<base::Value> value;
386 ASSERT_EQ(kOk, ExecuteFindChildElements(&session, params, &value).code());
387 ASSERT_TRUE(value.get());
388 ASSERT_TRUE(value->Equals(chrome->GetResult()));
389 ASSERT_EQ("frame_id4", chrome->GetFrame());
390 ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS),
391 chrome->GetFunction());
392 const base::ListValue* args = chrome->GetArgs();
393 ASSERT_TRUE(args);
394 base::DictionaryValue locator_param;
395 locator_param.SetString("class name", "c");
396 base::DictionaryValue root_element_param;
397 root_element_param.SetString("ELEMENT", "1");
398 base::ListValue expected_args;
399 expected_args.Append(locator_param.DeepCopy());
400 expected_args.Append(root_element_param.DeepCopy());
401 ASSERT_TRUE(expected_args.Equals(args));
402 }
403
404 TEST(CommandsTest, FailedFindChildElements) {
405 Session session("id",
406 scoped_ptr<Chrome>(new FindElementChrome(false, false, 1)));
407 session.implicit_wait = 0;
kkania 2013/01/15 22:24:36 isn't this set to 0 by default; fix the other = 0
chrisgao (Use stgao instead) 2013/01/16 19:06:24 Done.
408 base::DictionaryValue params;
409 params.SetString("using", "id");
410 params.SetString("value", "a");
411 params.SetString("id", "1");
412 scoped_ptr<base::Value> value;
413 ASSERT_EQ(kOk, ExecuteFindChildElements(&session, params, &value).code());
414 base::ListValue* list;
415 ASSERT_TRUE(value->GetAsList(&list));
416 ASSERT_EQ(0U, list->GetSize());
417 }
418
381 namespace { 419 namespace {
382 420
383 class ErrorCallFunctionChrome : public StubChrome { 421 class ErrorCallFunctionChrome : public StubChrome {
384 public: 422 public:
385 ErrorCallFunctionChrome() {} 423 explicit ErrorCallFunctionChrome(StatusCode code) : code_(code) {}
386 virtual ~ErrorCallFunctionChrome() {} 424 virtual ~ErrorCallFunctionChrome() {}
387 425
388 // Overridden from Chrome: 426 // Overridden from Chrome:
389 virtual Status CallFunction(const std::string& frame, 427 virtual Status CallFunction(const std::string& frame,
390 const std::string& function, 428 const std::string& function,
391 const base::ListValue& args, 429 const base::ListValue& args,
392 scoped_ptr<base::Value>* result) OVERRIDE { 430 scoped_ptr<base::Value>* result) OVERRIDE {
393 return Status(kUnknownError); 431 return Status(code_);
394 } 432 }
433
434 private:
435 StatusCode code_;
395 }; 436 };
396 437
397 } // namespace 438 } // namespace
398 439
399 TEST(CommandsTest, ErrorFindElement) { 440 TEST(CommandsTest, ErrorFindElement) {
400 Session session("id", scoped_ptr<Chrome>(new ErrorCallFunctionChrome())); 441 Session session("id",
442 scoped_ptr<Chrome>(new ErrorCallFunctionChrome(kUnknownError)));
401 base::DictionaryValue params; 443 base::DictionaryValue params;
402 params.SetString("using", "id"); 444 params.SetString("using", "id");
403 params.SetString("value", "a"); 445 params.SetString("value", "a");
404 scoped_ptr<base::Value> value; 446 scoped_ptr<base::Value> value;
405 ASSERT_EQ(kUnknownError, ExecuteFindElement(&session, params, &value).code()); 447 ASSERT_EQ(kUnknownError, ExecuteFindElement(&session, params, &value).code());
406 ASSERT_EQ(kUnknownError, 448 ASSERT_EQ(kUnknownError,
407 ExecuteFindElements(&session, params, &value).code()); 449 ExecuteFindElements(&session, params, &value).code());
408 } 450 }
451
452 TEST(CommandsTest, ErrorFindChildElement) {
453 Session session("id",
454 scoped_ptr<Chrome>(new ErrorCallFunctionChrome(kStaleElementReference)));
455 base::DictionaryValue params;
456 params.SetString("using", "id");
457 params.SetString("value", "a");
458 params.SetString("id", "1");
459 scoped_ptr<base::Value> value;
460 ASSERT_EQ(kStaleElementReference,
461 ExecuteFindChildElement(&session, params, &value).code());
462 ASSERT_EQ(kStaleElementReference,
463 ExecuteFindChildElements(&session, params, &value).code());
464 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698