TestsForWeb.php 60 KB

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