selenium-test-server.pl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. use strict;
  2. use warnings;
  3. use HTTP::Daemon;
  4. use HTTP::Status;
  5. use Data::Compare;
  6. use JSON;
  7. # Port where Selenium server listens
  8. my $port = 4444;
  9. my $daemon = HTTP::Daemon->new( LocalPort => $port )
  10. || die "Couldn't start HTTP server at $port ";
  11. print "Server listening at: ", $daemon->url, "\n";
  12. my $resource = {
  13. '/wd/hub/session' => {
  14. 'input' =>,
  15. 'output' =>,
  16. },
  17. '/wd/hub/session/123456789' => {
  18. 'input' =>,
  19. 'output' =>,
  20. },
  21. '/wd/hub/session/123456789/window_handle' => {
  22. 'input' =>,
  23. 'output' =>,
  24. },
  25. '/wd/hub/session/123456789/window_handles' => {
  26. 'input' =>,
  27. 'output' =>,
  28. },
  29. '/wd/hub/session/123456789/url' => {
  30. 'input' =>,
  31. 'output' =>,
  32. },
  33. '/wd/hub/session/123456789/forward' => {
  34. 'input' =>,
  35. 'output' =>,
  36. },
  37. '/wd/hub/session/123456789/back' => {
  38. 'input' =>,
  39. 'output' =>,
  40. },
  41. '/wd/hub/session/123456789/refresh' => {
  42. 'input' =>,
  43. 'output' =>,
  44. },
  45. '/wd/hub/session/123456789/execute' => {
  46. 'input' =>,
  47. 'output' =>,
  48. },
  49. '/wd/hub/session/123456789/screenshot' => {
  50. 'input' =>,
  51. 'output' =>,
  52. },
  53. '/wd/hub/session/123456789/frame' => {
  54. 'input' =>,
  55. 'output' =>,
  56. },
  57. '/wd/hub/session/123456789/window' => {
  58. 'input' =>,
  59. 'output' =>,
  60. },
  61. '/wd/hub/session/123456789/speed' => {
  62. 'input' =>,
  63. 'output' =>,
  64. },
  65. '/wd/hub/session/123456789/cookie' => {
  66. 'input' =>,
  67. 'output' =>,
  68. },
  69. '/wd/hub/session/123456789/cookie/foo-bar' => {
  70. 'input' =>,
  71. 'output' =>,
  72. },
  73. '/wd/hub/session/123456789/source' => {
  74. 'input' =>,
  75. 'output' =>,
  76. },
  77. '/wd/hub/session/123456789/title' => {
  78. 'input' =>,
  79. 'output' =>,
  80. },
  81. '/wd/hub/session/123456789/element' => {
  82. 'input' =>,
  83. 'output' =>,
  84. },
  85. '/wd/hub/session/123456789/elements' => {
  86. 'input' =>,
  87. 'output' =>,
  88. },
  89. '/wd/hub/session/123456789/element/active' => {
  90. 'input' =>,
  91. 'output' =>,
  92. },
  93. '/wd/hub/session/123456789/element/elemID' => {
  94. 'input' =>,
  95. 'output' =>,
  96. },
  97. '/wd/hub/session/123456789/element/elemID/element' => {
  98. 'input' =>,
  99. 'output' =>,
  100. },
  101. '/wd/hub/session/123456789/element/elemID/elements' => {
  102. 'input' =>,
  103. 'output' =>,
  104. },
  105. '/wd/hub/session/123456789/element/elemID/click' => {
  106. 'input' =>,
  107. 'output' =>,
  108. },
  109. '/wd/hub/session/123456789/element/elemID/submit' => {
  110. 'input' =>,
  111. 'output' =>,
  112. },
  113. '/wd/hub/session/123456789/element/elemID/text' => {
  114. 'input' =>,
  115. 'output' =>,
  116. },
  117. '/wd/hub/session/123456789/element/elemID/value' => {
  118. 'input' =>,
  119. 'output' =>,
  120. },
  121. '/wd/hub/session/123456789/element/elemID/name' => {
  122. 'input' =>,
  123. 'output' =>,
  124. },
  125. '/wd/hub/session/123456789/element/elemID/clear' => {
  126. 'input' =>,
  127. 'output' =>,
  128. },
  129. '/wd/hub/session/123456789/element/elemID/selected' => {
  130. 'input' =>,
  131. 'output' =>,
  132. },
  133. '/wd/hub/session/123456789/element/elemID/toggle' => {
  134. 'input' =>,
  135. 'output' =>,
  136. },
  137. '/wd/hub/session/123456789/element/elemID/enabled' => {
  138. 'input' =>,
  139. 'output' =>,
  140. },
  141. '/wd/hub/session/123456789/element/elemID/attribute/attrName' => {
  142. 'input' =>,
  143. 'output' =>,
  144. },
  145. '/wd/hub/session/123456789/element/elemID/equals/otherElemID' => {
  146. 'input' =>,
  147. 'output' =>,
  148. },
  149. '/wd/hub/session/123456789/element/elemID/displayed' => {
  150. 'input' =>,
  151. 'output' =>,
  152. },
  153. '/wd/hub/session/123456789/element/elemID/location' => {
  154. 'input' =>,
  155. 'output' =>,
  156. },
  157. '/wd/hub/session/123456789/element/elemID/location_in_view' => {
  158. 'input' =>,
  159. 'output' =>,
  160. },
  161. '/wd/hub/session/123456789/element/elemID/size' => {
  162. 'input' =>,
  163. 'output' =>,
  164. },
  165. '/wd/hub/session/123456789/element/elemID/css/propName' => {
  166. 'input' =>,
  167. 'output' =>,
  168. },
  169. '/wd/hub/session/123456789/element/elemID/hover' => {
  170. 'input' =>,
  171. 'output' =>,
  172. },
  173. '/wd/hub/session/123456789/element/elemID/drag' => {
  174. 'input' =>,
  175. 'output' =>,
  176. },
  177. };
  178. my $json = new JSON;
  179. while ( my $client = $daemon->accept ) {
  180. while ( my $request = $client->get_request ) {
  181. my $url = $request->uri->path;
  182. # Check if the resource is defined
  183. if ( !( defined $resource->{$url} ) ) {
  184. $client->send_error(404);
  185. }
  186. elsif ( $url eq '/wd/hub/session' ) {
  187. # our dummy session 123456789...
  188. my $rs = new HTTP::Response(303);
  189. $rs->header( 'Location' => 'http://localhost:4444/wd/hub/session/123456789' );
  190. my $resp = {
  191. 'sessionId' => '123456789',
  192. 'value' => {
  193. 'browserName' => 'firefox',
  194. 'version' => '',
  195. 'javascriptEnabled' => JSON::true,
  196. 'class' => 'org.openqa.selenium.remote.DesiredCapabilities',
  197. 'platform' => 'ANY'
  198. },
  199. 'status' => 0,
  200. 'class' => 'org.openqa.selenium.remote.Response'
  201. };
  202. my $json_data = $json->allow_nonref->utf8(1)->encode($resp);
  203. $rs->content($json_data);
  204. $client->send_response($rs);
  205. }
  206. else {
  207. my $rs = new HTTP::Response(200);
  208. my $json_data = '';
  209. if ( defined $resource->{$url}->{'input'} ) {
  210. my $post_data =
  211. $json->allow_nonref->utf8(1)->decode( $request->content );
  212. if ( !( Compare( $post_data, $resource->{$url}->{'input'} ) ) )
  213. {
  214. $rs->code(400);
  215. $json_data = "";
  216. }
  217. else {
  218. $json_data =
  219. $json->allow_nonref->utf8(1)
  220. ->encode( $resource->{$url}->{'output'} );
  221. }
  222. }
  223. $rs->header( 'Content-Type' => 'application/json; charset=utf-8' );
  224. $rs->content($json_data);
  225. $client->send_response($rs);
  226. }
  227. }
  228. # clean up
  229. $client->close();
  230. undef($client);
  231. }