GeneralTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace Behat\Mink\Tests\Driver\Form;
  3. use Behat\Mink\Tests\Driver\TestCase;
  4. class GeneralTest extends TestCase
  5. {
  6. // test multiple submit buttons
  7. public function testIssue212()
  8. {
  9. $session = $this->getSession();
  10. $session->visit($this->pathTo('/issue212.html'));
  11. $field = $this->findById('poney-button');
  12. $this->assertEquals('poney', $field->getValue());
  13. }
  14. public function testBasicForm()
  15. {
  16. $this->getSession()->visit($this->pathTo('/basic_form.html'));
  17. $webAssert = $this->getAssertSession();
  18. $page = $this->getSession()->getPage();
  19. $this->assertEquals('Basic Form Page', $webAssert->elementExists('css', 'h1')->getText());
  20. $firstname = $webAssert->fieldExists('first_name');
  21. $lastname = $webAssert->fieldExists('lastn');
  22. $this->assertEquals('Firstname', $firstname->getValue());
  23. $this->assertEquals('Lastname', $lastname->getValue());
  24. $firstname->setValue('Konstantin');
  25. $page->fillField('last_name', 'Kudryashov');
  26. $this->assertEquals('Konstantin', $firstname->getValue());
  27. $this->assertEquals('Kudryashov', $lastname->getValue());
  28. $page->findButton('Reset')->click();
  29. $this->assertEquals('Firstname', $firstname->getValue());
  30. $this->assertEquals('Lastname', $lastname->getValue());
  31. $firstname->setValue('Konstantin');
  32. $page->fillField('last_name', 'Kudryashov');
  33. $page->findButton('Save')->click();
  34. if ($this->safePageWait(5000, 'document.getElementById("first") !== null')) {
  35. $this->assertEquals('Anket for Konstantin', $webAssert->elementExists('css', 'h1')->getText());
  36. $this->assertEquals('Firstname: Konstantin', $webAssert->elementExists('css', '#first')->getText());
  37. $this->assertEquals('Lastname: Kudryashov', $webAssert->elementExists('css', '#last')->getText());
  38. }
  39. }
  40. /**
  41. * @dataProvider formSubmitWaysDataProvider
  42. */
  43. public function testFormSubmitWays($submitVia)
  44. {
  45. $session = $this->getSession();
  46. $session->visit($this->pathTo('/basic_form.html'));
  47. $page = $session->getPage();
  48. $webAssert = $this->getAssertSession();
  49. $firstname = $webAssert->fieldExists('first_name');
  50. $firstname->setValue('Konstantin');
  51. $page->findButton($submitVia)->click();
  52. if ($this->safePageWait(5000, 'document.getElementById("first") !== null')) {
  53. $this->assertEquals('Firstname: Konstantin', $webAssert->elementExists('css', '#first')->getText());
  54. } else {
  55. $this->fail('Form was never submitted');
  56. }
  57. }
  58. public function formSubmitWaysDataProvider()
  59. {
  60. return array(
  61. array('Save'),
  62. array('input-type-image'),
  63. array('button-without-type'),
  64. array('button-type-submit'),
  65. );
  66. }
  67. public function testFormSubmit()
  68. {
  69. $session = $this->getSession();
  70. $session->visit($this->pathTo('/basic_form.html'));
  71. $webAssert = $this->getAssertSession();
  72. $webAssert->fieldExists('first_name')->setValue('Konstantin');
  73. $webAssert->elementExists('xpath', 'descendant-or-self::form[1]')->submit();
  74. if ($this->safePageWait(5000, 'document.getElementById("first") !== null')) {
  75. $this->assertEquals('Firstname: Konstantin', $webAssert->elementExists('css', '#first')->getText());
  76. };
  77. }
  78. public function testFormSubmitWithoutButton()
  79. {
  80. $session = $this->getSession();
  81. $session->visit($this->pathTo('/form_without_button.html'));
  82. $webAssert = $this->getAssertSession();
  83. $webAssert->fieldExists('first_name')->setValue('Konstantin');
  84. $webAssert->elementExists('xpath', 'descendant-or-self::form[1]')->submit();
  85. if ($this->safePageWait(5000, 'document.getElementById("first") !== null')) {
  86. $this->assertEquals('Firstname: Konstantin', $webAssert->elementExists('css', '#first')->getText());
  87. };
  88. }
  89. public function testBasicGetForm()
  90. {
  91. $this->getSession()->visit($this->pathTo('/basic_get_form.php'));
  92. $webAssert = $this->getAssertSession();
  93. $page = $this->getSession()->getPage();
  94. $this->assertEquals('Basic Get Form Page', $webAssert->elementExists('css', 'h1')->getText());
  95. $search = $webAssert->fieldExists('q');
  96. $search->setValue('some#query');
  97. $page->pressButton('Find');
  98. $div = $webAssert->elementExists('css', 'div');
  99. $this->assertEquals('some#query', $div->getText());
  100. }
  101. public function testAdvancedForm()
  102. {
  103. $this->getSession()->visit($this->pathTo('/advanced_form.html'));
  104. $page = $this->getSession()->getPage();
  105. $page->fillField('first_name', 'ever');
  106. $page->fillField('last_name', 'zet');
  107. $page->pressButton('Register');
  108. $this->assertContains('no file', $page->getContent());
  109. $this->getSession()->visit($this->pathTo('/advanced_form.html'));
  110. $webAssert = $this->getAssertSession();
  111. $page = $this->getSession()->getPage();
  112. $this->assertEquals('ADvanced Form Page', $webAssert->elementExists('css', 'h1')->getText());
  113. $firstname = $webAssert->fieldExists('first_name');
  114. $lastname = $webAssert->fieldExists('lastn');
  115. $email = $webAssert->fieldExists('Your email:');
  116. $select = $webAssert->fieldExists('select_number');
  117. $sex = $webAssert->fieldExists('sex');
  118. $maillist = $webAssert->fieldExists('mail_list');
  119. $agreement = $webAssert->fieldExists('agreement');
  120. $notes = $webAssert->fieldExists('notes');
  121. $about = $webAssert->fieldExists('about');
  122. $this->assertEquals('Firstname', $firstname->getValue());
  123. $this->assertEquals('Lastname', $lastname->getValue());
  124. $this->assertEquals('your@email.com', $email->getValue());
  125. $this->assertEquals('20', $select->getValue());
  126. $this->assertEquals('w', $sex->getValue());
  127. $this->assertEquals('original notes', $notes->getValue());
  128. $this->assertEquals('on', $maillist->getValue());
  129. $this->assertNull($agreement->getValue());
  130. $this->assertTrue($maillist->isChecked());
  131. $this->assertFalse($agreement->isChecked());
  132. $agreement->check();
  133. $this->assertTrue($agreement->isChecked());
  134. $maillist->uncheck();
  135. $this->assertFalse($maillist->isChecked());
  136. $select->selectOption('thirty');
  137. $this->assertEquals('30', $select->getValue());
  138. $sex->selectOption('m');
  139. $this->assertEquals('m', $sex->getValue());
  140. $notes->setValue('new notes');
  141. $this->assertEquals('new notes', $notes->getValue());
  142. $about->attachFile($this->mapRemoteFilePath(__DIR__.'/../../web-fixtures/some_file.txt'));
  143. $button = $page->findButton('Register');
  144. $this->assertNotNull($button);
  145. $page->fillField('first_name', 'Foo "item"');
  146. $page->fillField('last_name', 'Bar');
  147. $page->fillField('Your email:', 'ever.zet@gmail.com');
  148. $this->assertEquals('Foo "item"', $firstname->getValue());
  149. $this->assertEquals('Bar', $lastname->getValue());
  150. $button->press();
  151. if ($this->safePageWait(5000, 'document.getElementsByTagName("title") !== null')) {
  152. $out = <<<OUT
  153. array (
  154. 'agreement' = 'on',
  155. 'email' = 'ever.zet@gmail.com',
  156. 'first_name' = 'Foo &quot;item&quot;',
  157. 'last_name' = 'Bar',
  158. 'notes' = 'new notes',
  159. 'select_number' = '30',
  160. 'sex' = 'm',
  161. 'submit' = 'Register',
  162. )
  163. some_file.txt
  164. 1 uploaded file
  165. OUT;
  166. $this->assertContains($out, $page->getContent());
  167. }
  168. }
  169. public function testMultiInput()
  170. {
  171. $this->getSession()->visit($this->pathTo('/multi_input_form.html'));
  172. $page = $this->getSession()->getPage();
  173. $webAssert = $this->getAssertSession();
  174. $this->assertEquals('Multi input Test', $webAssert->elementExists('css', 'h1')->getText());
  175. $first = $webAssert->fieldExists('First');
  176. $second = $webAssert->fieldExists('Second');
  177. $third = $webAssert->fieldExists('Third');
  178. $this->assertEquals('tag1', $first->getValue());
  179. $this->assertSame('tag2', $second->getValue());
  180. $this->assertEquals('tag1', $third->getValue());
  181. $first->setValue('tag2');
  182. $this->assertEquals('tag2', $first->getValue());
  183. $this->assertSame('tag2', $second->getValue());
  184. $this->assertEquals('tag1', $third->getValue());
  185. $second->setValue('one');
  186. $this->assertEquals('tag2', $first->getValue());
  187. $this->assertSame('one', $second->getValue());
  188. $third->setValue('tag3');
  189. $this->assertEquals('tag2', $first->getValue());
  190. $this->assertSame('one', $second->getValue());
  191. $this->assertEquals('tag3', $third->getValue());
  192. $button = $page->findButton('Register');
  193. $this->assertNotNull($button);
  194. $button->press();
  195. $space = ' ';
  196. $out = <<<OUT
  197. 'tags' =$space
  198. array (
  199. 0 = 'tag2',
  200. 1 = 'one',
  201. 2 = 'tag3',
  202. ),
  203. OUT;
  204. $this->assertContains($out, $page->getContent());
  205. }
  206. public function testAdvancedFormSecondSubmit()
  207. {
  208. $this->getSession()->visit($this->pathTo('/advanced_form.html'));
  209. $page = $this->getSession()->getPage();
  210. $button = $page->findButton('Login');
  211. $this->assertNotNull($button);
  212. $button->press();
  213. $toSearch = array(
  214. "'agreement' = 'off',",
  215. "'submit' = 'Login',",
  216. 'no file',
  217. );
  218. $pageContent = $page->getContent();
  219. foreach ($toSearch as $searchString) {
  220. $this->assertContains($searchString, $pageContent);
  221. }
  222. }
  223. public function testSubmitEmptyTextarea()
  224. {
  225. $this->getSession()->visit($this->pathTo('/empty_textarea.html'));
  226. $page = $this->getSession()->getPage();
  227. $page->pressButton('Save');
  228. $toSearch = array(
  229. "'textarea' = '',",
  230. "'submit' = 'Save',",
  231. 'no file',
  232. );
  233. $pageContent = $page->getContent();
  234. foreach ($toSearch as $searchString) {
  235. $this->assertContains($searchString, $pageContent);
  236. }
  237. }
  238. }