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

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: Address comments. 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 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 scoped_refptr<SessionAccessor>(new FakeSessionAccessor(&session))); 195 scoped_refptr<SessionAccessor>(new FakeSessionAccessor(&session)));
196 base::DictionaryValue params; 196 base::DictionaryValue params;
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 enum TestScenario {
206 kElementExistsQueryOnce = 0,
207 kElementExistsQueryTwice,
208 kElementNotExistsQueryOnce,
209 kElementExistsTimeout
210 };
211
205 class FindElementChrome : public StubChrome { 212 class FindElementChrome : public StubChrome {
206 public: 213 public:
207 explicit FindElementChrome(bool element_exists) 214 FindElementChrome(bool only_one, TestScenario scenario)
208 : element_exists_(element_exists), called_(false) {} 215 : only_one_(only_one), scenario_(scenario), current_count_(0) {
216 switch (scenario_) {
217 case kElementExistsQueryOnce:
218 case kElementExistsQueryTwice:
219 case kElementExistsTimeout: {
220 if (only_one_) {
221 base::DictionaryValue element;
222 element.SetString("ELEMENT", "1");
223 result_.reset(element.DeepCopy());
224 } else {
225 base::DictionaryValue element1;
226 element1.SetString("ELEMENT", "1");
227 base::DictionaryValue element2;
228 element2.SetString("ELEMENT", "2");
229 base::ListValue list;
230 list.Append(element1.DeepCopy());
231 list.Append(element2.DeepCopy());
232 result_.reset(list.DeepCopy());
233 }
234 break;
235 }
236 case kElementNotExistsQueryOnce: {
237 if (only_one_)
238 result_.reset(base::Value::CreateNullValue());
239 else
240 result_.reset(new base::ListValue());
241 break;
242 }
243 }
244 }
209 virtual ~FindElementChrome() {} 245 virtual ~FindElementChrome() {}
210 246
211 const std::string& GetFrame() { return frame_; } 247 void Verify(const std::string& expected_frame,
212 const std::string& GetFunction() { return function_; } 248 const base::ListValue* expected_args,
213 const base::ListValue* GetArgs() { return args_.get(); } 249 const base::Value* actrual_result) {
250 EXPECT_EQ(expected_frame, frame_);
251 std::string function;
252 if (only_one_)
253 function = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT);
254 else
255 function = webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS);
256 EXPECT_EQ(function, function_);
257 ASSERT_TRUE(args_.get());
258 EXPECT_TRUE(expected_args->Equals(args_.get()));
259 ASSERT_TRUE(actrual_result);
260 EXPECT_TRUE(result_->Equals(actrual_result));
261 }
214 262
215 // Overridden from Chrome: 263 // Overridden from Chrome:
216 virtual Status CallFunction(const std::string& frame, 264 virtual Status CallFunction(const std::string& frame,
217 const std::string& function, 265 const std::string& function,
218 const base::ListValue& args, 266 const base::ListValue& args,
219 scoped_ptr<base::Value>* result) OVERRIDE { 267 scoped_ptr<base::Value>* result) OVERRIDE {
220 if (element_exists_ && called_) { 268 ++current_count_;
221 base::DictionaryValue element; 269 if (scenario_ == kElementExistsTimeout ||
222 element.SetString("ELEMENT", "1"); 270 (scenario_ == kElementExistsQueryTwice && current_count_ == 1)) {
223 result->reset(element.DeepCopy()); 271 // Always return empty result when testing timeout.
272 if (only_one_)
273 result->reset(base::Value::CreateNullValue());
274 else
275 result->reset(new base::ListValue());
276 } else {
277 switch (scenario_) {
278 case kElementExistsQueryOnce:
279 case kElementNotExistsQueryOnce: {
280 EXPECT_EQ(1, current_count_);
281 break;
282 }
283 case kElementExistsQueryTwice: {
284 EXPECT_EQ(2, current_count_);
285 break;
286 }
287 default: {
288 break;
289 }
290 }
291
292 result->reset(result_->DeepCopy());
224 frame_ = frame; 293 frame_ = frame;
225 function_ = function; 294 function_ = function;
226 args_.reset(args.DeepCopy()); 295 args_.reset(args.DeepCopy());
227 } else {
228 result->reset(base::Value::CreateNullValue());
229 } 296 }
230 called_ = true;
231 return Status(kOk); 297 return Status(kOk);
232 } 298 }
299
233 private: 300 private:
234 bool element_exists_; 301 bool only_one_;
235 bool called_; 302 TestScenario scenario_;
303 int current_count_;
236 std::string frame_; 304 std::string frame_;
237 std::string function_; 305 std::string function_;
238 scoped_ptr<base::ListValue> args_; 306 scoped_ptr<base::ListValue> args_;
307 scoped_ptr<base::Value> result_;
239 }; 308 };
240 309
241 } // namespace 310 } // namespace
242 311
243 TEST(CommandsTest, SuccessfulFindElement) { 312 TEST(CommandsTest, SuccessfulFindElement) {
244 FindElementChrome* chrome = new FindElementChrome(true); 313 FindElementChrome* chrome =
245 Session session("id", scoped_ptr<Chrome>(chrome)); 314 new FindElementChrome(true, kElementExistsQueryTwice);
246 session.implicit_wait = 100; 315 Session session("id", scoped_ptr<Chrome>(chrome));
316 session.implicit_wait = 10;
247 session.frame = "frame_id1"; 317 session.frame = "frame_id1";
248 base::DictionaryValue params; 318 base::DictionaryValue params;
249 params.SetString("using", "id"); 319 params.SetString("using", "id");
250 params.SetString("value", "a"); 320 params.SetString("value", "a");
251 scoped_ptr<base::Value> value; 321 scoped_ptr<base::Value> result;
252 ASSERT_EQ(kOk, ExecuteFindElement(&session, params, &value).code()); 322 ASSERT_EQ(kOk, ExecuteFindElement(1, &session, params, &result).code());
253 base::DictionaryValue* element; 323 base::DictionaryValue param;
254 ASSERT_TRUE(value->GetAsDictionary(&element)); 324 param.SetString("id", "a");
255 ASSERT_EQ(1U, element->size()); 325 base::ListValue expected_args;
256 std::string element_id; 326 expected_args.Append(param.DeepCopy());
257 ASSERT_TRUE(element->GetString("ELEMENT", &element_id)); 327 chrome->Verify("frame_id1", &expected_args, result.get());
258 ASSERT_EQ("1", element_id);
259 ASSERT_EQ("frame_id1", chrome->GetFrame());
260 ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENT),
261 chrome->GetFunction());
262 const base::ListValue* args = chrome->GetArgs();
263 ASSERT_TRUE(args);
264 ASSERT_EQ(1U, args->GetSize());
265 const base::DictionaryValue* dict;
266 ASSERT_TRUE(args->GetDictionary(0U, &dict));
267 ASSERT_EQ(1U, dict->size());
268 std::string id;
269 ASSERT_TRUE(dict->GetString("id", &id));
270 ASSERT_EQ("a", id);
271 } 328 }
272 329
273 TEST(CommandsTest, FailedFindElement) { 330 TEST(CommandsTest, FailedFindElement) {
274 Session session("id", scoped_ptr<Chrome>(new FindElementChrome(false))); 331 Session session("id",
275 session.implicit_wait = 0; 332 scoped_ptr<Chrome>(
276 base::DictionaryValue params; 333 new FindElementChrome(true, kElementNotExistsQueryOnce)));
277 params.SetString("using", "id"); 334 base::DictionaryValue params;
278 params.SetString("value", "a"); 335 params.SetString("using", "id");
279 scoped_ptr<base::Value> value; 336 params.SetString("value", "a");
337 scoped_ptr<base::Value> result;
280 ASSERT_EQ(kNoSuchElement, 338 ASSERT_EQ(kNoSuchElement,
281 ExecuteFindElement(&session, params, &value).code()); 339 ExecuteFindElement(1, &session, params, &result).code());
282 } 340 }
283
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 341
329 TEST(CommandsTest, SuccessfulFindElements) { 342 TEST(CommandsTest, SuccessfulFindElements) {
330 FindElementsChrome* chrome = new FindElementsChrome(true); 343 FindElementChrome* chrome =
331 Session session("id", scoped_ptr<Chrome>(chrome)); 344 new FindElementChrome(false, kElementExistsQueryTwice);
332 session.implicit_wait = 100; 345 Session session("id", scoped_ptr<Chrome>(chrome));
346 session.implicit_wait = 10;
333 session.frame = "frame_id2"; 347 session.frame = "frame_id2";
334 base::DictionaryValue params; 348 base::DictionaryValue params;
335 params.SetString("using", "name"); 349 params.SetString("using", "name");
336 params.SetString("value", "b"); 350 params.SetString("value", "b");
337 scoped_ptr<base::Value> value; 351 scoped_ptr<base::Value> result;
338 ASSERT_EQ(kOk, ExecuteFindElements(&session, params, &value).code()); 352 ASSERT_EQ(kOk, ExecuteFindElements(1, &session, params, &result).code());
353 base::DictionaryValue param;
354 param.SetString("name", "b");
355 base::ListValue expected_args;
356 expected_args.Append(param.DeepCopy());
357 chrome->Verify("frame_id2", &expected_args, result.get());
358 }
359
360 TEST(CommandsTest, FailedFindElements) {
361 Session session("id",
362 scoped_ptr<Chrome>(
363 new FindElementChrome(false, kElementNotExistsQueryOnce)));
364 base::DictionaryValue params;
365 params.SetString("using", "id");
366 params.SetString("value", "a");
367 scoped_ptr<base::Value> result;
368 ASSERT_EQ(kOk, ExecuteFindElements(1, &session, params, &result).code());
339 base::ListValue* list; 369 base::ListValue* list;
340 ASSERT_TRUE(value->GetAsList(&list)); 370 ASSERT_TRUE(result->GetAsList(&list));
341 ASSERT_EQ(2U, list->GetSize()); 371 ASSERT_EQ(0U, list->GetSize());
342 base::DictionaryValue* element1; 372 }
343 ASSERT_TRUE(list->GetDictionary(0U, &element1)); 373
344 ASSERT_EQ(1U, element1->size()); 374 TEST(CommandsTest, SuccessfulFindChildElement) {
345 std::string element1_id; 375 FindElementChrome* chrome =
346 ASSERT_TRUE(element1->GetString("ELEMENT", &element1_id)); 376 new FindElementChrome(true, kElementExistsQueryTwice);
347 ASSERT_EQ("1", element1_id); 377 Session session("id", scoped_ptr<Chrome>(chrome));
348 base::DictionaryValue* element2; 378 session.implicit_wait = 10;
349 ASSERT_TRUE(list->GetDictionary(1U, &element2)); 379 session.frame = "frame_id3";
350 ASSERT_EQ(1U, element2->size()); 380 base::DictionaryValue params;
351 std::string element2_id; 381 params.SetString("using", "tag name");
352 ASSERT_TRUE(element2->GetString("ELEMENT", &element2_id)); 382 params.SetString("value", "div");
353 ASSERT_EQ("2", element2_id); 383 params.SetString("id", "1");
354 ASSERT_EQ("frame_id2", chrome->GetFrame()); 384 scoped_ptr<base::Value> result;
355 ASSERT_EQ(webdriver::atoms::asString(webdriver::atoms::FIND_ELEMENTS), 385 ASSERT_EQ(kOk, ExecuteFindChildElement(1, &session, params, &result).code());
356 chrome->GetFunction()); 386 base::DictionaryValue locator_param;
357 const base::ListValue* args = chrome->GetArgs(); 387 locator_param.SetString("tag name", "div");
358 ASSERT_TRUE(args); 388 base::DictionaryValue root_element_param;
359 ASSERT_EQ(1U, args->GetSize()); 389 root_element_param.SetString("ELEMENT", "1");
360 const base::DictionaryValue* dict; 390 base::ListValue expected_args;
361 ASSERT_TRUE(args->GetDictionary(0U, &dict)); 391 expected_args.Append(locator_param.DeepCopy());
362 ASSERT_EQ(1U, dict->size()); 392 expected_args.Append(root_element_param.DeepCopy());
363 std::string name; 393 chrome->Verify("frame_id3", &expected_args, result.get());
364 ASSERT_TRUE(dict->GetString("name", &name)); 394 }
365 ASSERT_EQ("b", name); 395
366 } 396 TEST(CommandsTest, FailedFindChildElement) {
367 397 Session session("id",
368 TEST(CommandsTest, FailedFindElements) { 398 scoped_ptr<Chrome>(
369 Session session("id", scoped_ptr<Chrome>(new FindElementsChrome(false))); 399 new FindElementChrome(true, kElementNotExistsQueryOnce)));
370 session.implicit_wait = 0; 400 base::DictionaryValue params;
371 base::DictionaryValue params; 401 params.SetString("using", "id");
372 params.SetString("using", "id"); 402 params.SetString("value", "a");
373 params.SetString("value", "a"); 403 params.SetString("id", "1");
374 scoped_ptr<base::Value> value; 404 scoped_ptr<base::Value> result;
375 ASSERT_EQ(kOk, ExecuteFindElements(&session, params, &value).code()); 405 ASSERT_EQ(kNoSuchElement,
406 ExecuteFindChildElement(1, &session, params, &result).code());
407 }
408
409 TEST(CommandsTest, SuccessfulFindChildElements) {
410 FindElementChrome* chrome =
411 new FindElementChrome(false, kElementExistsQueryTwice);
412 Session session("id", scoped_ptr<Chrome>(chrome));
413 session.implicit_wait = 10;
kkania 2013/01/16 19:45:10 set all the implicit waits to 1000. if for some re
chrisgao (Use stgao instead) 2013/01/16 20:08:20 Done. But 1000 is too long for the testcase timeou
414 session.frame = "frame_id4";
415 base::DictionaryValue params;
416 params.SetString("using", "class name");
417 params.SetString("value", "c");
418 params.SetString("id", "1");
419 scoped_ptr<base::Value> result;
420 ASSERT_EQ(kOk, ExecuteFindChildElements(1, &session, params, &result).code());
421 base::DictionaryValue locator_param;
422 locator_param.SetString("class name", "c");
423 base::DictionaryValue root_element_param;
424 root_element_param.SetString("ELEMENT", "1");
425 base::ListValue expected_args;
426 expected_args.Append(locator_param.DeepCopy());
427 expected_args.Append(root_element_param.DeepCopy());
428 chrome->Verify("frame_id4", &expected_args, result.get());
429 }
430
431 TEST(CommandsTest, FailedFindChildElements) {
432 Session session("id",
433 scoped_ptr<Chrome>(
434 new FindElementChrome(false, kElementNotExistsQueryOnce)));
435 base::DictionaryValue params;
436 params.SetString("using", "id");
437 params.SetString("value", "a");
438 params.SetString("id", "1");
439 scoped_ptr<base::Value> result;
440 ASSERT_EQ(kOk, ExecuteFindChildElements(1, &session, params, &result).code());
376 base::ListValue* list; 441 base::ListValue* list;
377 ASSERT_TRUE(value->GetAsList(&list)); 442 ASSERT_TRUE(result->GetAsList(&list));
378 ASSERT_EQ(0U, list->GetSize()); 443 ASSERT_EQ(0U, list->GetSize());
379 } 444 }
380 445
446 TEST(CommandsTest, TimeoutInFindElement) {
447 Session session("id",
448 scoped_ptr<Chrome>(
449 new FindElementChrome(true, kElementExistsTimeout)));
450 session.implicit_wait = 2;
451 base::DictionaryValue params;
452 params.SetString("using", "id");
453 params.SetString("value", "a");
454 params.SetString("id", "1");
455 scoped_ptr<base::Value> result;
456 ASSERT_EQ(kNoSuchElement,
457 ExecuteFindElement(1, &session, params, &result).code());
458 }
459
381 namespace { 460 namespace {
382 461
383 class ErrorCallFunctionChrome : public StubChrome { 462 class ErrorCallFunctionChrome : public StubChrome {
384 public: 463 public:
385 ErrorCallFunctionChrome() {} 464 explicit ErrorCallFunctionChrome(StatusCode code) : code_(code) {}
386 virtual ~ErrorCallFunctionChrome() {} 465 virtual ~ErrorCallFunctionChrome() {}
387 466
388 // Overridden from Chrome: 467 // Overridden from Chrome:
389 virtual Status CallFunction(const std::string& frame, 468 virtual Status CallFunction(const std::string& frame,
390 const std::string& function, 469 const std::string& function,
391 const base::ListValue& args, 470 const base::ListValue& args,
392 scoped_ptr<base::Value>* result) OVERRIDE { 471 scoped_ptr<base::Value>* result) OVERRIDE {
393 return Status(kUnknownError); 472 return Status(code_);
394 } 473 }
474
475 private:
476 StatusCode code_;
395 }; 477 };
396 478
397 } // namespace 479 } // namespace
398 480
399 TEST(CommandsTest, ErrorFindElement) { 481 TEST(CommandsTest, ErrorFindElement) {
400 Session session("id", scoped_ptr<Chrome>(new ErrorCallFunctionChrome())); 482 Session session("id",
483 scoped_ptr<Chrome>(new ErrorCallFunctionChrome(kUnknownError)));
401 base::DictionaryValue params; 484 base::DictionaryValue params;
402 params.SetString("using", "id"); 485 params.SetString("using", "id");
403 params.SetString("value", "a"); 486 params.SetString("value", "a");
404 scoped_ptr<base::Value> value; 487 scoped_ptr<base::Value> value;
405 ASSERT_EQ(kUnknownError, ExecuteFindElement(&session, params, &value).code());
406 ASSERT_EQ(kUnknownError, 488 ASSERT_EQ(kUnknownError,
407 ExecuteFindElements(&session, params, &value).code()); 489 ExecuteFindElement(1, &session, params, &value).code());
490 ASSERT_EQ(kUnknownError,
491 ExecuteFindElements(1, &session, params, &value).code());
408 } 492 }
493
494 TEST(CommandsTest, ErrorFindChildElement) {
495 Session session("id",
496 scoped_ptr<Chrome>(new ErrorCallFunctionChrome(kStaleElementReference)));
497 base::DictionaryValue params;
498 params.SetString("using", "id");
499 params.SetString("value", "a");
500 params.SetString("id", "1");
501 scoped_ptr<base::Value> value;
502 ASSERT_EQ(kStaleElementReference,
503 ExecuteFindChildElement(1, &session, params, &value).code());
504 ASSERT_EQ(kStaleElementReference,
505 ExecuteFindChildElements(1, &session, params, &value).code());
506 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698