TestsForWeb.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. <?php
  2. /**
  3. * Author: davert
  4. * Date: 13.01.12
  5. *
  6. * Class TestsForMink
  7. * Description:
  8. *
  9. */
  10. abstract class TestsForWeb extends \Codeception\TestCase\Test
  11. {
  12. /**
  13. * @var \Codeception\Module\PhpBrowser
  14. */
  15. protected $module;
  16. public function testAmOnPage()
  17. {
  18. $this->module->amOnPage('/');
  19. $this->module->see('Welcome to test app!');
  20. $this->module->_cleanup();
  21. $this->module->amOnPage('/info');
  22. $this->module->see('Information');
  23. }
  24. public function testCurrentUrl()
  25. {
  26. $this->module->amOnPage('/info');
  27. $this->module->seeCurrentUrlEquals('/info');
  28. $this->module->dontSeeInCurrentUrl('/user');
  29. $this->module->dontSeeCurrentUrlMatches('~user~');
  30. $this->module->amOnPage('/form/checkbox');
  31. $this->module->seeCurrentUrlEquals('/form/checkbox');
  32. $this->module->seeInCurrentUrl('form');
  33. $this->module->seeCurrentUrlMatches('~form/.*~');
  34. $this->module->dontSeeCurrentUrlEquals('/');
  35. $this->module->dontSeeCurrentUrlMatches('~form/a~');
  36. $this->module->dontSeeInCurrentUrl('user');
  37. }
  38. public function testSee()
  39. {
  40. $this->module->amOnPage('/');
  41. $this->module->see('Welcome to test app!');
  42. $this->module->see('A wise man said: "debug!"');
  43. $this->module->see('Welcome to test app!', 'h1');
  44. $this->module->see('Some text with formatting on separate lines');
  45. $this->module->see('Some text with formatting on separate lines', '#area4');
  46. $this->module->see('on separate lines', '#area4 .someclass');
  47. //ensure backwards compatibility, this assertion passed before this change
  48. $this->module->see("Test Link \n\n\n Test");
  49. //Single quote HTML entities must be decoded
  50. $this->module->see("please don't provide us any personal information.");
  51. $this->module->amOnPage('/info');
  52. $this->module->see('valuable', 'p');
  53. $this->module->see('valuable', 'descendant-or-self::body/p');
  54. $this->module->dontSee('Welcome');
  55. $this->module->dontSee('valuable', 'h1');
  56. $this->module->dontSee('Welcome', 'h6');
  57. }
  58. public function testDontSeeFailsWhenMultilineTextMatches()
  59. {
  60. $this->shouldFail();
  61. $this->module->amOnPage('/');
  62. $this->module->dontSee('Some text with formatting on separate lines');
  63. }
  64. public function testDontSeeFailsWhenMultilineTextMatchesInSelector()
  65. {
  66. $this->shouldFail();
  67. $this->module->amOnPage('/');
  68. $this->module->dontSee('Some text with formatting on separate lines', '#area4');
  69. }
  70. /**
  71. * @Issue https://github.com/Codeception/Codeception/issues/3114
  72. */
  73. public function testSeeIsCaseInsensitiveForUnicodeText()
  74. {
  75. $this->module->amOnPage('/info');
  76. $this->module->see('ссылочка');
  77. $this->module->see('ссылочка', 'a');
  78. }
  79. public function testDontSeeIsCaseInsensitiveForUnicodeText()
  80. {
  81. $this->setExpectedException("PHPUnit_Framework_AssertionFailedError");
  82. $this->module->amOnPage('/info');
  83. $this->module->dontSee('ссылочка');
  84. }
  85. public function testSeeInSource()
  86. {
  87. $this->module->amOnPage('/');
  88. $this->module->seeInSource('<h1>Welcome to test app!</h1>');
  89. $this->module->seeInSource('A wise man said: "debug!"');
  90. $this->module->dontSeeInSource('John Cleese');
  91. }
  92. public function testSeeInCurrentUrl()
  93. {
  94. $this->module->amOnPage('/info');
  95. $this->module->seeInCurrentUrl('/info');
  96. }
  97. public function testSeeLink()
  98. {
  99. $this->module->amOnPage('/external_url');
  100. $this->module->seeLink('Next');
  101. $this->module->seeLink('Next', 'http://codeception.com/');
  102. }
  103. public function testDontSeeLink()
  104. {
  105. $this->module->amOnPage('/external_url');
  106. $this->module->dontSeeLink('Back');
  107. $this->module->dontSeeLink('Next', '/fsdfsdf/');
  108. }
  109. public function testSeeLinkFailsIfTextDoesNotMatch()
  110. {
  111. $this->setExpectedException(
  112. 'PHPUnit_Framework_AssertionFailedError',
  113. "No links containing text 'Codeception' were found in page /external_url"
  114. );
  115. $this->module->amOnPage('/external_url');
  116. $this->module->seeLink('Codeception');
  117. }
  118. public function testSeeLinkFailsIfHrefDoesNotMatch()
  119. {
  120. $this->setExpectedException(
  121. 'PHPUnit_Framework_AssertionFailedError',
  122. "No links containing text 'Next' and URL '/fsdfsdf/' were found in page /external_url"
  123. );
  124. $this->module->amOnPage('/external_url');
  125. $this->module->seeLink('Next', '/fsdfsdf/');
  126. }
  127. public function testDontSeeLinkFailsIfTextMatches()
  128. {
  129. $this->setExpectedException(
  130. 'PHPUnit_Framework_AssertionFailedError',
  131. "Link containing text 'Next' was found in page /external_url"
  132. );
  133. $this->module->amOnPage('/external_url');
  134. $this->module->dontSeeLink('Next');
  135. }
  136. public function testDontSeeLinkFailsIfTextAndUrlMatches()
  137. {
  138. $this->setExpectedException(
  139. 'PHPUnit_Framework_AssertionFailedError',
  140. "Link containing text 'Next' and URL 'http://codeception.com/' was found in page /external_url"
  141. );
  142. $this->module->amOnPage('/external_url');
  143. $this->module->dontSeeLink('Next', 'http://codeception.com/');
  144. }
  145. public function testClick()
  146. {
  147. $this->module->amOnPage('/');
  148. $this->module->click('More info');
  149. $this->module->seeInCurrentUrl('/info');
  150. $this->module->amOnPage('/');
  151. $this->module->click('#link');
  152. $this->module->seeInCurrentUrl('/info');
  153. $this->module->amOnPage('/');
  154. $this->module->click("descendant-or-self::a[@id = 'link']");
  155. $this->module->seeInCurrentUrl('/info');
  156. }
  157. public function testClickByName()
  158. {
  159. $this->module->amOnPage('/form/button');
  160. $this->module->click("btn0");
  161. $form = data::get('form');
  162. $this->assertEquals('val', $form['text']);
  163. }
  164. public function testClickByLinkTitle()
  165. {
  166. $this->module->amOnPage('/');
  167. $this->module->click("Link Title");
  168. $this->module->seeInCurrentUrl('/info');
  169. }
  170. public function testClickOnContext()
  171. {
  172. $this->module->amOnPage('/');
  173. $this->module->click('More info', 'p');
  174. $this->module->seeInCurrentUrl('/info');
  175. $this->module->amOnPage('/');
  176. $this->module->click('More info', 'body>p');
  177. $this->module->seeInCurrentUrl('/info');
  178. }
  179. public function testCheckboxByCss()
  180. {
  181. $this->module->amOnPage('/form/checkbox');
  182. $this->module->checkOption('#checkin');
  183. $this->module->click('Submit');
  184. $form = data::get('form');
  185. $this->assertEquals('agree', $form['terms']);
  186. }
  187. public function testCheckboxByName()
  188. {
  189. $this->module->amOnPage('/form/checkbox');
  190. $this->module->checkOption('terms');
  191. $this->module->click('Submit');
  192. $form = data::get('form');
  193. $this->assertEquals('agree', $form['terms']);
  194. }
  195. public function testCheckboxByLabel()
  196. {
  197. $this->module->amOnPage('/form/checkbox');
  198. $this->module->checkOption('I Agree');
  199. $this->module->click('Submit');
  200. $form = data::get('form');
  201. $this->assertEquals('agree', $form['terms']);
  202. }
  203. /**
  204. * @group testCheckboxArray
  205. * @Issue https://github.com/Codeception/Codeception/pull/1145
  206. */
  207. public function testCheckboxArray()
  208. {
  209. $this->module->amOnPage('/form/checkbox_array');
  210. $this->module->checkOption('#id2');
  211. $this->module->click('Submit');
  212. $form = data::get('form');
  213. $this->assertEquals('second', reset($form['field']));
  214. }
  215. public function testSelectByCss()
  216. {
  217. $this->module->amOnPage('/form/select');
  218. $this->module->selectOption('form select[name=age]', 'adult');
  219. $this->module->click('Submit');
  220. $form = data::get('form');
  221. $this->assertEquals('adult', $form['age']);
  222. }
  223. public function testSelectByName()
  224. {
  225. $this->module->amOnPage('/form/select');
  226. $this->module->selectOption('age', 'adult');
  227. $this->module->click('Submit');
  228. $form = data::get('form');
  229. $this->assertEquals('adult', $form['age']);
  230. }
  231. public function testSelectByLabel()
  232. {
  233. $this->module->amOnPage('/form/select');
  234. $this->module->selectOption('Select your age', 'dead');
  235. $this->module->click('Submit');
  236. $form = data::get('form');
  237. $this->assertEquals('dead', $form['age']);
  238. }
  239. public function testSelectByLabelAndOptionText()
  240. {
  241. $this->module->amOnPage('/form/select');
  242. $this->module->selectOption('Select your age', '21-60');
  243. $this->module->click('Submit');
  244. $form = data::get('form');
  245. $this->assertEquals('adult', $form['age']);
  246. }
  247. public function testSeeSelectedOption()
  248. {
  249. $this->module->amOnPage('/form/select');
  250. $this->module->seeOptionIsSelected('#age', '60-100');
  251. $this->module->dontSeeOptionIsSelected('#age', '100-210');
  252. }
  253. public function testSeeSelectedOptionForRadioButton()
  254. {
  255. $this->module->amOnPage('/form/example6');
  256. $this->module->seeOptionIsSelected('input[name=frequency]', 'hour');
  257. $this->module->dontSeeOptionIsSelected('input[name=frequency]', 'week');
  258. }
  259. /**
  260. * @Issue https://github.com/Codeception/Codeception/issues/2733
  261. */
  262. public function testSeeSelectedOptionReturnsFirstOptionIfNotSelected()
  263. {
  264. $this->module->amOnPage('/form/complex');
  265. $this->module->seeOptionIsSelected('#age', 'below 13');
  266. $this->module->click('Submit');
  267. $form = data::get('form');
  268. $this->assertEquals('child', $form['age'], 'first option was not submitted');
  269. }
  270. /**
  271. * @group testSubmitSeveralSubmitsForm
  272. * @Issue https://github.com/Codeception/Codeception/issues/1183
  273. */
  274. public function testSubmitSeveralSubmitsForm()
  275. {
  276. $this->module->amOnPage('/form/example8');
  277. $this->module->click('form button[value="second"]');
  278. $form = data::get('form');
  279. $this->assertEquals('second', $form['submit']);
  280. }
  281. /**
  282. * Additional test to make sure no off-by-one related problem.
  283. *
  284. * @group testSubmitSeveralSubmitsForm
  285. * @Issue https://github.com/Codeception/Codeception/issues/1183
  286. */
  287. public function testSubmitLotsOfSubmitsForm()
  288. {
  289. $this->module->amOnPage('/form/example11');
  290. $this->module->click('form button[value="fifth"]');
  291. $form = data::get('form');
  292. $this->assertEquals('fifth', $form['submit']);
  293. }
  294. public function testSelectMultipleOptionsByText()
  295. {
  296. $this->module->amOnPage('/form/select_multiple');
  297. $this->module->selectOption('What do you like the most?', array('Play Video Games', 'Have Sex'));
  298. $this->module->click('Submit');
  299. $form = data::get('form');
  300. $this->assertEquals(array('play', 'adult'), $form['like']);
  301. }
  302. public function testSelectMultipleOptionsByValue()
  303. {
  304. $this->module->amOnPage('/form/select_multiple');
  305. $this->module->selectOption('What do you like the most?', array('eat', 'adult'));
  306. $this->module->click('Submit');
  307. $form = data::get('form');
  308. $this->assertEquals(array('eat', 'adult'), $form['like']);
  309. }
  310. public function testHidden()
  311. {
  312. $this->module->amOnPage('/form/hidden');
  313. $this->module->click('Submit');
  314. $form = data::get('form');
  315. $this->assertEquals('kill_people', $form['action']);
  316. }
  317. public function testTextareaByCss()
  318. {
  319. $this->module->amOnPage('/form/textarea');
  320. $this->module->fillField('textarea', 'Nothing special');
  321. $this->module->click('Submit');
  322. $form = data::get('form');
  323. $this->assertEquals('Nothing special', $form['description']);
  324. }
  325. public function testTextareaByLabel()
  326. {
  327. $this->module->amOnPage('/form/textarea');
  328. $this->module->fillField('Description', 'Nothing special');
  329. $this->module->click('Submit');
  330. $form = data::get('form');
  331. $this->assertEquals('Nothing special', $form['description']);
  332. }
  333. public function testTextFieldByCss()
  334. {
  335. $this->module->amOnPage('/form/field');
  336. $this->module->fillField('#name', 'Nothing special');
  337. $this->module->click('Submit');
  338. $form = data::get('form');
  339. $this->assertEquals('Nothing special', $form['name']);
  340. }
  341. public function testTextFieldByName()
  342. {
  343. $this->module->amOnPage('/form/example1');
  344. $this->module->fillField('LoginForm[username]', 'davert');
  345. $this->module->fillField('LoginForm[password]', '123456');
  346. $this->module->click('Login');
  347. $login = data::get('form');
  348. $this->assertEquals('davert', $login['LoginForm']['username']);
  349. $this->assertEquals('123456', $login['LoginForm']['password']);
  350. }
  351. public function testTextFieldByLabel()
  352. {
  353. $this->module->amOnPage('/form/field');
  354. $this->module->fillField('Name', 'Nothing special');
  355. $this->module->click('Submit');
  356. $form = data::get('form');
  357. $this->assertEquals('Nothing special', $form['name']);
  358. }
  359. public function testTextFieldByLabelWithoutFor()
  360. {
  361. $this->module->amOnPage('/form/field');
  362. $this->module->fillField('Other label', 'Nothing special');
  363. $this->module->click('Submit');
  364. $form = data::get('form');
  365. $this->assertEquals('Nothing special', $form['othername']);
  366. }
  367. public function testFileFieldByCss()
  368. {
  369. $this->module->amOnPage('/form/file');
  370. $this->module->attachFile('#avatar', 'app/avatar.jpg');
  371. $this->module->click('Submit');
  372. $this->assertNotEmpty(data::get('files'));
  373. $files = data::get('files');
  374. $this->assertArrayHasKey('avatar', $files);
  375. }
  376. public function testFileFieldByLabel()
  377. {
  378. $this->module->amOnPage('/form/file');
  379. $this->module->attachFile('Avatar', 'app/avatar.jpg');
  380. $this->module->click('Submit');
  381. $this->assertNotEmpty(data::get('files'));
  382. }
  383. public function testSeeCheckboxIsNotChecked()
  384. {
  385. $this->module->amOnPage('/form/checkbox');
  386. $this->module->dontSeeCheckboxIsChecked('#checkin');
  387. $this->module->dontSeeCheckboxIsChecked('I Agree');
  388. }
  389. public function testSeeCheckboxChecked()
  390. {
  391. $this->module->amOnPage('/info');
  392. $this->module->seeCheckboxIsChecked('input[type=checkbox]');
  393. $this->module->seeCheckboxIsChecked('Checked');
  394. }
  395. public function testSeeWithNonLatin()
  396. {
  397. $this->module->amOnPage('/info');
  398. $this->module->see('на');
  399. }
  400. public function testSeeWithNonLatinAndSelectors()
  401. {
  402. $this->module->amOnPage('/info');
  403. $this->module->see('Текст', 'p');
  404. }
  405. public function testSeeInFieldOnInput()
  406. {
  407. $this->module->amOnPage('/form/field');
  408. $this->module->seeInField('Name', 'OLD_VALUE');
  409. $this->module->seeInField('input[name=name]', 'OLD_VALUE');
  410. $this->module->seeInField('descendant-or-self::input[@id="name"]', 'OLD_VALUE');
  411. }
  412. public function testSeeInFieldForEmptyInput()
  413. {
  414. $this->module->amOnPage('/form/empty');
  415. $this->module->seeInField('#empty_input', '');
  416. }
  417. public function testSeeInFieldOnTextarea()
  418. {
  419. $this->module->amOnPage('/form/textarea');
  420. $this->module->seeInField('Description', 'sunrise');
  421. $this->module->seeInField('textarea', 'sunrise');
  422. $this->module->seeInField('descendant-or-self::textarea[@id="description"]', 'sunrise');
  423. }
  424. public function testSeeInFieldForEmptyTextarea()
  425. {
  426. $this->module->amOnPage('/form/empty');
  427. $this->module->seeInField('#empty_textarea', '');
  428. }
  429. public function testSeeInFieldOnCheckbox()
  430. {
  431. $this->module->amOnPage('/form/field_values');
  432. $this->module->dontSeeInField('checkbox[]', 'not seen one');
  433. $this->module->seeInField('checkbox[]', 'see test one');
  434. $this->module->dontSeeInField('checkbox[]', 'not seen two');
  435. $this->module->seeInField('checkbox[]', 'see test two');
  436. $this->module->dontSeeInField('checkbox[]', 'not seen three');
  437. $this->module->seeInField('checkbox[]', 'see test three');
  438. }
  439. public function testSeeInFieldWithBoolean()
  440. {
  441. $this->module->amOnPage('/form/field_values');
  442. $this->module->seeInField('checkbox1', true);
  443. $this->module->dontSeeInField('checkbox1', false);
  444. $this->module->seeInField('checkbox2', false);
  445. $this->module->dontSeeInField('checkbox2', true);
  446. $this->module->seeInField('radio2', true);
  447. $this->module->dontSeeInField('radio2', false);
  448. $this->module->seeInField('radio3', false);
  449. $this->module->dontSeeInField('radio3', true);
  450. }
  451. public function testSeeInFieldOnRadio()
  452. {
  453. $this->module->amOnPage('/form/field_values');
  454. $this->module->seeInField('radio1', 'see test one');
  455. $this->module->dontSeeInField('radio1', 'not seen one');
  456. $this->module->dontSeeInField('radio1', 'not seen two');
  457. $this->module->dontSeeInField('radio1', 'not seen three');
  458. }
  459. public function testSeeInFieldOnSelect()
  460. {
  461. $this->module->amOnPage('/form/field_values');
  462. $this->module->seeInField('select1', 'see test one');
  463. $this->module->dontSeeInField('select1', 'not seen one');
  464. $this->module->dontSeeInField('select1', 'not seen two');
  465. $this->module->dontSeeInField('select1', 'not seen three');
  466. }
  467. public function testSeeInFieldEmptyValueForUnselectedSelect()
  468. {
  469. $this->module->amOnPage('/form/field_values');
  470. $this->module->seeInField('select3', '');
  471. }
  472. public function testSeeInFieldOnSelectMultiple()
  473. {
  474. $this->module->amOnPage('/form/field_values');
  475. $this->module->dontSeeInField('select2', 'not seen one');
  476. $this->module->seeInField('select2', 'see test one');
  477. $this->module->dontSeeInField('select2', 'not seen two');
  478. $this->module->seeInField('select2', 'see test two');
  479. $this->module->dontSeeInField('select2', 'not seen three');
  480. $this->module->seeInField('select2', 'see test three');
  481. }
  482. public function testSeeInFieldWithExactMatch()
  483. {
  484. $this->module->amOnPage('/form/field_values');
  485. $this->module->seeInField(array('name' => 'select2'), 'see test one');
  486. }
  487. public function testDontSeeInFieldOnInput()
  488. {
  489. $this->module->amOnPage('/form/field');
  490. $this->module->dontSeeInField('Name', 'Davert');
  491. $this->module->dontSeeInField('input[name=name]', 'Davert');
  492. $this->module->dontSeeInField('descendant-or-self::input[@id="name"]', 'Davert');
  493. }
  494. public function testDontSeeInFieldOnTextarea()
  495. {
  496. $this->module->amOnPage('/form/textarea');
  497. $this->module->dontSeeInField('Description', 'sunset');
  498. $this->module->dontSeeInField('textarea', 'sunset');
  499. $this->module->dontSeeInField('descendant-or-self::textarea[@id="description"]', 'sunset');
  500. }
  501. public function testSeeInFormFields()
  502. {
  503. $this->module->amOnPage('/form/field_values');
  504. $params = [
  505. 'checkbox[]' => [
  506. 'see test one',
  507. 'see test two',
  508. ],
  509. 'radio1' => 'see test one',
  510. 'checkbox1' => true,
  511. 'checkbox2' => false,
  512. 'select1' => 'see test one',
  513. 'select2' => [
  514. 'see test one',
  515. 'see test two',
  516. 'see test three'
  517. ]
  518. ];
  519. $this->module->seeInFormFields('form', $params);
  520. }
  521. public function testSeeInFormFieldsFails()
  522. {
  523. $this->module->amOnPage('/form/field_values');
  524. $this->setExpectedException("PHPUnit_Framework_AssertionFailedError");
  525. $params = [
  526. 'radio1' => 'something I should not see',
  527. 'checkbox1' => true,
  528. 'checkbox2' => false,
  529. 'select1' => 'see test one',
  530. 'select2' => [
  531. 'see test one',
  532. 'see test two',
  533. 'see test three'
  534. ]
  535. ];
  536. $this->module->seeInFormFields('form', $params);
  537. }
  538. public function testDontSeeInFormFields()
  539. {
  540. $this->module->amOnPage('/form/field_values');
  541. $params = [
  542. 'checkbox[]' => [
  543. 'not seen one',
  544. 'not seen two',
  545. ],
  546. 'radio1' => 'not seen one',
  547. 'checkbox1' => false,
  548. 'checkbox2' => true,
  549. 'select1' => 'not seen one',
  550. 'select2' => [
  551. 'not seen one',
  552. 'No where to be seen'
  553. ]
  554. ];
  555. $this->module->dontSeeInFormFields('form', $params);
  556. }
  557. public function testDontSeeInFormFieldsFails()
  558. {
  559. $this->module->amOnPage('/form/field_values');
  560. $this->setExpectedException("PHPUnit_Framework_AssertionFailedError");
  561. $params = [
  562. 'checkbox[]' => [
  563. 'wont see this anyway',
  564. 'see test one',
  565. ],
  566. 'select2' => [
  567. 'not seen one',
  568. 'No where to be seen'
  569. ]
  570. ];
  571. $this->module->dontSeeInFormFields('form', $params);
  572. }
  573. public function testSeeInFieldWithNonLatin()
  574. {
  575. $this->module->amOnPage('/info');
  576. $this->module->seeInField('rus', 'Верно');
  577. }
  578. public function testApostrophesInText()
  579. {
  580. $this->module->amOnPage('/info');
  581. $this->module->see("Don't do that at home!");
  582. $this->module->see("Don't do that at home!", 'h3');
  583. }
  584. public function testSign()
  585. {
  586. $this->module->amOnPage('/info');
  587. $this->module->seeLink('Sign in!');
  588. $this->module->amOnPage('/info');
  589. $this->module->click('Sign in!');
  590. }
  591. public function testGrabTextFrom()
  592. {
  593. $this->module->amOnPage('/');
  594. $result = $this->module->grabTextFrom('h1');
  595. $this->assertEquals("Welcome to test app!", $result);
  596. $result = $this->module->grabTextFrom('descendant-or-self::h1');
  597. $this->assertEquals("Welcome to test app!", $result);
  598. $result = $this->module->grabTextFrom('~Welcome to (\w+) app!~');
  599. $this->assertEquals('test', $result);
  600. }
  601. public function testGrabValueFrom()
  602. {
  603. $this->module->amOnPage('/form/hidden');
  604. $result = $this->module->grabValueFrom('#action');
  605. $this->assertEquals("kill_people", $result);
  606. $result = $this->module->grabValueFrom("descendant-or-self::form/descendant::input[@name='action']");
  607. $this->assertEquals("kill_people", $result);
  608. $this->module->amOnPage('/form/textarea');
  609. $result = $this->module->grabValueFrom('#description');
  610. $this->assertEquals('sunrise', $result);
  611. $this->module->amOnPage('/form/select');
  612. $result = $this->module->grabValueFrom('#age');
  613. $this->assertEquals('oldfag', $result);
  614. }
  615. /**
  616. * @see https://github.com/Codeception/Codeception/issues/3866
  617. */
  618. public function testGrabValueFromWithFillField()
  619. {
  620. $this->module->amOnPage('/form/bug3866');
  621. $this->module->fillField('empty', 'new value');
  622. $result = $this->module->grabValueFrom('#empty');
  623. $this->assertEquals('new value', $result);
  624. $this->module->fillField('empty_textarea', 'new value');
  625. $result = $this->module->grabValueFrom('#empty_textarea');
  626. $this->assertEquals('new value', $result);
  627. $this->module->fillField('//textarea[@name="textarea[name][]"]', 'new value');
  628. $result = $this->module->grabValueFrom('#textarea_with_square_bracket');
  629. $this->assertEquals('new value', $result);
  630. }
  631. public function testGrabAttributeFrom()
  632. {
  633. $this->module->amOnPage('/search');
  634. $this->assertEquals('get', $this->module->grabAttributeFrom('form', 'method'));
  635. }
  636. public function testLinksWithSimilarNames()
  637. {
  638. $this->module->amOnPage('/');
  639. $this->module->click('Test Link');
  640. $this->module->seeInCurrentUrl('/form/file');
  641. $this->module->amOnPage('/');
  642. $this->module->click('Test');
  643. $this->module->seeInCurrentUrl('/form/hidden');
  644. }
  645. public function testLinksWithDifferentContext()
  646. {
  647. $this->module->amOnPage('/');
  648. $this->module->click('Test', '#area1');
  649. $this->module->seeInCurrentUrl('/form/file');
  650. $this->module->amOnPage('/');
  651. $this->module->click('Test', '#area2');
  652. $this->module->seeInCurrentUrl('/form/hidden');
  653. }
  654. public function testSeeElementOnPage()
  655. {
  656. $this->module->amOnPage('/form/field');
  657. $this->module->seeElement('input[name=name]');
  658. $this->module->seeElement('input', ['name' => 'name']);
  659. $this->module->seeElement('input', ['id' => 'name']);
  660. $this->module->seeElement('descendant-or-self::input[@id="name"]');
  661. $this->module->dontSeeElement('#something-beyond');
  662. $this->module->dontSeeElement('input', ['id' => 'something-beyond']);
  663. $this->module->dontSeeElement('descendant-or-self::input[@id="something-beyond"]');
  664. }
  665. // regression test. https://github.com/Codeception/Codeception/issues/587
  666. public function testSeeElementOnPageFails()
  667. {
  668. $this->setExpectedException("PHPUnit_Framework_AssertionFailedError");
  669. $this->module->amOnPage('/form/field');
  670. $this->module->dontSeeElement('input[name=name]');
  671. }
  672. public function testCookies()
  673. {
  674. $cookie_name = 'test_cookie';
  675. $cookie_value = 'this is a test';
  676. $this->module->amOnPage('/');
  677. $this->module->setCookie('nocookie', '1111');
  678. $this->module->setCookie($cookie_name, $cookie_value);
  679. $this->module->setCookie('notthatcookie', '22222');
  680. $this->module->seeCookie($cookie_name);
  681. $this->module->dontSeeCookie('evil_cookie');
  682. $cookie = $this->module->grabCookie($cookie_name);
  683. $this->assertEquals($cookie_value, $cookie);
  684. $this->module->resetCookie($cookie_name);
  685. $this->module->dontSeeCookie($cookie_name);
  686. }
  687. public function testCookiesWithPath()
  688. {
  689. $cookie_name = 'cookie';
  690. $cookie_value = 'tasty';
  691. $this->module->amOnPage('/info');
  692. $this->module->setCookie($cookie_name, $cookie_value, ['path' => '/info']);
  693. $this->module->seeCookie($cookie_name, ['path' => '/info']);
  694. $this->module->dontSeeCookie('evil_cookie');
  695. $cookie = $this->module->grabCookie($cookie_name, ['path' => '/info']);
  696. $this->assertEquals($cookie_value, $cookie);
  697. $this->module->resetCookie($cookie_name, ['path' => '/info']);
  698. $this->module->dontSeeCookie($cookie_name, ['path' => '/info']);
  699. $this->module->dontSeeCookie($cookie_name);
  700. }
  701. public function testSendingCookies()
  702. {
  703. $this->module->amOnPage('/');
  704. $this->module->setCookie('nocookie', '1111');
  705. $this->module->amOnPage('/cookies');
  706. $this->module->see('nocookie', 'pre');
  707. }
  708. public function testPageTitle()
  709. {
  710. $this->module->amOnPage('/');
  711. $this->module->seeInTitle('TestEd Beta 2.0');
  712. $this->module->dontSeeInTitle('Welcome to test app');
  713. $this->module->amOnPage('/info');
  714. $this->module->dontSeeInTitle('TestEd Beta 2.0');
  715. }
  716. public function testSeeFails()
  717. {
  718. $this->shouldFail();
  719. $this->module->amOnPage('/');
  720. $this->module->see('Text not here');
  721. }
  722. public function testSeeInsideFails()
  723. {
  724. $this->shouldFail();
  725. $this->module->amOnPage('/info');
  726. $this->module->see('woups', 'p');
  727. }
  728. public function testDontSeeInInsideFails()
  729. {
  730. $this->shouldFail();
  731. $this->module->amOnPage('/info');
  732. $this->module->dontSee('interesting', 'p');
  733. }
  734. public function testSeeElementFails()
  735. {
  736. $this->shouldFail();
  737. $this->module->amOnPage('/info');
  738. $this->module->seeElement('.alert');
  739. }
  740. public function testDontSeeElementFails()
  741. {
  742. $this->shouldFail();
  743. $this->module->amOnPage('/info');
  744. $this->module->dontSeeElement('#back');
  745. }
  746. public function testSeeInFieldFail()
  747. {
  748. $this->shouldFail();
  749. $this->module->amOnPage('/form/empty');
  750. $this->module->seeInField('#empty_textarea', 'xxx');
  751. }
  752. public function testSeeInFieldOnTextareaFails()
  753. {
  754. $this->shouldFail();
  755. $this->module->amOnPage('/form/textarea');
  756. $this->module->dontSeeInField('Description', 'sunrise');
  757. }
  758. public function testSeeCheckboxIsNotCheckedFails()
  759. {
  760. $this->shouldFail();
  761. $this->module->amOnPage('/form/complex');
  762. $this->module->dontSeeCheckboxIsChecked('#checkin');
  763. }
  764. public function testSeeCheckboxCheckedFails()
  765. {
  766. $this->shouldFail();
  767. $this->module->amOnPage('/form/checkbox');
  768. $this->module->seeCheckboxIsChecked('#checkin');
  769. }
  770. public function testDontSeeElementOnPageFails()
  771. {
  772. $this->shouldFail();
  773. $this->module->amOnPage('/form/field');
  774. $this->module->dontSeeElement('descendant-or-self::input[@id="name"]');
  775. }
  776. public function testStrictLocators()
  777. {
  778. $this->module->amOnPage('/login');
  779. $this->module->seeElement(['id' => 'submit-label']);
  780. $this->module->seeElement(['name' => 'password']);
  781. $this->module->seeElement(['class' => 'optional']);
  782. $this->module->seeElement(['css' => 'form.global_form_box']);
  783. $this->module->seeElement(['xpath' => \Codeception\Util\Locator::tabIndex(4)]);
  784. $this->module->fillField(['name' => 'password'], '123456');
  785. $this->module->amOnPage('/form/select');
  786. $this->module->selectOption(['name' => 'age'], 'child');
  787. $this->module->amOnPage('/form/checkbox');
  788. $this->module->checkOption(['name' => 'terms']);
  789. $this->module->amOnPage('/');
  790. $this->module->seeElement(['link' => 'Test']);
  791. $this->module->click(['link' => 'Test']);
  792. $this->module->seeCurrentUrlEquals('/form/hidden');
  793. }
  794. public function testFailStrictLocators()
  795. {
  796. $this->shouldFail();
  797. $this->module->amOnPage('/form/checkbox');
  798. $this->module->checkOption(['name' => 'age']);
  799. }
  800. public function testExample1()
  801. {
  802. $this->module->amOnPage('/form/example1');
  803. $this->module->see('Login', 'button');
  804. $this->module->fillField('#LoginForm_username', 'davert');
  805. $this->module->fillField('#LoginForm_password', '123456');
  806. $this->module->checkOption('#LoginForm_rememberMe');
  807. $this->module->click('Login');
  808. $login = data::get('form');
  809. $this->assertEquals('davert', $login['LoginForm']['username']);
  810. $this->assertEquals('123456', $login['LoginForm']['password']);
  811. $this->assertNotEmpty($login['LoginForm']['rememberMe']);
  812. }
  813. public function testExample2()
  814. {
  815. $this->module->amOnPage('/form/example2');
  816. $this->module->fillField('input[name=username]', 'davert');
  817. $this->module->fillField('input[name=password]', '123456');
  818. $this->module->click('Log on');
  819. $login = data::get('form');
  820. $this->assertEquals('davert', $login['username']);
  821. $this->assertEquals('123456', $login['password']);
  822. $this->assertEquals('login', $login['action']);
  823. }
  824. public function testAmpersand()
  825. {
  826. $this->module->amOnPage('/info');
  827. $this->module->see('Kill & Destroy');
  828. $this->module->see('Kill & Destroy', 'div');
  829. }
  830. /**
  831. * https://github.com/Codeception/Codeception/issues/1091
  832. */
  833. public function testExample4()
  834. {
  835. $this->module->amOnPage('/form/example4');
  836. $this->module->click(['css' => '#register button[type="submit"]']);
  837. $this->module->amOnPage('/form/example4');
  838. $this->module->click('#register button[type="submit"]');
  839. }
  840. /**
  841. * https://github.com/Codeception/Codeception/issues/1098
  842. */
  843. public function testExample5()
  844. {
  845. $this->module->amOnPage('/form/example5');
  846. $this->module->fillField('username', 'John');
  847. $this->module->fillField('password', '1234');
  848. $this->module->click('Login');
  849. $this->module->seeCurrentUrlEquals('/form/example5?username=John&password=1234');
  850. }
  851. public function testExample5WithSubmitForm()
  852. {
  853. $this->module->amOnPage('/form/example5');
  854. $this->module->submitForm('form', ['username' => 'John', 'password' => '1234']);
  855. $this->module->seeCurrentUrlEquals('/form/example5?username=John&password=1234');
  856. }
  857. public function testExample5WithParams()
  858. {
  859. $this->module->amOnPage('/form/example5?a=b');
  860. $this->module->fillField('username', 'John');
  861. $this->module->fillField('password', '1234');
  862. $this->module->click('Login');
  863. $this->module->seeCurrentUrlEquals('/form/example5?username=John&password=1234');
  864. }
  865. public function testExample5WithSubmitFormAndParams()
  866. {
  867. $this->module->amOnPage('/form/example5?a=b');
  868. $this->module->submitForm('form', ['username' => 'John', 'password' => '1234']);
  869. $this->module->seeCurrentUrlEquals('/form/example5?username=John&password=1234');
  870. }
  871. /**
  872. * @Issue https://github.com/Codeception/Codeception/issues/1212
  873. */
  874. public function testExample9()
  875. {
  876. $this->module->amOnPage('/form/example9');
  877. $this->module->attachFile('form[name=package_csv_form] input[name=xls_file]', 'app/avatar.jpg');
  878. $this->module->click('Upload packages', 'form[name=package_csv_form]');
  879. $this->assertNotEmpty(data::get('files'));
  880. $files = data::get('files');
  881. $this->assertArrayHasKey('xls_file', $files);
  882. $form = data::get('form');
  883. codecept_debug($form);
  884. $this->assertArrayHasKey('submit', $form);
  885. $this->assertArrayHasKey('MAX_FILE_SIZE', $form);
  886. $this->assertArrayHasKey('form_name', $form);
  887. }
  888. public function testSubmitForm()
  889. {
  890. $this->module->amOnPage('/form/complex');
  891. $this->module->submitForm('form', array(
  892. 'name' => 'Davert',
  893. 'description' => 'Is Codeception maintainer'
  894. ));
  895. $form = data::get('form');
  896. $this->assertEquals('Davert', $form['name']);
  897. $this->assertEquals('Is Codeception maintainer', $form['description']);
  898. $this->assertFalse(isset($form['disabled_fieldset']));
  899. $this->assertFalse(isset($form['disabled_field']));
  900. $this->assertEquals('kill_all', $form['action']);
  901. }
  902. public function testSubmitFormWithFillField()
  903. {
  904. $this->module->amOnPage('/form/complex');
  905. $this->module->fillField('name', 'Kilgore Trout');
  906. $this->module->fillField('description', 'Is a fish');
  907. $this->module->submitForm('form', [
  908. 'description' => 'Is from Iliyum, NY'
  909. ]);
  910. $form = data::get('form');
  911. $this->assertEquals('Kilgore Trout', $form['name']);
  912. $this->assertEquals('Is from Iliyum, NY', $form['description']);
  913. }
  914. public function testSubmitFormWithoutButton()
  915. {
  916. $this->module->amOnPage('/form/empty');
  917. $this->module->submitForm('form', array(
  918. 'text' => 'Hello!'
  919. ));
  920. $form = data::get('form');
  921. $this->assertEquals('Hello!', $form['text']);
  922. }
  923. public function testSubmitFormWithAmpersand()
  924. {
  925. $this->module->amOnPage('/form/submitform_ampersands');
  926. $this->module->submitForm('form', []);
  927. $form = data::get('form');
  928. $this->assertEquals('this & that', $form['test']);
  929. }
  930. public function testSubmitFormMultiSelectWithArrayParameter()
  931. {
  932. $this->module->amOnPage('/form/submitform_multiple');
  933. $this->module->submitForm('form', [
  934. 'select' => [
  935. 'see test one',
  936. 'not seen four'
  937. ]
  938. ]);
  939. $form = data::get('form');
  940. $this->assertCount(2, $form['select']);
  941. $this->assertEquals('see test one', $form['select'][0]);
  942. $this->assertEquals('not seen four', $form['select'][1]);
  943. }
  944. public function testSubmitFormWithMultiSelect()
  945. {
  946. $this->module->amOnPage('/form/submitform_multiple');
  947. $this->module->submitForm('form', []);
  948. $form = data::get('form');
  949. $this->assertCount(2, $form['select']);
  950. $this->assertEquals('see test one', $form['select'][0]);
  951. $this->assertEquals('see test two', $form['select'][1]);
  952. }
  953. public function testSubmitFormCheckboxWithArrayParameter()
  954. {
  955. $this->module->amOnPage('/form/field_values');
  956. $this->module->submitForm('form', [
  957. 'checkbox' => [
  958. 'not seen one',
  959. 'see test two',
  960. 'not seen three'
  961. ]
  962. ]);
  963. $form = data::get('form');
  964. $this->assertCount(3, $form['checkbox']);
  965. $this->assertEquals('not seen one', $form['checkbox'][0]);
  966. $this->assertEquals('see test two', $form['checkbox'][1]);
  967. $this->assertEquals('not seen three', $form['checkbox'][2]);
  968. }
  969. public function testSubmitFormCheckboxWithBooleanArrayParameter()
  970. {
  971. $this->module->amOnPage('/form/field_values');
  972. $this->module->submitForm('form', [
  973. 'checkbox' => [
  974. true,
  975. false,
  976. true
  977. ]
  978. ]);
  979. $form = data::get('form');
  980. $this->assertCount(2, $form['checkbox']);
  981. $this->assertEquals('not seen one', $form['checkbox'][0]);
  982. $this->assertEquals('not seen two', $form['checkbox'][1]);
  983. }
  984. /**
  985. * https://github.com/Codeception/Codeception/issues/1381
  986. */
  987. public function testFillingFormFieldWithoutSubmitButton()
  988. {
  989. $this->module->amOnPage('/form/empty_fill');
  990. $this->module->fillField('test', 'value');
  991. }
  992. public function testSubmitFormWithDefaultTextareaValue()
  993. {
  994. $this->module->amOnPage('/form/textarea');
  995. $this->module->submitForm('form', []);
  996. $form = data::get('form');
  997. $this->assertEquals('sunrise', $form['description']);
  998. }
  999. /**
  1000. * @issue #1180
  1001. */
  1002. public function testClickLinkWithInnerSpan()
  1003. {
  1004. $this->module->amOnPage('/form/example7');
  1005. $this->module->click("Buy Chocolate Bar");
  1006. $this->module->seeCurrentUrlEquals('/');
  1007. }
  1008. /*
  1009. * @issue #1304
  1010. */
  1011. public function testSelectTwoSubmitsByText()
  1012. {
  1013. $this->module->amOnPage('/form/select_two_submits');
  1014. $this->module->selectOption('What kind of sandwich would you like?', 2);
  1015. $this->module->click('Save');
  1016. $form = data::get('form');
  1017. $this->assertEquals(2, $form['sandwich_select']);
  1018. }
  1019. public function testSelectTwoSubmitsByCSS()
  1020. {
  1021. $this->module->amOnPage('/form/select_two_submits');
  1022. $this->module->selectOption("form select[name='sandwich_select']", '2');
  1023. $this->module->click('Save');
  1024. $form = data::get('form');
  1025. $this->assertEquals(2, $form['sandwich_select']);
  1026. }
  1027. protected function shouldFail()
  1028. {
  1029. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  1030. }
  1031. /**
  1032. * https://github.com/Codeception/Codeception/issues/1051
  1033. */
  1034. public function testSubmitFormWithTwoSubmitButtonsSubmitsCorrectValue()
  1035. {
  1036. $this->module->amOnPage('/form/example10');
  1037. $this->module->seeElement("#button2");
  1038. $this->module->click("#button2");
  1039. $form = data::get('form');
  1040. $this->assertTrue(isset($form['button2']));
  1041. $this->assertTrue(isset($form['username']));
  1042. $this->assertEquals('value2', $form['button2']);
  1043. $this->assertEquals('fred', $form['username']);
  1044. }
  1045. /**
  1046. * https://github.com/Codeception/Codeception/issues/1051
  1047. */
  1048. public function testSubmitFormWithTwoSubmitButtonsSubmitsCorrectValueAfterFillField()
  1049. {
  1050. $this->module->amOnPage('/form/example10');
  1051. $this->module->fillField("username", "bob");
  1052. $this->module->click("#button2");
  1053. $form = data::get('form');
  1054. $this->assertTrue(isset($form['button2']));
  1055. $this->assertTrue(isset($form['username']));
  1056. $this->assertEquals('value2', $form['button2']);
  1057. $this->assertEquals('bob', $form['username']);
  1058. }
  1059. /*
  1060. * https://github.com/Codeception/Codeception/issues/1274
  1061. */
  1062. public function testSubmitFormWithDocRelativePathForAction()
  1063. {
  1064. $this->module->amOnPage('/form/example12');
  1065. $this->module->submitForm('form', array(
  1066. 'test' => 'value'
  1067. ));
  1068. $this->module->seeCurrentUrlEquals('/form/example11');
  1069. }
  1070. public function testSubmitFormWithDocRelativePathForActionFromDefaultPage()
  1071. {
  1072. $this->module->amOnPage('/form/');
  1073. $this->module->submitForm('form', array(
  1074. 'test' => 'value'
  1075. ));
  1076. $this->module->seeCurrentUrlEquals('/form/example11');
  1077. }
  1078. public function testLinkWithDocRelativeURLFromDefaultPage()
  1079. {
  1080. $this->module->amOnPage('/form/');
  1081. $this->module->click('Doc-Relative Link');
  1082. $this->module->seeCurrentUrlEquals('/form/example11');
  1083. }
  1084. /*
  1085. * https://github.com/Codeception/Codeception/issues/1507
  1086. */
  1087. public function testSubmitFormWithDefaultRadioAndCheckboxValues()
  1088. {
  1089. $this->module->amOnPage('/form/example16');
  1090. $this->module->submitForm('form', array(
  1091. 'test' => 'value'
  1092. ));
  1093. $form = data::get('form');
  1094. $this->assertTrue(isset($form['checkbox1']), 'Checkbox value not sent');
  1095. $this->assertTrue(isset($form['radio1']), 'Radio button value not sent');
  1096. $this->assertEquals('testing', $form['checkbox1']);
  1097. $this->assertEquals('to be sent', $form['radio1']);
  1098. }
  1099. public function testSubmitFormCheckboxWithBoolean()
  1100. {
  1101. $this->module->amOnPage('/form/example16');
  1102. $this->module->submitForm('form', array(
  1103. 'checkbox1' => true
  1104. ));
  1105. $form = data::get('form');
  1106. $this->assertTrue(isset($form['checkbox1']), 'Checkbox value not sent');
  1107. $this->assertEquals('testing', $form['checkbox1']);
  1108. $this->module->amOnPage('/form/example16');
  1109. $this->module->submitForm('form', array(
  1110. 'checkbox1' => false
  1111. ));
  1112. $form = data::get('form');
  1113. $this->assertFalse(isset($form['checkbox1']), 'Checkbox value sent');
  1114. }
  1115. public function testSubmitFormWithButtons()
  1116. {
  1117. $this->module->amOnPage('/form/form_with_buttons');
  1118. $this->module->submitForm('form', array(
  1119. 'test' => 'value',
  1120. ));
  1121. $form = data::get('form');
  1122. $this->assertFalse(
  1123. isset($form['button1']) || isset($form['button2']) || isset($form['button3']) || isset($form['button4']),
  1124. 'Button values should not be set'
  1125. );
  1126. $this->module->amOnPage('/form/form_with_buttons');
  1127. $this->module->submitForm('form', array(
  1128. 'test' => 'value',
  1129. ), 'button3');
  1130. $form = data::get('form');
  1131. $this->assertFalse(
  1132. isset($form['button1']) || isset($form['button2']) || isset($form['button4']),
  1133. 'Button values for buttons 1, 2 and 4 should not be set'
  1134. );
  1135. $this->assertTrue(isset($form['button3']), 'Button value for button3 should be set');
  1136. $this->assertEquals($form['button3'], 'third', 'Button value for button3 should equal third');
  1137. $this->module->amOnPage('/form/form_with_buttons');
  1138. $this->module->submitForm('form', array(
  1139. 'test' => 'value',
  1140. ), 'button4');
  1141. $form = data::get('form');
  1142. $this->assertFalse(
  1143. isset($form['button1']) || isset($form['button2']) || isset($form['button3']),
  1144. 'Button values for buttons 1, 2 and 3 should not be set'
  1145. );
  1146. $this->assertTrue(isset($form['button4']), 'Button value for button4 should be set');
  1147. $this->assertEquals($form['button4'], 'fourth', 'Button value for button4 should equal fourth');
  1148. }
  1149. /**
  1150. * https://github.com/Codeception/Codeception/issues/1409
  1151. */
  1152. public function testWrongXpath()
  1153. {
  1154. $this->setExpectedException('Codeception\Exception\MalformedLocatorException');
  1155. $this->module->amOnPage('/');
  1156. $this->module->seeElement('//aas[asd}[sd]a[/[');
  1157. }
  1158. public function testWrongCSS()
  1159. {
  1160. $this->setExpectedException('Codeception\Exception\MalformedLocatorException');
  1161. $this->module->amOnPage('/');
  1162. $this->module->seeElement('.user#iasos<here');
  1163. }
  1164. public function testWrongStrictCSSLocator()
  1165. {
  1166. $this->setExpectedException('Codeception\Exception\MalformedLocatorException');
  1167. $this->module->amOnPage('/');
  1168. $this->module->seeElement(['css' => 'hel!1$<world']);
  1169. }
  1170. public function testWrongStrictXPathLocator()
  1171. {
  1172. $this->setExpectedException('Codeception\Exception\MalformedLocatorException');
  1173. $this->module->amOnPage('/');
  1174. $this->module->seeElement(['xpath' => 'hello<wo>rld']);
  1175. }
  1176. public function testFormWithFilesArray()
  1177. {
  1178. $this->module->amOnPage('/form/example13');
  1179. $this->module->attachFile('foo[bar]', 'app/avatar.jpg');
  1180. $this->module->attachFile('foo[baz]', 'app/avatar.jpg');
  1181. $this->module->click('Submit');
  1182. $this->assertNotEmpty(data::get('files'));
  1183. $files = data::get('files');
  1184. $this->assertArrayHasKey('bar', $files['foo']['name']);
  1185. $this->assertArrayHasKey('baz', $files['foo']['name']);
  1186. }
  1187. public function testFormWithFileSpecialCharNames()
  1188. {
  1189. $this->module->amOnPage('/form/example14');
  1190. $this->module->attachFile('foo bar', 'app/avatar.jpg');
  1191. $this->module->attachFile('foo.baz', 'app/avatar.jpg');
  1192. $this->module->click('Submit');
  1193. $this->assertNotEmpty(data::get('files'));
  1194. $files = data::get('files');
  1195. $this->assertNotEmpty($files);
  1196. $this->assertArrayHasKey('foo_bar', $files);
  1197. $this->assertArrayHasKey('foo_baz', $files);
  1198. }
  1199. /**
  1200. * @Issue https://github.com/Codeception/Codeception/issues/1454
  1201. */
  1202. public function testTextFieldByNameFirstNotCss()
  1203. {
  1204. $this->module->amOnPage('/form/example15');
  1205. $this->module->fillField('title', 'Special Widget');
  1206. $this->module->fillField('description', 'description');
  1207. $this->module->fillField('price', '19.99');
  1208. $this->module->click('Create');
  1209. $data = data::get('form');
  1210. $this->assertEquals('Special Widget', $data['title']);
  1211. }
  1212. /**
  1213. * @Issue https://github.com/Codeception/Codeception/issues/1535
  1214. */
  1215. public function testCheckingOptionsWithComplexNames()
  1216. {
  1217. $this->module->amOnPage('/form/bug1535');
  1218. $this->module->checkOption('#bmessage-topicslinks input[value="4"]');
  1219. $this->module->click('Submit');
  1220. $data = data::get('form');
  1221. $this->assertContains(4, $data['BMessage']['topicsLinks']);
  1222. }
  1223. /**
  1224. * @Issue https://github.com/Codeception/Codeception/issues/1585
  1225. * @Issue https://github.com/Codeception/Codeception/issues/1602
  1226. */
  1227. public function testUnreachableField()
  1228. {
  1229. $this->module->amOnPage('/form/bug1585');
  1230. $this->module->fillField('textarea[name="captions[]"]', 'test2');
  1231. $this->module->fillField('items[1][]', 'test3');
  1232. $this->module->fillField('input[name="users[]"]', 'davert');
  1233. $this->module->attachFile('input[name="files[]"]', 'app/avatar.jpg');
  1234. $this->module->click('Submit');
  1235. $data = data::get('form');
  1236. $this->assertContains('test3', $data['items'][1]);
  1237. $this->assertContains('test2', $data['captions']);
  1238. $this->assertContains('davert', $data['users']);
  1239. }
  1240. public function testSubmitAdjacentForms()
  1241. {
  1242. $this->module->amOnPage('/form/submit_adjacentforms');
  1243. $this->module->submitForm('#form-2', []);
  1244. $data = data::get('form');
  1245. $this->assertTrue(isset($data['second-field']));
  1246. $this->assertFalse(isset($data['first-field']));
  1247. $this->assertEquals('Killgore Trout', $data['second-field']);
  1248. }
  1249. public function testSubmitAdjacentFormsByButton()
  1250. {
  1251. $this->module->amOnPage('/form/submit_adjacentforms');
  1252. $this->module->fillField('first-field', 'First');
  1253. $this->module->fillField('second-field', 'Second');
  1254. $this->module->click('#submit1');
  1255. $data = data::get('form');
  1256. $this->assertTrue(isset($data['first-field']));
  1257. $this->assertFalse(isset($data['second-field']));
  1258. $this->assertEquals('First', $data['first-field']);
  1259. $this->module->amOnPage('/form/submit_adjacentforms');
  1260. $this->module->fillField('first-field', 'First');
  1261. $this->module->fillField('second-field', 'Second');
  1262. $this->module->click('#submit2');
  1263. $data = data::get('form');
  1264. $this->assertFalse(isset($data['first-field']));
  1265. $this->assertTrue(isset($data['second-field']));
  1266. $this->assertEquals('Second', $data['second-field']);
  1267. }
  1268. public function testArrayField()
  1269. {
  1270. $this->module->amOnPage('/form/example17');
  1271. $this->module->seeInField('input[name="FooBar[bar]"]', 'baz');
  1272. $this->module->seeInField('input[name="Food[beer][yum][yeah]"]', 'mmhm');
  1273. }
  1274. public function testFillFieldSquareBracketNames()
  1275. {
  1276. $this->module->amOnPage('/form/names-sq-brackets');
  1277. $this->module->fillField('//input[@name="input_text"]', 'filling this input');
  1278. $this->module->fillField('//input[@name="input[text][]"]', 'filling this input');
  1279. $this->module->fillField('//textarea[@name="textarea_name"]', 'filling this textarea');
  1280. $this->module->fillField('//textarea[@name="textarea[name][]"]', 'filling this textarea');
  1281. $this->module->fillField('//textarea[@name="textarea[name][]"]', 'filling this textarea once again');
  1282. $this->module->fillField('//textarea[@name="textarea_name"]', 'filling this textarea');
  1283. $this->module->fillField('//textarea[@name="textarea[name][]"]', 'filling this textarea more');
  1284. $this->module->fillField('//textarea[@name="textarea[name][]"]', 'filling this textarea most');
  1285. }
  1286. public function testSelectAndCheckOptionSquareBracketNames()
  1287. {
  1288. $this->module->amOnPage('/form/names-sq-brackets');
  1289. $this->module->selectOption('//input[@name="input_radio_name"]', '1');
  1290. $this->module->selectOption('//input[@name="input_radio_name"]', '2');
  1291. $this->module->checkOption('//input[@name="input_checkbox_name"]', '1');
  1292. $this->module->checkOption('//input[@name="input_checkbox_name"]', '2');
  1293. $this->module->checkOption('//input[@name="input[checkbox][name][]"]', '1');
  1294. $this->module->checkOption('//input[@name="input[checkbox][name][]"]', '2');
  1295. $this->module->checkOption('//input[@name="input[checkbox][name][]"]', '1');
  1296. $this->module->selectOption('//select[@name="select_name"]', '1');
  1297. $this->module->selectOption('//input[@name="input[radio][name][]"]', '1');
  1298. $this->module->selectOption('//input[@name="input[radio][name][]"]', '2');
  1299. $this->module->selectOption('//input[@name="input[radio][name][]"]', '1');
  1300. $this->module->selectOption('//select[@name="select[name][]"]', '1');
  1301. }
  1302. public function testFillFieldWithAmpersand()
  1303. {
  1304. $this->module->amOnPage('/form/field');
  1305. $this->module->fillField('Name', 'this & that');
  1306. $this->module->click('Submit');
  1307. $form = data::get('form');
  1308. $this->assertEquals('this & that', $form['name']);
  1309. }
  1310. public function testSeeInDeactivatedField()
  1311. {
  1312. $this->module->amOnPage('/form/complex');
  1313. $this->module->seeInField('#disabled_field', 'disabled_field');
  1314. $this->module->seeInField('#salutation', 'mr');
  1315. }
  1316. public function testSwitchToIframe()
  1317. {
  1318. $this->module->amOnPage('/iframe');
  1319. $this->module->switchToIframe('content');
  1320. $this->module->see('Is that interesting?');
  1321. $this->module->click('Ссылочка');
  1322. }
  1323. public function testGrabMultiple()
  1324. {
  1325. $this->module->amOnPage('/info');
  1326. $arr = $this->module->grabMultiple('#grab-multiple a:first-child');
  1327. $this->assertCount(1, $arr);
  1328. $this->assertEquals('First', $arr[0]);
  1329. $arr = $this->module->grabMultiple('#grab-multiple a');
  1330. $this->assertCount(3, $arr);
  1331. $this->assertEquals('First', $arr[0]);
  1332. $this->assertEquals('Second', $arr[1]);
  1333. $this->assertEquals('Third', $arr[2]);
  1334. // href for WebDriver with selenium returns a full link, so testing with ID
  1335. $arr = $this->module->grabMultiple('#grab-multiple a', 'id');
  1336. $this->assertCount(3, $arr);
  1337. $this->assertEquals('first-link', $arr[0]);
  1338. $this->assertEquals('second-link', $arr[1]);
  1339. $this->assertEquals('third-link', $arr[2]);
  1340. }
  1341. /**
  1342. * @issue https://github.com/Codeception/Codeception/issues/2960
  1343. */
  1344. public function testClickMultiByteLink()
  1345. {
  1346. $this->module->amOnPage('/info');
  1347. $this->module->click('Franšízy - pobočky');
  1348. $this->module->seeCurrentUrlEquals('/');
  1349. }
  1350. /**
  1351. * @issue https://github.com/Codeception/Codeception/issues/3528
  1352. */
  1353. public function testClickThrowsElementNotFoundExceptionWhenTextContainsNumber()
  1354. {
  1355. $this->setExpectedException('Codeception\Exception\ElementNotFound',
  1356. "'Link 2' is invalid CSS and XPath selector and Link or Button element with 'name=Link 2' was not found.");
  1357. $this->module->amOnPage('/info');
  1358. $this->module->click('Link 2');
  1359. }
  1360. public function testClickExistingLinkWithTextContainingNumber()
  1361. {
  1362. $this->module->amOnPage('/info');
  1363. $this->module->click('Link 3');
  1364. $this->module->seeCurrentUrlEquals('/cookies');
  1365. }
  1366. public function testSelectOptionValueSelector()
  1367. {
  1368. $this->module->amOnPage('/form/select_selectors');
  1369. $this->module->selectOption('age', ['value' => '20']);
  1370. $this->module->click('Submit');
  1371. $data = data::get('form');
  1372. $this->assertEquals('20', $data['age']);
  1373. }
  1374. public function testSelectOptionTextSelector()
  1375. {
  1376. $this->module->amOnPage('/form/select_selectors');
  1377. $this->module->selectOption('age', ['text' => '20']);
  1378. $this->module->seeOptionIsSelected('age', '20');
  1379. $this->module->selectOption('age', ['text' => '21']);
  1380. $this->module->seeOptionIsSelected('age', '21');
  1381. }
  1382. public function testClickButtonInLink()
  1383. {
  1384. $this->module->amOnPage('/form/button_in_link');
  1385. $this->module->click('More Info');
  1386. $this->module->seeCurrentUrlEquals('/info');
  1387. }
  1388. public function testClickButtonInLinkAndSpan()
  1389. {
  1390. $this->module->amOnPage('/form/button_in_link');
  1391. $this->module->click('Span Info');
  1392. $this->module->seeCurrentUrlEquals('/info');
  1393. }
  1394. public function testClickButtonInLinkUsingCssLocator()
  1395. {
  1396. $this->module->amOnPage('/form/button_in_link');
  1397. $this->module->click(['css' => 'input[value="More Info"]']);
  1398. $this->module->seeCurrentUrlEquals('/info');
  1399. }
  1400. public function testClickButtonInLinkAndSpanUsingCssLocator()
  1401. {
  1402. $this->module->amOnPage('/form/button_in_link');
  1403. $this->module->click(['css' => 'input[value="Span Info"]']);
  1404. $this->module->seeCurrentUrlEquals('/info');
  1405. }
  1406. public function testClickHashLink()
  1407. {
  1408. $this->module->amOnPage('/form/anchor');
  1409. $this->module->click('Hash Link');
  1410. $this->module->seeCurrentUrlEquals('/form/anchor');
  1411. }
  1412. public function testClickHashButton()
  1413. {
  1414. $this->module->amOnPage('/form/anchor');
  1415. $this->module->click('Hash Button');
  1416. $this->module->seeCurrentUrlEquals('/form/anchor');
  1417. }
  1418. public function testSubmitHashForm()
  1419. {
  1420. $this->module->amOnPage('/form/anchor');
  1421. $this->module->click('Hash Form');
  1422. $this->module->seeCurrentUrlEquals('/form/anchor');
  1423. }
  1424. public function testClickingRelativeLinkHonoursBaseHref()
  1425. {
  1426. $this->module->amOnPage('/basehref');
  1427. $this->module->click('Relative Link');
  1428. $this->module->seeCurrentUrlEquals('/form/example7');
  1429. }
  1430. public function testSubmittingRelativeFormHonoursBaseHref()
  1431. {
  1432. $this->module->amOnPage('/basehref');
  1433. $this->module->click('Relative Form');
  1434. $this->module->seeCurrentUrlEquals('/form/example5');
  1435. }
  1436. }