browser_test.php 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. <?php
  2. // $Id: browser_test.php 1782 2008-04-25 17:09:06Z pp11 $
  3. require_once(dirname(__FILE__) . '/../autorun.php');
  4. require_once(dirname(__FILE__) . '/../browser.php');
  5. require_once(dirname(__FILE__) . '/../user_agent.php');
  6. require_once(dirname(__FILE__) . '/../http.php');
  7. require_once(dirname(__FILE__) . '/../page.php');
  8. require_once(dirname(__FILE__) . '/../encoding.php');
  9. Mock::generate('SimpleHttpResponse');
  10. Mock::generate('SimplePage');
  11. Mock::generate('SimpleForm');
  12. Mock::generate('SimpleUserAgent');
  13. Mock::generatePartial(
  14. 'SimpleBrowser',
  15. 'MockParseSimpleBrowser',
  16. array('createUserAgent', 'parse'));
  17. Mock::generatePartial(
  18. 'SimpleBrowser',
  19. 'MockUserAgentSimpleBrowser',
  20. array('createUserAgent'));
  21. class TestOfHistory extends UnitTestCase {
  22. function testEmptyHistoryHasFalseContents() {
  23. $history = new SimpleBrowserHistory();
  24. $this->assertIdentical($history->getUrl(), false);
  25. $this->assertIdentical($history->getParameters(), false);
  26. }
  27. function testCannotMoveInEmptyHistory() {
  28. $history = new SimpleBrowserHistory();
  29. $this->assertFalse($history->back());
  30. $this->assertFalse($history->forward());
  31. }
  32. function testCurrentTargetAccessors() {
  33. $history = new SimpleBrowserHistory();
  34. $history->recordEntry(
  35. new SimpleUrl('http://www.here.com/'),
  36. new SimpleGetEncoding());
  37. $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.here.com/'));
  38. $this->assertIdentical($history->getParameters(), new SimpleGetEncoding());
  39. }
  40. function testSecondEntryAccessors() {
  41. $history = new SimpleBrowserHistory();
  42. $history->recordEntry(
  43. new SimpleUrl('http://www.first.com/'),
  44. new SimpleGetEncoding());
  45. $history->recordEntry(
  46. new SimpleUrl('http://www.second.com/'),
  47. new SimplePostEncoding(array('a' => 1)));
  48. $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.second.com/'));
  49. $this->assertIdentical(
  50. $history->getParameters(),
  51. new SimplePostEncoding(array('a' => 1)));
  52. }
  53. function testGoingBackwards() {
  54. $history = new SimpleBrowserHistory();
  55. $history->recordEntry(
  56. new SimpleUrl('http://www.first.com/'),
  57. new SimpleGetEncoding());
  58. $history->recordEntry(
  59. new SimpleUrl('http://www.second.com/'),
  60. new SimplePostEncoding(array('a' => 1)));
  61. $this->assertTrue($history->back());
  62. $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
  63. $this->assertIdentical($history->getParameters(), new SimpleGetEncoding());
  64. }
  65. function testGoingBackwardsOffBeginning() {
  66. $history = new SimpleBrowserHistory();
  67. $history->recordEntry(
  68. new SimpleUrl('http://www.first.com/'),
  69. new SimpleGetEncoding());
  70. $this->assertFalse($history->back());
  71. $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
  72. $this->assertIdentical($history->getParameters(), new SimpleGetEncoding());
  73. }
  74. function testGoingForwardsOffEnd() {
  75. $history = new SimpleBrowserHistory();
  76. $history->recordEntry(
  77. new SimpleUrl('http://www.first.com/'),
  78. new SimpleGetEncoding());
  79. $this->assertFalse($history->forward());
  80. $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
  81. $this->assertIdentical($history->getParameters(), new SimpleGetEncoding());
  82. }
  83. function testGoingBackwardsAndForwards() {
  84. $history = new SimpleBrowserHistory();
  85. $history->recordEntry(
  86. new SimpleUrl('http://www.first.com/'),
  87. new SimpleGetEncoding());
  88. $history->recordEntry(
  89. new SimpleUrl('http://www.second.com/'),
  90. new SimplePostEncoding(array('a' => 1)));
  91. $this->assertTrue($history->back());
  92. $this->assertTrue($history->forward());
  93. $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.second.com/'));
  94. $this->assertIdentical(
  95. $history->getParameters(),
  96. new SimplePostEncoding(array('a' => 1)));
  97. }
  98. function testNewEntryReplacesNextOne() {
  99. $history = new SimpleBrowserHistory();
  100. $history->recordEntry(
  101. new SimpleUrl('http://www.first.com/'),
  102. new SimpleGetEncoding());
  103. $history->recordEntry(
  104. new SimpleUrl('http://www.second.com/'),
  105. new SimplePostEncoding(array('a' => 1)));
  106. $history->back();
  107. $history->recordEntry(
  108. new SimpleUrl('http://www.third.com/'),
  109. new SimpleGetEncoding());
  110. $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.third.com/'));
  111. $this->assertIdentical($history->getParameters(), new SimpleGetEncoding());
  112. }
  113. function testNewEntryDropsFutureEntries() {
  114. $history = new SimpleBrowserHistory();
  115. $history->recordEntry(
  116. new SimpleUrl('http://www.first.com/'),
  117. new SimpleGetEncoding());
  118. $history->recordEntry(
  119. new SimpleUrl('http://www.second.com/'),
  120. new SimpleGetEncoding());
  121. $history->recordEntry(
  122. new SimpleUrl('http://www.third.com/'),
  123. new SimpleGetEncoding());
  124. $history->back();
  125. $history->back();
  126. $history->recordEntry(
  127. new SimpleUrl('http://www.fourth.com/'),
  128. new SimpleGetEncoding());
  129. $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.fourth.com/'));
  130. $this->assertFalse($history->forward());
  131. $history->back();
  132. $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
  133. $this->assertFalse($history->back());
  134. }
  135. }
  136. class TestOfParsedPageAccess extends UnitTestCase {
  137. function loadPage(&$page) {
  138. $response = new MockSimpleHttpResponse($this);
  139. $agent = new MockSimpleUserAgent($this);
  140. $agent->returns('fetchResponse', $response);
  141. $browser = new MockParseSimpleBrowser($this);
  142. $browser->returns('createUserAgent', $agent);
  143. $browser->returns('parse', $page);
  144. $browser->__construct();
  145. $browser->get('http://this.com/page.html');
  146. return $browser;
  147. }
  148. function testAccessorsWhenNoPage() {
  149. $agent = new MockSimpleUserAgent($this);
  150. $browser = new MockParseSimpleBrowser($this);
  151. $browser->returns('createUserAgent', $agent);
  152. $browser->__construct();
  153. $this->assertEqual($browser->getContent(), '');
  154. }
  155. function testParse() {
  156. $page = new MockSimplePage();
  157. $page->setReturnValue('getRequest', "GET here.html\r\n\r\n");
  158. $page->setReturnValue('getRaw', 'Raw HTML');
  159. $page->setReturnValue('getTitle', 'Here');
  160. $page->setReturnValue('getFrameFocus', 'Frame');
  161. $page->setReturnValue('getMimeType', 'text/html');
  162. $page->setReturnValue('getResponseCode', 200);
  163. $page->setReturnValue('getAuthentication', 'Basic');
  164. $page->setReturnValue('getRealm', 'Somewhere');
  165. $page->setReturnValue('getTransportError', 'Ouch!');
  166. $browser = $this->loadPage($page);
  167. $this->assertEqual($browser->getRequest(), "GET here.html\r\n\r\n");
  168. $this->assertEqual($browser->getContent(), 'Raw HTML');
  169. $this->assertEqual($browser->getTitle(), 'Here');
  170. $this->assertEqual($browser->getFrameFocus(), 'Frame');
  171. $this->assertIdentical($browser->getResponseCode(), 200);
  172. $this->assertEqual($browser->getMimeType(), 'text/html');
  173. $this->assertEqual($browser->getAuthentication(), 'Basic');
  174. $this->assertEqual($browser->getRealm(), 'Somewhere');
  175. $this->assertEqual($browser->getTransportError(), 'Ouch!');
  176. }
  177. function testLinkAffirmationWhenPresent() {
  178. $page = new MockSimplePage();
  179. $page->setReturnValue('getUrlsByLabel', array('http://www.nowhere.com'));
  180. $page->expectOnce('getUrlsByLabel', array('a link label'));
  181. $browser = $this->loadPage($page);
  182. $this->assertIdentical($browser->getLink('a link label'), 'http://www.nowhere.com');
  183. }
  184. function testLinkAffirmationByIdWhenPresent() {
  185. $page = new MockSimplePage();
  186. $page->setReturnValue('getUrlById', 'a_page.com', array(99));
  187. $page->setReturnValue('getUrlById', false, array('*'));
  188. $browser = $this->loadPage($page);
  189. $this->assertIdentical($browser->getLinkById(99), 'a_page.com');
  190. $this->assertFalse($browser->getLinkById(98));
  191. }
  192. function testSettingFieldIsPassedToPage() {
  193. $page = new MockSimplePage();
  194. $page->expectOnce('setField', array(new SimpleByLabelOrName('key'), 'Value', false));
  195. $page->setReturnValue('getField', 'Value');
  196. $browser = $this->loadPage($page);
  197. $this->assertEqual($browser->getField('key'), 'Value');
  198. $browser->setField('key', 'Value');
  199. }
  200. }
  201. class TestOfBrowserNavigation extends UnitTestCase {
  202. function createBrowser($agent, $page) {
  203. $browser = new MockParseSimpleBrowser();
  204. $browser->returns('createUserAgent', $agent);
  205. $browser->returns('parse', $page);
  206. $browser->__construct();
  207. return $browser;
  208. }
  209. function testClickLinkRequestsPage() {
  210. $agent = new MockSimpleUserAgent();
  211. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  212. $agent->expectAt(
  213. 0,
  214. 'fetchResponse',
  215. array(new SimpleUrl('http://this.com/page.html'), new SimpleGetEncoding()));
  216. $agent->expectAt(
  217. 1,
  218. 'fetchResponse',
  219. array(new SimpleUrl('http://this.com/new.html'), new SimpleGetEncoding()));
  220. $agent->expectCallCount('fetchResponse', 2);
  221. $page = new MockSimplePage();
  222. $page->setReturnValue('getUrlsByLabel', array(new SimpleUrl('http://this.com/new.html')));
  223. $page->expectOnce('getUrlsByLabel', array('New'));
  224. $page->setReturnValue('getRaw', 'A page');
  225. $browser = $this->createBrowser($agent, $page);
  226. $browser->get('http://this.com/page.html');
  227. $this->assertTrue($browser->clickLink('New'));
  228. }
  229. function testClickLinkWithUnknownFrameStillRequestsWholePage() {
  230. $agent = new MockSimpleUserAgent();
  231. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  232. $agent->expectAt(
  233. 0,
  234. 'fetchResponse',
  235. array(new SimpleUrl('http://this.com/page.html'), new SimpleGetEncoding()));
  236. $target = new SimpleUrl('http://this.com/new.html');
  237. $target->setTarget('missing');
  238. $agent->expectAt(
  239. 1,
  240. 'fetchResponse',
  241. array($target, new SimpleGetEncoding()));
  242. $agent->expectCallCount('fetchResponse', 2);
  243. $parsed_url = new SimpleUrl('http://this.com/new.html');
  244. $parsed_url->setTarget('missing');
  245. $page = new MockSimplePage();
  246. $page->setReturnValue('getUrlsByLabel', array($parsed_url));
  247. $page->setReturnValue('hasFrames', false);
  248. $page->expectOnce('getUrlsByLabel', array('New'));
  249. $page->setReturnValue('getRaw', 'A page');
  250. $browser = $this->createBrowser($agent, $page);
  251. $browser->get('http://this.com/page.html');
  252. $this->assertTrue($browser->clickLink('New'));
  253. }
  254. function testClickingMissingLinkFails() {
  255. $agent = new MockSimpleUserAgent($this);
  256. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  257. $page = new MockSimplePage();
  258. $page->setReturnValue('getUrlsByLabel', array());
  259. $page->setReturnValue('getRaw', 'stuff');
  260. $browser = $this->createBrowser($agent, $page);
  261. $this->assertTrue($browser->get('http://this.com/page.html'));
  262. $this->assertFalse($browser->clickLink('New'));
  263. }
  264. function testClickIndexedLink() {
  265. $agent = new MockSimpleUserAgent();
  266. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  267. $agent->expectAt(
  268. 1,
  269. 'fetchResponse',
  270. array(new SimpleUrl('1.html'), new SimpleGetEncoding()));
  271. $agent->expectCallCount('fetchResponse', 2);
  272. $page = new MockSimplePage();
  273. $page->setReturnValue(
  274. 'getUrlsByLabel',
  275. array(new SimpleUrl('0.html'), new SimpleUrl('1.html')));
  276. $page->setReturnValue('getRaw', 'A page');
  277. $browser = $this->createBrowser($agent, $page);
  278. $browser->get('http://this.com/page.html');
  279. $this->assertTrue($browser->clickLink('New', 1));
  280. }
  281. function testClinkLinkById() {
  282. $agent = new MockSimpleUserAgent();
  283. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  284. $agent->expectAt(1, 'fetchResponse', array(
  285. new SimpleUrl('http://this.com/link.html'),
  286. new SimpleGetEncoding()));
  287. $agent->expectCallCount('fetchResponse', 2);
  288. $page = new MockSimplePage();
  289. $page->setReturnValue('getUrlById', new SimpleUrl('http://this.com/link.html'));
  290. $page->expectOnce('getUrlById', array(2));
  291. $page->setReturnValue('getRaw', 'A page');
  292. $browser = $this->createBrowser($agent, $page);
  293. $browser->get('http://this.com/page.html');
  294. $this->assertTrue($browser->clickLinkById(2));
  295. }
  296. function testClickingMissingLinkIdFails() {
  297. $agent = new MockSimpleUserAgent();
  298. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  299. $page = new MockSimplePage();
  300. $page->setReturnValue('getUrlById', false);
  301. $browser = $this->createBrowser($agent, $page);
  302. $browser->get('http://this.com/page.html');
  303. $this->assertFalse($browser->clickLink(0));
  304. }
  305. function testSubmitFormByLabel() {
  306. $agent = new MockSimpleUserAgent();
  307. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  308. $agent->expectAt(1, 'fetchResponse', array(
  309. new SimpleUrl('http://this.com/handler.html'),
  310. new SimplePostEncoding(array('a' => 'A'))));
  311. $agent->expectCallCount('fetchResponse', 2);
  312. $form = new MockSimpleForm();
  313. $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
  314. $form->setReturnValue('getMethod', 'post');
  315. $form->setReturnValue('submitButton', new SimplePostEncoding(array('a' => 'A')));
  316. $form->expectOnce('submitButton', array(new SimpleByLabel('Go'), false));
  317. $page = new MockSimplePage();
  318. $page->returns('getFormBySubmit', $form);
  319. $page->expectOnce('getFormBySubmit', array(new SimpleByLabel('Go')));
  320. $page->setReturnValue('getRaw', 'stuff');
  321. $browser = $this->createBrowser($agent, $page);
  322. $browser->get('http://this.com/page.html');
  323. $this->assertTrue($browser->clickSubmit('Go'));
  324. }
  325. function testDefaultSubmitFormByLabel() {
  326. $agent = new MockSimpleUserAgent();
  327. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  328. $agent->expectAt(1, 'fetchResponse', array(
  329. new SimpleUrl('http://this.com/page.html'),
  330. new SimpleGetEncoding(array('a' => 'A'))));
  331. $agent->expectCallCount('fetchResponse', 2);
  332. $form = new MockSimpleForm();
  333. $form->setReturnValue('getAction', new SimpleUrl('http://this.com/page.html'));
  334. $form->setReturnValue('getMethod', 'get');
  335. $form->setReturnValue('submitButton', new SimpleGetEncoding(array('a' => 'A')));
  336. $page = new MockSimplePage();
  337. $page->returns('getFormBySubmit', $form);
  338. $page->expectOnce('getFormBySubmit', array(new SimpleByLabel('Submit')));
  339. $page->setReturnValue('getRaw', 'stuff');
  340. $page->setReturnValue('getUrl', new SimpleUrl('http://this.com/page.html'));
  341. $browser = $this->createBrowser($agent, $page);
  342. $browser->get('http://this.com/page.html');
  343. $this->assertTrue($browser->clickSubmit());
  344. }
  345. function testSubmitFormByName() {
  346. $agent = new MockSimpleUserAgent();
  347. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  348. $form = new MockSimpleForm();
  349. $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
  350. $form->setReturnValue('getMethod', 'post');
  351. $form->setReturnValue('submitButton', new SimplePostEncoding(array('a' => 'A')));
  352. $page = new MockSimplePage();
  353. $page->returns('getFormBySubmit', $form);
  354. $page->expectOnce('getFormBySubmit', array(new SimpleByName('me')));
  355. $page->setReturnValue('getRaw', 'stuff');
  356. $browser = $this->createBrowser($agent, $page);
  357. $browser->get('http://this.com/page.html');
  358. $this->assertTrue($browser->clickSubmitByName('me'));
  359. }
  360. function testSubmitFormById() {
  361. $agent = new MockSimpleUserAgent();
  362. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  363. $form = new MockSimpleForm();
  364. $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
  365. $form->setReturnValue('getMethod', 'post');
  366. $form->setReturnValue('submitButton', new SimplePostEncoding(array('a' => 'A')));
  367. $form->expectOnce('submitButton', array(new SimpleById(99), false));
  368. $page = new MockSimplePage();
  369. $page->returns('getFormBySubmit', $form);
  370. $page->expectOnce('getFormBySubmit', array(new SimpleById(99)));
  371. $page->setReturnValue('getRaw', 'stuff');
  372. $browser = $this->createBrowser($agent, $page);
  373. $browser->get('http://this.com/page.html');
  374. $this->assertTrue($browser->clickSubmitById(99));
  375. }
  376. function testSubmitFormByImageLabel() {
  377. $agent = new MockSimpleUserAgent();
  378. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  379. $form = new MockSimpleForm();
  380. $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
  381. $form->setReturnValue('getMethod', 'post');
  382. $form->setReturnValue('submitImage', new SimplePostEncoding(array('a' => 'A')));
  383. $form->expectOnce('submitImage', array(new SimpleByLabel('Go!'), 10, 11, false));
  384. $page = new MockSimplePage();
  385. $page->returns('getFormByImage', $form);
  386. $page->expectOnce('getFormByImage', array(new SimpleByLabel('Go!')));
  387. $page->setReturnValue('getRaw', 'stuff');
  388. $browser = $this->createBrowser($agent, $page);
  389. $browser->get('http://this.com/page.html');
  390. $this->assertTrue($browser->clickImage('Go!', 10, 11));
  391. }
  392. function testSubmitFormByImageName() {
  393. $agent = new MockSimpleUserAgent();
  394. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  395. $form = new MockSimpleForm();
  396. $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
  397. $form->setReturnValue('getMethod', 'post');
  398. $form->setReturnValue('submitImage', new SimplePostEncoding(array('a' => 'A')));
  399. $form->expectOnce('submitImage', array(new SimpleByName('a'), 10, 11, false));
  400. $page = new MockSimplePage();
  401. $page->returns('getFormByImage', $form);
  402. $page->expectOnce('getFormByImage', array(new SimpleByName('a')));
  403. $page->setReturnValue('getRaw', 'stuff');
  404. $browser = $this->createBrowser($agent, $page);
  405. $browser->get('http://this.com/page.html');
  406. $this->assertTrue($browser->clickImageByName('a', 10, 11));
  407. }
  408. function testSubmitFormByImageId() {
  409. $agent = new MockSimpleUserAgent();
  410. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  411. $form = new MockSimpleForm();
  412. $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
  413. $form->setReturnValue('getMethod', 'post');
  414. $form->setReturnValue('submitImage', new SimplePostEncoding(array('a' => 'A')));
  415. $form->expectOnce('submitImage', array(new SimpleById(99), 10, 11, false));
  416. $page = new MockSimplePage();
  417. $page->returns('getFormByImage', $form);
  418. $page->expectOnce('getFormByImage', array(new SimpleById(99)));
  419. $page->setReturnValue('getRaw', 'stuff');
  420. $browser = $this->createBrowser($agent, $page);
  421. $browser->get('http://this.com/page.html');
  422. $this->assertTrue($browser->clickImageById(99, 10, 11));
  423. }
  424. function testSubmitFormByFormId() {
  425. $agent = new MockSimpleUserAgent();
  426. $agent->returns('fetchResponse', new MockSimpleHttpResponse());
  427. $agent->expectAt(1, 'fetchResponse', array(
  428. new SimpleUrl('http://this.com/handler.html'),
  429. new SimplePostEncoding(array('a' => 'A'))));
  430. $agent->expectCallCount('fetchResponse', 2);
  431. $form = new MockSimpleForm();
  432. $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
  433. $form->setReturnValue('getMethod', 'post');
  434. $form->setReturnValue('submit', new SimplePostEncoding(array('a' => 'A')));
  435. $page = new MockSimplePage();
  436. $page->returns('getFormById', $form);
  437. $page->expectOnce('getFormById', array(33));
  438. $page->setReturnValue('getRaw', 'stuff');
  439. $browser = $this->createBrowser($agent, $page);
  440. $browser->get('http://this.com/page.html');
  441. $this->assertTrue($browser->submitFormById(33));
  442. }
  443. }
  444. class TestOfBrowserFrames extends UnitTestCase {
  445. function createBrowser($agent) {
  446. $browser = new MockUserAgentSimpleBrowser();
  447. $browser->returns('createUserAgent', $agent);
  448. $browser->__construct();
  449. return $browser;
  450. }
  451. function createUserAgent($pages) {
  452. $agent = new MockSimpleUserAgent();
  453. foreach ($pages as $url => $raw) {
  454. $url = new SimpleUrl($url);
  455. $response = new MockSimpleHttpResponse();
  456. $response->setReturnValue('getUrl', $url);
  457. $response->setReturnValue('getContent', $raw);
  458. $agent->returns('fetchResponse', $response, array($url, '*'));
  459. }
  460. return $agent;
  461. }
  462. function testSimplePageHasNoFrames() {
  463. $browser = $this->createBrowser($this->createUserAgent(
  464. array('http://site.with.no.frames/' => 'A non-framed page')));
  465. $this->assertEqual(
  466. $browser->get('http://site.with.no.frames/'),
  467. 'A non-framed page');
  468. $this->assertIdentical($browser->getFrames(), 'http://site.with.no.frames/');
  469. }
  470. function testFramesetWithNoFrames() {
  471. $browser = $this->createBrowser($this->createUserAgent(
  472. array('http://site.with.no.frames/' => '<frameset></frameset>')));
  473. $this->assertEqual($browser->get('http://site.with.no.frames/'), '');
  474. $this->assertIdentical($browser->getFrames(), array());
  475. }
  476. function testFramesetWithSingleFrame() {
  477. $frameset = '<frameset><frame name="a" src="frame.html"></frameset>';
  478. $browser = $this->createBrowser($this->createUserAgent(array(
  479. 'http://site.with.one.frame/' => $frameset,
  480. 'http://site.with.one.frame/frame.html' => 'A frame')));
  481. $this->assertEqual($browser->get('http://site.with.one.frame/'), 'A frame');
  482. $this->assertIdentical(
  483. $browser->getFrames(),
  484. array('a' => 'http://site.with.one.frame/frame.html'));
  485. }
  486. function testTitleTakenFromFramesetPage() {
  487. $frameset = '<title>Frameset title</title>' .
  488. '<frameset><frame name="a" src="frame.html"></frameset>';
  489. $browser = $this->createBrowser($this->createUserAgent(array(
  490. 'http://site.with.one.frame/' => $frameset,
  491. 'http://site.with.one.frame/frame.html' => '<title>Page title</title>')));
  492. $browser->get('http://site.with.one.frame/');
  493. $this->assertEqual($browser->getTitle(), 'Frameset title');
  494. }
  495. function testFramesetWithSingleUnnamedFrame() {
  496. $frameset = '<frameset><frame src="frame.html"></frameset>';
  497. $browser = $this->createBrowser($this->createUserAgent(array(
  498. 'http://site.with.one.frame/' => $frameset,
  499. 'http://site.with.one.frame/frame.html' => 'One frame')));
  500. $this->assertEqual(
  501. $browser->get('http://site.with.one.frame/'),
  502. 'One frame');
  503. $this->assertIdentical(
  504. $browser->getFrames(),
  505. array(1 => 'http://site.with.one.frame/frame.html'));
  506. }
  507. function testFramesetWithMultipleFrames() {
  508. $frameset = '<frameset>' .
  509. '<frame name="a" src="frame_a.html">' .
  510. '<frame name="b" src="frame_b.html">' .
  511. '<frame name="c" src="frame_c.html">' .
  512. '</frameset>';
  513. $browser = $this->createBrowser($this->createUserAgent(array(
  514. 'http://site.with.frames/' => $frameset,
  515. 'http://site.with.frames/frame_a.html' => 'A frame',
  516. 'http://site.with.frames/frame_b.html' => 'B frame',
  517. 'http://site.with.frames/frame_c.html' => 'C frame')));
  518. $this->assertEqual(
  519. $browser->get('http://site.with.frames/'),
  520. 'A frameB frameC frame');
  521. $this->assertIdentical($browser->getFrames(), array(
  522. 'a' => 'http://site.with.frames/frame_a.html',
  523. 'b' => 'http://site.with.frames/frame_b.html',
  524. 'c' => 'http://site.with.frames/frame_c.html'));
  525. }
  526. function testFrameFocusByName() {
  527. $frameset = '<frameset>' .
  528. '<frame name="a" src="frame_a.html">' .
  529. '<frame name="b" src="frame_b.html">' .
  530. '<frame name="c" src="frame_c.html">' .
  531. '</frameset>';
  532. $browser = $this->createBrowser($this->createUserAgent(array(
  533. 'http://site.with.frames/' => $frameset,
  534. 'http://site.with.frames/frame_a.html' => 'A frame',
  535. 'http://site.with.frames/frame_b.html' => 'B frame',
  536. 'http://site.with.frames/frame_c.html' => 'C frame')));
  537. $browser->get('http://site.with.frames/');
  538. $browser->setFrameFocus('a');
  539. $this->assertEqual($browser->getContent(), 'A frame');
  540. $browser->setFrameFocus('b');
  541. $this->assertEqual($browser->getContent(), 'B frame');
  542. $browser->setFrameFocus('c');
  543. $this->assertEqual($browser->getContent(), 'C frame');
  544. }
  545. function testFramesetWithSomeNamedFrames() {
  546. $frameset = '<frameset>' .
  547. '<frame name="a" src="frame_a.html">' .
  548. '<frame src="frame_b.html">' .
  549. '<frame name="c" src="frame_c.html">' .
  550. '<frame src="frame_d.html">' .
  551. '</frameset>';
  552. $browser = $this->createBrowser($this->createUserAgent(array(
  553. 'http://site.with.frames/' => $frameset,
  554. 'http://site.with.frames/frame_a.html' => 'A frame',
  555. 'http://site.with.frames/frame_b.html' => 'B frame',
  556. 'http://site.with.frames/frame_c.html' => 'C frame',
  557. 'http://site.with.frames/frame_d.html' => 'D frame')));
  558. $this->assertEqual(
  559. $browser->get('http://site.with.frames/'),
  560. 'A frameB frameC frameD frame');
  561. $this->assertIdentical($browser->getFrames(), array(
  562. 'a' => 'http://site.with.frames/frame_a.html',
  563. 2 => 'http://site.with.frames/frame_b.html',
  564. 'c' => 'http://site.with.frames/frame_c.html',
  565. 4 => 'http://site.with.frames/frame_d.html'));
  566. }
  567. function testFrameFocusWithMixedNamesAndIndexes() {
  568. $frameset = '<frameset>' .
  569. '<frame name="a" src="frame_a.html">' .
  570. '<frame src="frame_b.html">' .
  571. '<frame name="c" src="frame_c.html">' .
  572. '<frame src="frame_d.html">' .
  573. '</frameset>';
  574. $browser = $this->createBrowser($this->createUserAgent(array(
  575. 'http://site.with.frames/' => $frameset,
  576. 'http://site.with.frames/frame_a.html' => 'A frame',
  577. 'http://site.with.frames/frame_b.html' => 'B frame',
  578. 'http://site.with.frames/frame_c.html' => 'C frame',
  579. 'http://site.with.frames/frame_d.html' => 'D frame')));
  580. $browser->get('http://site.with.frames/');
  581. $browser->setFrameFocus('a');
  582. $this->assertEqual($browser->getContent(), 'A frame');
  583. $browser->setFrameFocus(2);
  584. $this->assertEqual($browser->getContent(), 'B frame');
  585. $browser->setFrameFocus('c');
  586. $this->assertEqual($browser->getContent(), 'C frame');
  587. $browser->setFrameFocus(4);
  588. $this->assertEqual($browser->getContent(), 'D frame');
  589. $browser->clearFrameFocus();
  590. $this->assertEqual($browser->getContent(), 'A frameB frameC frameD frame');
  591. }
  592. function testNestedFrameset() {
  593. $inner = '<frameset>' .
  594. '<frame name="page" src="page.html">' .
  595. '</frameset>';
  596. $outer = '<frameset>' .
  597. '<frame name="inner" src="inner.html">' .
  598. '</frameset>';
  599. $browser = $this->createBrowser($this->createUserAgent(array(
  600. 'http://site.with.nested.frame/' => $outer,
  601. 'http://site.with.nested.frame/inner.html' => $inner,
  602. 'http://site.with.nested.frame/page.html' => 'The page')));
  603. $this->assertEqual(
  604. $browser->get('http://site.with.nested.frame/'),
  605. 'The page');
  606. $this->assertIdentical($browser->getFrames(), array(
  607. 'inner' => array(
  608. 'page' => 'http://site.with.nested.frame/page.html')));
  609. }
  610. function testCanNavigateToNestedFrame() {
  611. $inner = '<frameset>' .
  612. '<frame name="one" src="one.html">' .
  613. '<frame name="two" src="two.html">' .
  614. '</frameset>';
  615. $outer = '<frameset>' .
  616. '<frame name="inner" src="inner.html">' .
  617. '<frame name="three" src="three.html">' .
  618. '</frameset>';
  619. $browser = $this->createBrowser($this->createUserAgent(array(
  620. 'http://site.with.nested.frames/' => $outer,
  621. 'http://site.with.nested.frames/inner.html' => $inner,
  622. 'http://site.with.nested.frames/one.html' => 'Page one',
  623. 'http://site.with.nested.frames/two.html' => 'Page two',
  624. 'http://site.with.nested.frames/three.html' => 'Page three')));
  625. $browser->get('http://site.with.nested.frames/');
  626. $this->assertEqual($browser->getContent(), 'Page onePage twoPage three');
  627. $this->assertTrue($browser->setFrameFocus('inner'));
  628. $this->assertEqual($browser->getFrameFocus(), array('inner'));
  629. $this->assertTrue($browser->setFrameFocus('one'));
  630. $this->assertEqual($browser->getFrameFocus(), array('inner', 'one'));
  631. $this->assertEqual($browser->getContent(), 'Page one');
  632. $this->assertTrue($browser->setFrameFocus('two'));
  633. $this->assertEqual($browser->getFrameFocus(), array('inner', 'two'));
  634. $this->assertEqual($browser->getContent(), 'Page two');
  635. $browser->clearFrameFocus();
  636. $this->assertTrue($browser->setFrameFocus('three'));
  637. $this->assertEqual($browser->getFrameFocus(), array('three'));
  638. $this->assertEqual($browser->getContent(), 'Page three');
  639. $this->assertTrue($browser->setFrameFocus('inner'));
  640. $this->assertEqual($browser->getContent(), 'Page onePage two');
  641. }
  642. function testCanNavigateToNestedFrameByIndex() {
  643. $inner = '<frameset>' .
  644. '<frame src="one.html">' .
  645. '<frame src="two.html">' .
  646. '</frameset>';
  647. $outer = '<frameset>' .
  648. '<frame src="inner.html">' .
  649. '<frame src="three.html">' .
  650. '</frameset>';
  651. $browser = $this->createBrowser($this->createUserAgent(array(
  652. 'http://site.with.nested.frames/' => $outer,
  653. 'http://site.with.nested.frames/inner.html' => $inner,
  654. 'http://site.with.nested.frames/one.html' => 'Page one',
  655. 'http://site.with.nested.frames/two.html' => 'Page two',
  656. 'http://site.with.nested.frames/three.html' => 'Page three')));
  657. $browser->get('http://site.with.nested.frames/');
  658. $this->assertEqual($browser->getContent(), 'Page onePage twoPage three');
  659. $this->assertTrue($browser->setFrameFocusByIndex(1));
  660. $this->assertEqual($browser->getFrameFocus(), array(1));
  661. $this->assertTrue($browser->setFrameFocusByIndex(1));
  662. $this->assertEqual($browser->getFrameFocus(), array(1, 1));
  663. $this->assertEqual($browser->getContent(), 'Page one');
  664. $this->assertTrue($browser->setFrameFocusByIndex(2));
  665. $this->assertEqual($browser->getFrameFocus(), array(1, 2));
  666. $this->assertEqual($browser->getContent(), 'Page two');
  667. $browser->clearFrameFocus();
  668. $this->assertTrue($browser->setFrameFocusByIndex(2));
  669. $this->assertEqual($browser->getFrameFocus(), array(2));
  670. $this->assertEqual($browser->getContent(), 'Page three');
  671. $this->assertTrue($browser->setFrameFocusByIndex(1));
  672. $this->assertEqual($browser->getContent(), 'Page onePage two');
  673. }
  674. }
  675. ?>