PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/unit/Codeception/Module/TestsForWeb.php

https://github.com/pmcjury/Codeception
PHP | 574 lines | 477 code | 85 blank | 12 comment | 0 complexity | d2c5de135a549e642e9818b9b297143b MD5 | raw file
  1. <?php
  2. /**
  3. * Author: davert
  4. * Date: 13.01.12
  5. *
  6. * Class TestsForMink
  7. * Description:
  8. *
  9. */
  10. abstract class TestsForWeb extends \PHPUnit_Framework_TestCase
  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->amOnPage('/');
  43. $this->module->see('Welcome to test app!', 'h1');
  44. $this->module->amOnPage('/info');
  45. $this->module->see('valuable', 'p');
  46. $this->module->see('valuable','descendant-or-self::body/p');
  47. $this->module->dontSee('Welcome');
  48. $this->module->dontSee('valuable', 'h1');
  49. $this->module->dontSee('Welcome','h6');
  50. }
  51. public function testSeeInCurrentUrl()
  52. {
  53. $this->module->amOnPage('/info');
  54. $this->module->seeInCurrentUrl('/info');
  55. }
  56. public function testSeeLink()
  57. {
  58. $this->module->amOnPage('/');
  59. $this->module->seeLink('More info');
  60. $this->module->dontSeeLink('/info');
  61. $this->module->dontSeeLink('#info');
  62. $this->module->seeLink('More','/info');
  63. }
  64. public function testClick()
  65. {
  66. $this->module->amOnPage('/');
  67. $this->module->click('More info');
  68. $this->module->seeInCurrentUrl('/info');
  69. $this->module->amOnPage('/');
  70. $this->module->click('#link');
  71. $this->module->seeInCurrentUrl('/info');
  72. $this->module->amOnPage('/');
  73. $this->module->click("descendant-or-self::a[@id = 'link']");
  74. $this->module->seeInCurrentUrl('/info');
  75. }
  76. public function testClickByName()
  77. {
  78. $this->module->amOnPage('/form/button');
  79. $this->module->click("btn0");
  80. $form = data::get('form');
  81. $this->assertEquals('val', $form['text']);
  82. }
  83. public function testClickOnContext()
  84. {
  85. $this->module->amOnPage('/');
  86. $this->module->click('More info','p');
  87. $this->module->seeInCurrentUrl('/info');
  88. $this->module->amOnPage('/');
  89. $this->module->click('More info','body>p');
  90. $this->module->seeInCurrentUrl('/info');
  91. }
  92. public function testCheckboxByCss()
  93. {
  94. $this->module->amOnPage('/form/checkbox');
  95. $this->module->checkOption('#checkin');
  96. $this->module->click('Submit');
  97. $form = data::get('form');
  98. $this->assertEquals('agree', $form['terms']);
  99. }
  100. public function testCheckboxByLabel()
  101. {
  102. $this->module->amOnPage('/form/checkbox');
  103. $this->module->checkOption('I Agree');
  104. $this->module->click('Submit');
  105. $form = data::get('form');
  106. $this->assertEquals('agree', $form['terms']);
  107. }
  108. public function testSelectByCss()
  109. {
  110. $this->module->amOnPage('/form/select');
  111. $this->module->selectOption('form select[name=age]', 'adult');
  112. $this->module->click('Submit');
  113. $form = data::get('form');
  114. $this->assertEquals('adult', $form['age']);
  115. }
  116. public function testSelectByName()
  117. {
  118. $this->module->amOnPage('/form/select');
  119. $this->module->selectOption('age', 'adult');
  120. $this->module->click('Submit');
  121. $form = data::get('form');
  122. $this->assertEquals('adult', $form['age']);
  123. }
  124. public function testSelectByLabel()
  125. {
  126. $this->module->amOnPage('/form/select');
  127. $this->module->selectOption('Select your age', 'dead');
  128. $this->module->click('Submit');
  129. $form = data::get('form');
  130. $this->assertEquals('dead', $form['age']);
  131. }
  132. public function testSelectByLabelAndOptionText()
  133. {
  134. $this->module->amOnPage('/form/select');
  135. $this->module->selectOption('Select your age', '21-60');
  136. $this->module->click('Submit');
  137. $form = data::get('form');
  138. $this->assertEquals('adult', $form['age']);
  139. }
  140. public function testSeeSelectedOption()
  141. {
  142. $this->module->amOnPage('/form/select');
  143. $this->module->seeOptionIsSelected('#age', '60-100');
  144. $this->module->dontSeeOptionIsSelected('#age', '100-210');
  145. }
  146. public function testSelectMultipleOptionsByText()
  147. {
  148. $this->module->amOnPage('/form/select_multiple');
  149. $this->module->selectOption('What do you like the most?',array('Play Video Games', 'Have Sex'));
  150. $this->module->click('Submit');
  151. $form = data::get('form');
  152. $this->assertEquals(array('play','adult'), $form['like']);
  153. }
  154. public function testSelectMultipleOptionsByValue()
  155. {
  156. $this->module->amOnPage('/form/select_multiple');
  157. $this->module->selectOption('What do you like the most?',array('eat', 'adult'));
  158. $this->module->click('Submit');
  159. $form = data::get('form');
  160. $this->assertEquals(array('eat','adult'), $form['like']);
  161. }
  162. public function testHidden()
  163. {
  164. $this->module->amOnPage('/form/hidden');
  165. $this->module->click('Submit');
  166. $form = data::get('form');
  167. $this->assertEquals('kill_people', $form['action']);
  168. }
  169. public function testTextareaByCss()
  170. {
  171. $this->module->amOnPage('/form/textarea');
  172. $this->module->fillField('textarea', 'Nothing special');
  173. $this->module->click('Submit');
  174. $form = data::get('form');
  175. $this->assertEquals('Nothing special', $form['description']);
  176. }
  177. public function testTextareaByLabel()
  178. {
  179. $this->module->amOnPage('/form/textarea');
  180. $this->module->fillField('Description', 'Nothing special');
  181. $this->module->click('Submit');
  182. $form = data::get('form');
  183. $this->assertEquals('Nothing special', $form['description']);
  184. }
  185. public function testTextFieldByCss()
  186. {
  187. $this->module->amOnPage('/form/field');
  188. $this->module->fillField('#name', 'Nothing special');
  189. $this->module->click('Submit');
  190. $form = data::get('form');
  191. $this->assertEquals('Nothing special', $form['name']);
  192. }
  193. public function testTextFieldByName()
  194. {
  195. $this->module->amOnPage('/form/example1');
  196. $this->module->fillField('LoginForm[username]', 'davert');
  197. $this->module->fillField('LoginForm[password]', '123456');
  198. $this->module->click('Login');
  199. $login = data::get('form');
  200. $this->assertEquals('davert', $login['LoginForm']['username']);
  201. $this->assertEquals('123456', $login['LoginForm']['password']);
  202. }
  203. public function testTextFieldByLabel()
  204. {
  205. $this->module->amOnPage('/form/field');
  206. $this->module->fillField('Name', 'Nothing special');
  207. $this->module->click('Submit');
  208. $form = data::get('form');
  209. $this->assertEquals('Nothing special', $form['name']);
  210. }
  211. public function testFileFieldByCss()
  212. {
  213. $this->module->amOnPage('/form/file');
  214. $this->module->attachFile('#avatar', 'app/avatar.jpg');
  215. $this->module->click('Submit');
  216. $this->assertNotEmpty(data::get('files'));
  217. $files = data::get('files');
  218. $this->assertArrayHasKey('avatar', $files);
  219. }
  220. public function testFileFieldByLabel()
  221. {
  222. $this->module->amOnPage('/form/file');
  223. $this->module->attachFile('Avatar', 'app/avatar.jpg');
  224. $this->module->click('Submit');
  225. $this->assertNotEmpty(data::get('files'));
  226. }
  227. public function testSeeCheckboxIsNotChecked()
  228. {
  229. $this->module->amOnPage('/form/checkbox');
  230. $this->module->dontSeeCheckboxIsChecked('#checkin');
  231. }
  232. public function testSeeCheckboxChecked()
  233. {
  234. $this->module->amOnPage('/info');
  235. $this->module->seeCheckboxIsChecked('input[type=checkbox]');
  236. }
  237. public function testSeeWithNonLatin() {
  238. $this->module->amOnPage('/info');
  239. $this->module->see('на');
  240. }
  241. public function testSeeWithNonLatinAndSelectors() {
  242. $this->module->amOnPage('/info');
  243. $this->module->see('Текст', 'p');
  244. }
  245. public function testSeeInFieldOnInput()
  246. {
  247. $this->module->amOnPage('/form/field');
  248. $this->module->seeInField('Name','OLD_VALUE');
  249. $this->module->seeInField('input[name=name]','OLD_VALUE');
  250. $this->module->seeInField('descendant-or-self::input[@id="name"]','OLD_VALUE');
  251. }
  252. public function testSeeInFieldForEmptyInput()
  253. {
  254. $this->module->amOnPage('/form/empty');
  255. $this->module->seeInField('#empty_input','');
  256. }
  257. public function testSeeInFieldOnTextarea()
  258. {
  259. $this->module->amOnPage('/form/textarea');
  260. $this->module->seeInField('Description','sunrise');
  261. $this->module->seeInField('textarea','sunrise');
  262. $this->module->seeInField('descendant-or-self::textarea[@id="description"]','sunrise');
  263. }
  264. public function testSeeInFieldForEmptyTextarea()
  265. {
  266. $this->module->amOnPage('/form/empty');
  267. $this->module->seeInField('#empty_textarea','');
  268. }
  269. public function testDontSeeInFieldOnInput()
  270. {
  271. $this->module->amOnPage('/form/field');
  272. $this->module->dontSeeInField('Name','Davert');
  273. $this->module->dontSeeInField('input[name=name]','Davert');
  274. $this->module->dontSeeInField('descendant-or-self::input[@id="name"]','Davert');
  275. }
  276. public function testDontSeeInFieldOnTextarea()
  277. {
  278. $this->module->amOnPage('/form/textarea');
  279. $this->module->dontSeeInField('Description','sunset');
  280. $this->module->dontSeeInField('textarea','sunset');
  281. $this->module->dontSeeInField('descendant-or-self::textarea[@id="description"]','sunset');
  282. }
  283. public function testSeeInFieldWithNonLatin() {
  284. $this->module->amOnPage('/info');
  285. $this->module->seeInField('rus','Верно');
  286. }
  287. public function testApostrophesInText() {
  288. $this->module->amOnPage('/info');
  289. $this->module->see("Don't do that at home!");
  290. $this->module->see("Don't do that at home!",'h3');
  291. }
  292. public function testSign() {
  293. $this->module->amOnPage('/info');
  294. $this->module->seeLink('Sign in!');
  295. $this->module->amOnPage('/info');
  296. $this->module->click('Sign in!');
  297. }
  298. public function testGrabTextFrom() {
  299. $this->module->amOnPage('/');
  300. $result = $this->module->grabTextFrom('h1');
  301. $this->assertEquals("Welcome to test app!", $result);
  302. $result = $this->module->grabTextFrom('descendant-or-self::h1');
  303. $this->assertEquals("Welcome to test app!", $result);
  304. }
  305. public function testGrabValueFrom() {
  306. $this->module->amOnPage('/form/hidden');
  307. $result = $this->module->grabValueFrom('#action');
  308. $this->assertEquals("kill_people", $result);
  309. $result = $this->module->grabValueFrom("descendant-or-self::form/descendant::input[@name='action']");
  310. $this->assertEquals("kill_people", $result);
  311. $this->module->amOnPage('/form/textarea');
  312. }
  313. public function testGrabAttributeFrom()
  314. {
  315. $this->module->amOnPage('/search');
  316. $this->assertEquals('get', $this->module->grabAttributeFrom('form', 'method'));
  317. }
  318. public function testLinksWithSimilarNames() {
  319. $this->module->amOnPage('/');
  320. $this->module->click('Test Link');
  321. $this->module->seeInCurrentUrl('/form/file');
  322. $this->module->amOnPage('/');
  323. $this->module->click('Test');
  324. $this->module->seeInCurrentUrl('/form/hidden');
  325. }
  326. public function testLinksWithDifferentContext()
  327. {
  328. $this->module->amOnPage('/');
  329. $this->module->click('Test', '#area1');
  330. $this->module->seeInCurrentUrl('/form/file');
  331. $this->module->amOnPage('/');
  332. $this->module->click('Test', '#area2');
  333. $this->module->seeInCurrentUrl('/form/hidden');
  334. }
  335. public function testSeeElementOnPage()
  336. {
  337. $this->module->amOnPage('/form/field');
  338. $this->module->seeElement('input[name=name]');
  339. $this->module->seeElement('input', ['name' => 'name']);
  340. $this->module->seeElement('input', ['id' => 'name']);
  341. $this->module->seeElement('descendant-or-self::input[@id="name"]');
  342. $this->module->dontSeeElement('#something-beyond');
  343. $this->module->dontSeeElement('input', ['id' => 'something-beyond']);
  344. $this->module->dontSeeElement('descendant-or-self::input[@id="something-beyond"]');
  345. }
  346. // regression test. https://github.com/Codeception/Codeception/issues/587
  347. public function testSeeElementOnPageFails()
  348. {
  349. $this->setExpectedException("PHPUnit_Framework_AssertionFailedError");
  350. $this->module->amOnPage('/form/field');
  351. $this->module->dontSeeElement('input[name=name]');
  352. }
  353. public function testCookies()
  354. {
  355. $cookie_name = 'test_cookie';
  356. $cookie_value = 'this is a test';
  357. $this->module->setCookie($cookie_name, $cookie_value);
  358. $this->module->seeCookie($cookie_name);
  359. $this->module->dontSeeCookie('evil_cookie');
  360. $cookie = $this->module->grabCookie($cookie_name);
  361. $this->assertEquals($cookie_value, $cookie);
  362. $this->module->resetCookie($cookie_name);
  363. $this->module->dontSeeCookie($cookie_name);
  364. }
  365. public function testPageTitle()
  366. {
  367. $this->module->amOnPage('/');
  368. $this->module->seeInTitle('TestEd Beta 2.0');
  369. $this->module->dontSeeInTitle('Welcome to test app');
  370. $this->module->amOnPage('/info');
  371. $this->module->dontSeeInTitle('TestEd Beta 2.0');
  372. }
  373. public function testSeeFails()
  374. {
  375. $this->shouldFail();
  376. $this->module->amOnPage('/');
  377. $this->module->see('Text not here');
  378. }
  379. public function testSeeInsideFails()
  380. {
  381. $this->shouldFail();
  382. $this->module->amOnPage('/info');
  383. $this->module->see('woups','p');
  384. }
  385. public function testDontSeeInInsideFails()
  386. {
  387. $this->shouldFail();
  388. $this->module->amOnPage('/info');
  389. $this->module->dontSee('interesting','p');
  390. }
  391. public function testSeeElementFails()
  392. {
  393. $this->shouldFail();
  394. $this->module->amOnPage('/info');
  395. $this->module->seeElement('.alert');
  396. }
  397. public function testDontSeeElementFails()
  398. {
  399. $this->shouldFail();
  400. $this->module->amOnPage('/info');
  401. $this->module->dontSeeElement('#back');
  402. }
  403. public function testSeeInFieldFail()
  404. {
  405. $this->shouldFail();
  406. $this->module->amOnPage('/form/empty');
  407. $this->module->seeInField('#empty_textarea','xxx');
  408. }
  409. public function testSeeInFieldOnTextareaFails()
  410. {
  411. $this->shouldFail();
  412. $this->module->amOnPage('/form/textarea');
  413. $this->module->dontSeeInField('Description','sunrise');
  414. }
  415. public function testSeeCheckboxIsNotCheckedFails() {
  416. $this->shouldFail();
  417. $this->module->amOnPage('/form/complex');
  418. $this->module->dontSeeCheckboxIsChecked('#checkin');
  419. }
  420. public function testSeeCheckboxCheckedFails() {
  421. $this->shouldFail();
  422. $this->module->amOnPage('/form/checkbox');
  423. $this->module->seeCheckboxIsChecked('#checkin');
  424. }
  425. public function testDontSeeElementOnPageFails()
  426. {
  427. $this->shouldFail();
  428. $this->module->amOnPage('/form/field');
  429. $this->module->dontSeeElement('descendant-or-self::input[@id="name"]');
  430. }
  431. public function testStrictLocators()
  432. {
  433. $this->module->amOnPage('/login');
  434. $this->module->seeElement(['id' => 'submit-label']);
  435. $this->module->seeElement(['name' => 'password']);
  436. $this->module->seeElement(['class' => 'optional']);
  437. $this->module->seeElement(['css' => 'form.global_form_box']);
  438. $this->module->seeElement(['xpath' => \Codeception\Util\Locator::tabIndex(4)]);
  439. $this->module->fillField(['name' => 'password'], '123456');
  440. $this->module->amOnPage('/form/select');
  441. $this->module->selectOption(['name' => 'age'], 'child');
  442. $this->module->amOnPage('/form/checkbox');
  443. $this->module->checkOption(['name' => 'terms']);
  444. $this->module->amOnPage('/');
  445. $this->module->seeElement(['link' => 'Test']);
  446. $this->module->click(['link' => 'Test']);
  447. $this->module->seeCurrentUrlEquals('/form/hidden');
  448. }
  449. public function testFailStrictLocators()
  450. {
  451. $this->shouldFail();
  452. $this->module->amOnPage('/form/checkbox');
  453. $this->module->checkOption(['name' => 'age']);
  454. }
  455. public function testExample1()
  456. {
  457. $this->module->amOnPage('/form/example1');
  458. $this->module->see('Login','button');
  459. $this->module->fillField('#LoginForm_username', 'davert');
  460. $this->module->fillField('#LoginForm_password', '123456');
  461. $this->module->checkOption('#LoginForm_rememberMe');
  462. $this->module->click('Login');
  463. $login = data::get('form');
  464. $this->assertEquals('davert', $login['LoginForm']['username']);
  465. $this->assertEquals('123456', $login['LoginForm']['password']);
  466. $this->assertNotEmpty($login['LoginForm']['rememberMe']);
  467. }
  468. public function testExample2()
  469. {
  470. $this->module->amOnPage('/form/example2');
  471. $this->module->fillField('input[name=username]', 'davert');
  472. $this->module->fillField('input[name=password]', '123456');
  473. $this->module->click('Log on');
  474. $login = data::get('form');
  475. $this->assertEquals('davert', $login['username']);
  476. $this->assertEquals('123456', $login['password']);
  477. $this->assertEquals('login', $login['action']);
  478. }
  479. public function testAmpersand()
  480. {
  481. $this->module->amOnPage('/info');
  482. $this->module->see('Kill & Destroy');
  483. $this->module->see('Kill & Destroy','div');
  484. }
  485. protected function shouldFail()
  486. {
  487. $this->setExpectedException('PHPUnit_Framework_AssertionFailedError');
  488. }
  489. }