01-driver.t 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. use strict;
  2. use warnings;
  3. use JSON;
  4. use Net::Ping;
  5. use HTTP::Headers;
  6. use Test::More;
  7. use Test::LWP::UserAgent;
  8. use Selenium::Remote::Driver;
  9. BEGIN {
  10. if (defined $ENV{'WD_MOCKING_RECORD'} && ($ENV{'WD_MOCKING_RECORD'}==1)) {
  11. use t::lib::MockSeleniumWebDriver;
  12. my $p = Net::Ping->new("tcp", 2);
  13. $p->port_number(4444);
  14. unless ($p->ping('localhost')) {
  15. plan skip_all => "Selenium server is not running on localhost:4444";
  16. exit;
  17. }
  18. warn "\n\nRecording...\n\n";
  19. }
  20. }
  21. my $record = (defined $ENV{'WD_MOCKING_RECORD'} && ($ENV{'WD_MOCKING_RECORD'}==1))?1:0;
  22. my $os = $^O;
  23. if ($os =~ m/(aix|freebsd|openbsd|sunos|solaris)/) {
  24. $os = 'linux';
  25. }
  26. my $mock_file = "01-driver-mock-$os.json";
  27. if (!$record && !(-e "t/mock-recordings/$mock_file")) {
  28. plan skip_all => "Mocking of tests is not been enabled for this platform";
  29. }
  30. t::lib::MockSeleniumWebDriver::register($record,"t/mock-recordings/$mock_file");
  31. my $driver = Selenium::Remote::Driver->new(browser_name => 'firefox');
  32. my $website = 'http://localhost:63636';
  33. my $ret;
  34. DESIRED_CAPABILITIES: {
  35. # We're using a different test method for these because we needed
  36. # to inspect payload of the POST to /session, and the method of
  37. # recording the RES/REQ pairs doesn't provide any easy way to do
  38. # that.
  39. my $tua = Test::LWP::UserAgent->new;
  40. my $res = {
  41. cmd_return => {},
  42. cmd_status => 'OK',
  43. sessionId => '123124123'
  44. };
  45. $tua->map_response(qr{status}, HTTP::Response->new(200, 'OK'));
  46. my $requests_count = 0;
  47. my $mock_session_handler = sub {
  48. my $request = shift;
  49. $requests_count++;
  50. if ($request->method eq 'POST') {
  51. my $caps = from_json($request->decoded_content)->{desiredCapabilities};
  52. my @keys = keys %$caps;
  53. if (scalar @keys) {
  54. ok(scalar @keys == 2, 'exactly 2 keys passed in if we use desired_capabilities');
  55. my $grep = grep { 'browserName' eq $_ } @keys;
  56. ok($grep, 'and it is the right one');
  57. ok($caps->{superfluous} eq 'thing', 'and we pass through anything else');
  58. ok($caps->{browserName} eq 'firefox', 'and we override the "normal" caps');
  59. ok(!exists $caps->{platform}, 'or ignore them entirely');
  60. }
  61. else {
  62. ok(to_json($caps) eq '{}', 'an empty constructor defaults to an empty hash');
  63. }
  64. return HTTP::Response->new(204, 'OK', ['Content-Type' => 'application/json'], to_json($res));
  65. }
  66. else {
  67. # it's the DELETE when the driver calls
  68. # DESTROY. This should be the last call to /session/.
  69. return HTTP::Response->new(200, 'OK')
  70. }
  71. };
  72. $tua->map_response(qr{session}, $mock_session_handler);
  73. my $caps_driver = Selenium::Remote::Driver->new_from_caps(
  74. auto_close => 0,
  75. browser_name => 'not firefox',
  76. platform => 'WINDOWS',
  77. desired_capabilities => {
  78. 'browserName' => 'firefox',
  79. 'superfluous' => 'thing'
  80. },
  81. ua => $tua
  82. );
  83. ok($caps_driver->auto_close eq 0, 'and other properties are still set');
  84. $caps_driver = Selenium::Remote::Driver->new(
  85. auto_close => 0,
  86. browser_name => 'not firefox',
  87. platform => 'WINDOWS',
  88. desired_capabilities => {
  89. 'browserName' => 'firefox',
  90. 'superfluous' => 'thing'
  91. },
  92. ua => $tua
  93. );
  94. ok($caps_driver->auto_close eq 0, 'and other properties are set if we use the normal constructor');
  95. $caps_driver = Selenium::Remote::Driver->new_from_caps(ua => $tua);
  96. ok($requests_count == 3, 'The new_from_caps section has the correct number of requests to /session/');
  97. }
  98. GRID_STARTUP: {
  99. # Mimicking a grid server; /wd/hub/status fails, and we expect
  100. # grid/api/hub/status to be checked instead.
  101. my $tua = Test::LWP::UserAgent->new;
  102. my $not_ok = sub {
  103. return HTTP::Response->new(500, 'NOTOK');
  104. };
  105. $tua->map_response(qr{wd/hub/status}, $not_ok);
  106. my $grid_status_count = 0;
  107. my $ok = sub {
  108. my $res = {
  109. cmd_return => {},
  110. cmd_status => 'OK',
  111. sessionId => '123124123'
  112. };
  113. $grid_status_count++;
  114. return HTTP::Response->new(200, 'OK', ['Content-Type' => 'application/json'], to_json($res));
  115. };
  116. $tua->map_response(qr{(?:grid/api/hub/status|session)}, $ok);
  117. my $grid_driver = Selenium::Remote::Driver->new(ua => $tua);
  118. ok(defined $grid_driver, 'Grid: Object loaded fine using grid/api/hub/status');
  119. ok($grid_driver->isa('Selenium::Remote::Driver'), 'Grid: ...and of right type');
  120. ok($grid_status_count == 2, 'checked Grid specific status');
  121. }
  122. CHECK_DRIVER: {
  123. ok(defined $driver, 'Object loaded fine...');
  124. ok($driver->isa('Selenium::Remote::Driver'), '...and of right type');
  125. ok(defined $driver->{'session_id'}, 'Established session on remote server');
  126. $ret = $driver->get_capabilities;
  127. is($ret->{'browserName'}, 'firefox', 'Right capabilities');
  128. my $status = $driver->status;
  129. ok($status->{build}->{version},"Got status build.version");
  130. ok($status->{build}->{revision},"Got status build.revision");
  131. ok($status->{build}->{time},"Got status build.time");
  132. }
  133. IME: {
  134. SKIP: {
  135. eval {$driver->available_engines;};
  136. if ($@) {
  137. skip "ime not available on this system",3;
  138. }
  139. };
  140. }
  141. LOAD_PAGE: {
  142. $driver->get("$website/index.html");
  143. pass('Loaded home page');
  144. $ret = $driver->get_title();
  145. is($ret, 'Hello WebDriver', 'Got the title');
  146. $ret = $driver->get_current_url();
  147. ok($ret =~ m/$website/i, 'Got proper URL');
  148. }
  149. WINDOW: {
  150. $ret = $driver->get_current_window_handle();
  151. ok($ret =~ m/^{.*}$/, 'Proper window handle received');
  152. $ret = $driver->get_window_handles();
  153. is(ref $ret, 'ARRAY', 'Received all window handles');
  154. $ret = $driver->set_window_position(100,100);
  155. is($ret, 1, 'Set the window position to 100, 100');
  156. $ret = $driver->get_window_position();
  157. is ($ret->{'x'}, 100, 'Got the right X Co-ordinate');
  158. is ($ret->{'y'}, 100, 'Got the right Y Co-ordinate');
  159. $ret = $driver->set_window_size(640, 480);
  160. is($ret, 1, 'Set the window size to 640x480');
  161. $ret = $driver->get_window_size();
  162. is ($ret->{'height'}, 640, 'Got the right height');
  163. is ($ret->{'width'}, 480, 'Got the right width');
  164. $ret = $driver->get_page_source();
  165. ok($ret =~ m/^<html/i, 'Received page source');
  166. eval {$driver->set_implicit_wait_timeout(20001);};
  167. ok(!$@,"Set implicit wait timeout");
  168. eval {$driver->set_implicit_wait_timeout(0);};
  169. ok(!$@,"Reset implicit wait timeout");
  170. $ret = $driver->get("$website/frameset.html");
  171. $ret = $driver->switch_to_frame('second');
  172. }
  173. COOKIES: {
  174. $driver->get("$website/cookies.html");
  175. $ret = $driver->get_all_cookies();
  176. is(@{$ret}, 2, 'Got 2 cookies');
  177. $ret = $driver->delete_all_cookies();
  178. pass('Deleting cookies...');
  179. $ret = $driver->get_all_cookies();
  180. is(@{$ret}, 0, 'Deleted all cookies.');
  181. $ret = $driver->add_cookie('foo', 'bar', '/', 'localhost', 0);
  182. pass('Adding cookie foo...');
  183. $ret = $driver->get_all_cookies();
  184. is(@{$ret}, 1, 'foo cookie added.');
  185. is($ret->[0]{'secure'}, 0, 'foo cookie insecure.');
  186. $ret = $driver->delete_cookie_named('foo');
  187. pass('Deleting cookie foo...');
  188. $ret = $driver->get_all_cookies();
  189. is(@{$ret}, 0, 'foo cookie deleted.');
  190. $ret = $driver->delete_all_cookies();
  191. }
  192. MOVE: {
  193. $driver->get("$website/index.html");
  194. $driver->get("$website/formPage.html");
  195. $ret = $driver->go_back();
  196. pass('Clicked Back...');
  197. $ret = $driver->get_title();
  198. is($ret, 'Hello WebDriver', 'Got the right title');
  199. $ret = $driver->go_forward();
  200. pass('Clicked Forward...');
  201. $ret = $driver->get_title();
  202. is($ret, 'We Leave From Here', 'Got the right title');
  203. $ret = $driver->refresh();
  204. pass('Clicked Refresh...');
  205. $ret = $driver->get_title();
  206. is($ret, 'We Leave From Here', 'Got the right title');
  207. }
  208. FIND: {
  209. my $elem = $driver->find_element("//input[\@id='checky']");
  210. ok($elem->isa('Selenium::Remote::WebElement'), 'Got WebElement via Xpath');
  211. $elem = $driver->find_element('checky', 'id');
  212. ok($elem->isa('Selenium::Remote::WebElement'), 'Got WebElement via Id');
  213. $elem = $driver->find_element('checky', 'name');
  214. ok($elem->isa('Selenium::Remote::WebElement'), 'Got WebElement via Name');
  215. $elem = $driver->find_element('multi', 'id');
  216. $elem = $driver->find_child_element($elem, "option");
  217. ok($elem->isa('Selenium::Remote::WebElement'), 'Got child WebElement...');
  218. $ret = $elem->get_value();
  219. is($ret, 'Eggs', '...right child WebElement');
  220. $ret = $driver->find_child_elements($elem, "//option[\@selected='selected']");
  221. is(@{$ret}, 4, 'Got 4 WebElements');
  222. my $expected_err = "An element could not be located on the page using the "
  223. . "given search parameters: "
  224. . "element_that_doesnt_exist,id"
  225. # the following needs to always be right before the eval
  226. . " at " . __FILE__ . " line " . (__LINE__+1);
  227. eval { $driver->find_element("element_that_doesnt_exist","id"); };
  228. chomp $@;
  229. is($@,$expected_err.".","find_element croaks properly");
  230. my $elems = $driver->find_elements("//input[\@id='checky']");
  231. is(scalar(@$elems),1, 'Got an arrayref of WebElements');
  232. my @array_elems = $driver->find_elements("//input[\@id='checky']");
  233. is(scalar(@array_elems),1, 'Got an array of WebElements');
  234. is($elems->[0]->get_value(),$array_elems[0]->get_value(), 'and the elements returned are the same');
  235. }
  236. EXECUTE: {
  237. my $script = q{
  238. var arg1 = arguments[0];
  239. var elem = window.document.getElementById(arg1);
  240. return elem;
  241. };
  242. my $elem = $driver->execute_script($script,'checky');
  243. ok($elem->isa('Selenium::Remote::WebElement'), 'Executed script');
  244. is($elem->get_attribute('id'),'checky','Execute found proper element');
  245. $script = q{
  246. var links = window.document.links
  247. var length = links.length
  248. var results = new Array(length)
  249. while(length--) results[length] = links[length];
  250. return results;
  251. };
  252. $elem = $driver->execute_script($script);
  253. ok($elem, 'Got something back from execute_script');
  254. isa_ok($elem, 'ARRAY', 'What we got back is an ARRAY ref');
  255. ok(scalar(@$elem), 'There are elements in our array ref');
  256. foreach my $element (@$elem) {
  257. isa_ok($element, 'Selenium::Remote::WebElement', 'Element was converted to a WebElement object');
  258. }
  259. $script = q{
  260. var arg1 = arguments[0];
  261. var callback = arguments[arguments.length-1];
  262. var elem = window.document.getElementById(arg1);
  263. callback(elem);
  264. };
  265. $elem = $driver->execute_async_script($script,'multi');
  266. ok($elem->isa('Selenium::Remote::WebElement'),'Executed async script');
  267. is($elem->get_attribute('id'),'multi','Async found proper element');
  268. }
  269. ALERT: {
  270. $driver->get("$website/alerts.html");
  271. $driver->find_element("alert",'id')->click;
  272. is($driver->get_alert_text,'cheese','alert text match');
  273. eval {$driver->dismiss_alert;};
  274. ok(!$@,"dismissed alert");
  275. $driver->find_element("prompt",'id')->click;
  276. is($driver->get_alert_text,'Enter your name','prompt text match');
  277. $driver->send_keys_to_prompt("Larry Wall");
  278. eval {$driver->accept_alert;};
  279. ok(!$@,"accepted prompt");
  280. is($driver->get_alert_text,'Larry Wall','keys sent to prompt');
  281. $driver->dismiss_alert;
  282. $driver->find_element("confirm",'id')->click;
  283. is($driver->get_alert_text,"Are you sure?",'confirm text match');
  284. eval {$driver->dismiss_alert;};
  285. ok(!$@,"dismissed confirm");
  286. is($driver->get_alert_text,'false',"dismissed confirmed correct");
  287. $driver->accept_alert;
  288. $driver->find_element("confirm",'id')->click;
  289. eval {$driver->accept_alert;};
  290. ok(!$@,"accepted confirm");
  291. is($driver->get_alert_text,'true',"accept confirm correct");
  292. $driver->accept_alert;
  293. }
  294. PAUSE: {
  295. my $starttime=time();
  296. $driver->pause();
  297. my $endtime=time();
  298. ok($starttime <= $endtime-1,"starttime <= endtime+1"); # Slept at least 1 second
  299. ok($starttime >= $endtime-2,"starttime >= endtime-2"); # Slept at most 2 seconds
  300. }
  301. AUTO_CLOSE: {
  302. my $stayOpen = Selenium::Remote::Driver->new(
  303. browser_name => 'firefox',
  304. auto_close => 0
  305. );
  306. $stayOpen->DESTROY();
  307. ok(defined $stayOpen->{'session_id'}, 'auto close in init hash is respected');
  308. $stayOpen->auto_close(1);
  309. $stayOpen->DESTROY();
  310. ok(!defined $stayOpen->{'session_id'}, 'true for auto close is still respected');
  311. $driver->auto_close(0);
  312. $driver->DESTROY();
  313. ok(defined $driver->{'session_id'}, 'changing autoclose on the fly keeps the session open');
  314. $driver->auto_close(1);
  315. }
  316. BASE_URL: {
  317. {
  318. package MySeleniumRemoteDriver;
  319. use Moo;
  320. extends 'Selenium::Remote::Driver';
  321. sub _execute_command { $_[2]->{url} }
  322. 1;
  323. }
  324. my @tests = ({
  325. base_url => 'http://example.com',
  326. url => '/foo',
  327. expected => 'http://example.com/foo',
  328. },{
  329. base_url => 'http://example.com/',
  330. url => '/foo',
  331. expected => 'http://example.com/foo',
  332. },{
  333. base_url => 'http://example.com',
  334. url => 'foo',
  335. expected => 'http://example.com/foo',
  336. },{
  337. base_url => 'http://example.com/a',
  338. url => '/foo',
  339. expected => 'http://example.com/a/foo',
  340. },{
  341. base_url => 'http://example.com/a',
  342. url => 'foo',
  343. expected => 'http://example.com/a/foo',
  344. },{
  345. base_url => 'http://example.com/a',
  346. url => 'http://blog.example.com/foo',
  347. expected => 'http://blog.example.com/foo',
  348. });
  349. for my $test (@tests) {
  350. my $base_url_driver = MySeleniumRemoteDriver->new(
  351. browser_name => 'firefox',
  352. base_url => $test->{base_url},
  353. testing => 1,
  354. );
  355. my $got = $base_url_driver->get($test->{url});
  356. is $got, $test->{expected}, "base_url + $test->{url}";
  357. }
  358. }
  359. QUIT: {
  360. $ret = $driver->quit();
  361. ok((not defined $driver->{'session_id'}), 'Killed the remote session');
  362. }
  363. done_testing;