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

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

Powered by Google App Engine
This is Rietveld 408576698