WebDriverTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. <?php
  2. use Codeception\Step;
  3. use Codeception\Util\Stub;
  4. use Facebook\WebDriver\Remote\DesiredCapabilities;
  5. use Facebook\WebDriver\Remote\RemoteWebDriver;
  6. use Facebook\WebDriver\WebDriverBy;
  7. use Facebook\WebDriver\WebDriverKeys;
  8. require_once codecept_data_dir() . 'app/data.php';
  9. require_once __DIR__ . '/../unit/Codeception/Module/TestsForBrowsers.php';
  10. class WebDriverTest extends TestsForBrowsers
  11. {
  12. /**
  13. * @var \Codeception\Module\WebDriver
  14. */
  15. protected $module;
  16. /**
  17. * @var RemoteWebDriver
  18. */
  19. protected $webDriver;
  20. const MODULE_CLASS = 'Codeception\Module\WebDriver';
  21. const WEBDRIVER_CLASS = 'Facebook\WebDriver\Remote\RemoteWebDriver';
  22. public function _before()
  23. {
  24. $this->module = $this->getModule('WebDriver');
  25. $this->webDriver = &$this->getModule('WebDriver')->webDriver;
  26. }
  27. public function _after()
  28. {
  29. data::clean();
  30. }
  31. public function testClickEventOnCheckbox()
  32. {
  33. $this->module->amOnPage('/form/checkbox');
  34. $this->module->uncheckOption('#checkin');
  35. $this->module->dontSee('ticked', '#notice');
  36. $this->module->checkOption('#checkin');
  37. $this->module->see('ticked', '#notice');
  38. }
  39. public function testAcceptPopup()
  40. {
  41. $this->notForPhantomJS();
  42. $this->module->amOnPage('/form/popup');
  43. $this->module->click('Confirm');
  44. $this->module->acceptPopup();
  45. $this->module->see('Yes', '#result');
  46. }
  47. public function testCancelPopup()
  48. {
  49. $this->notForPhantomJS();
  50. $this->module->amOnPage('/form/popup');
  51. $this->module->click('Confirm');
  52. $this->module->cancelPopup();
  53. $this->module->see('No', '#result');
  54. }
  55. public function testSelectByCss()
  56. {
  57. $this->module->amOnPage('/form/select');
  58. $this->module->selectOption('form select[name=age]', '21-60');
  59. $this->module->click('Submit');
  60. $form = data::get('form');
  61. $this->assertEquals('adult', $form['age']);
  62. }
  63. public function testSelectInvalidOptionForSecondSelectFails()
  64. {
  65. $this->shouldFail();
  66. $this->module->amOnPage('/form/select_second');
  67. $this->module->selectOption('#select2', 'Value2');
  68. }
  69. public function testSeeInPopup()
  70. {
  71. $this->notForPhantomJS();
  72. $this->module->amOnPage('/form/popup');
  73. $this->module->click('Alert');
  74. $this->module->seeInPopup('Really?');
  75. $this->module->cancelPopup();
  76. }
  77. public function testFailedSeeInPopup()
  78. {
  79. $this->notForPhantomJS();
  80. $this->setExpectedException(
  81. 'PHPUnit_Framework_AssertionFailedError',
  82. 'Failed asserting that \'Really?\' contains "Different text"'
  83. );
  84. $this->module->amOnPage('/form/popup');
  85. $this->module->click('Alert');
  86. $this->module->seeInPopup('Different text');
  87. $this->module->cancelPopup();
  88. }
  89. public function testScreenshot()
  90. {
  91. $this->module->amOnPage('/');
  92. @unlink(\Codeception\Configuration::outputDir().'testshot.png');
  93. $testName="debugTest";
  94. $this->module->makeScreenshot($testName);
  95. $this->assertFileExists(\Codeception\Configuration::outputDir().'debug/'.$testName.'.png');
  96. @unlink(\Codeception\Configuration::outputDir().'debug/'.$testName.'.png');
  97. $this->module->_saveScreenshot(\Codeception\Configuration::outputDir().'testshot.png');
  98. $this->assertFileExists(\Codeception\Configuration::outputDir().'testshot.png');
  99. @unlink(\Codeception\Configuration::outputDir().'testshot.png');
  100. }
  101. public function testSubmitForm()
  102. {
  103. $this->module->amOnPage('/form/complex');
  104. $this->module->submitForm('form', [
  105. 'name' => 'Davert',
  106. 'age' => 'child',
  107. 'terms' => 'agree',
  108. 'description' => 'My Bio'
  109. ]);
  110. $form = data::get('form');
  111. $this->assertEquals('Davert', $form['name']);
  112. $this->assertEquals('kill_all', $form['action']);
  113. $this->assertEquals('My Bio', $form['description']);
  114. $this->assertEquals('agree', $form['terms']);
  115. $this->assertEquals('child', $form['age']);
  116. }
  117. public function testSubmitFormWithNumbers()
  118. {
  119. $this->module->amOnPage('/form/complex');
  120. $this->module->submitForm('form', [
  121. 'name' => 'Davert',
  122. 'age' => 'child',
  123. 'terms' => 'agree',
  124. 'description' => 10
  125. ]);
  126. $form = data::get('form');
  127. $this->assertEquals('Davert', $form['name']);
  128. $this->assertEquals('kill_all', $form['action']);
  129. $this->assertEquals('10', $form['description']);
  130. $this->assertEquals('agree', $form['terms']);
  131. $this->assertEquals('child', $form['age']);
  132. }
  133. /**
  134. * @dataProvider strictSelectorProvider
  135. */
  136. public function testSubmitFormWithButtonAsStrictSelector(array $selector)
  137. {
  138. $this->module->amOnPage('/form/strict_selectors');
  139. $this->module->submitForm('form', [
  140. 'name' => 'Davert',
  141. 'age' => 'child',
  142. 'terms' => 'agree',
  143. 'description' => 'My Bio'
  144. ], $selector);
  145. $form = data::get('form');
  146. $this->assertEquals('Davert', $form['name']);
  147. $this->assertEquals('kill_all', $form['action']);
  148. $this->assertEquals('My Bio', $form['description']);
  149. $this->assertEquals('agree', $form['terms']);
  150. $this->assertEquals('child', $form['age']);
  151. }
  152. public function strictSelectorProvider()
  153. {
  154. return [
  155. 'by id' => [['id' => 'submit_button']],
  156. 'by name' => [['name' => 'submit_button_name']],
  157. 'by css' => [['css' => 'form #submit_button']],
  158. 'by xpath' => [['xpath' => '//*[@id="submit_button"]']],
  159. 'by link' => [['link' => 'Submit']],
  160. 'by class' => [['class' => 'button']],
  161. ];
  162. }
  163. /**
  164. * @dataProvider webDriverByProvider
  165. */
  166. public function testSubmitFormWithButtonAsWebDriverBy(WebDriverBy $selector)
  167. {
  168. $this->module->amOnPage('/form/strict_selectors');
  169. $this->module->submitForm('form', [
  170. 'name' => 'Davert',
  171. 'age' => 'child',
  172. 'terms' => 'agree',
  173. 'description' => 'My Bio'
  174. ], $selector);
  175. $form = data::get('form');
  176. $this->assertEquals('Davert', $form['name']);
  177. $this->assertEquals('kill_all', $form['action']);
  178. $this->assertEquals('My Bio', $form['description']);
  179. $this->assertEquals('agree', $form['terms']);
  180. $this->assertEquals('child', $form['age']);
  181. }
  182. public function webDriverByProvider()
  183. {
  184. return [
  185. 'by id' => [WebDriverBy::id('submit_button')],
  186. 'by name' => [WebDriverBy::name('submit_button_name')],
  187. 'by css selector' => [WebDriverBy::cssSelector('form #submit_button')],
  188. 'by xpath' => [WebDriverBy::xpath('//*[@id="submit_button"]')],
  189. 'by link text' => [WebDriverBy::linkText('Submit')],
  190. 'by class name' => [WebDriverBy::className('button')],
  191. ];
  192. }
  193. public function testRadioButtonByValue()
  194. {
  195. $this->module->amOnPage('/form/radio');
  196. $this->module->selectOption('form', 'disagree');
  197. $this->module->click('Submit');
  198. $form = data::get('form');
  199. $this->assertEquals('disagree', $form['terms']);
  200. }
  201. public function testRadioButtonByLabelOnContext()
  202. {
  203. $this->module->amOnPage('/form/radio');
  204. $this->module->selectOption('form input', 'Get Off');
  205. $this->module->seeOptionIsSelected('form input', 'disagree');
  206. $this->module->dontSeeOptionIsSelected('form input', 'agree');
  207. $this->module->click('Submit');
  208. $form = data::get('form');
  209. $this->assertEquals('disagree', $form['terms']);
  210. }
  211. public function testRadioButtonByLabel()
  212. {
  213. $this->module->amOnPage('/form/radio');
  214. $this->module->checkOption('Get Off');
  215. $this->module->click('Submit');
  216. $form = data::get('form');
  217. $this->assertEquals('disagree', $form['terms']);
  218. }
  219. public function testRawSelenium()
  220. {
  221. $this->module->amOnPage('/');
  222. $this->module->executeInSelenium(function ($webdriver) {
  223. $webdriver->findElement(WebDriverBy::id('link'))->click();
  224. });
  225. $this->module->seeCurrentUrlEquals('/info');
  226. }
  227. public function testKeys()
  228. {
  229. $this->module->amOnPage('/form/field');
  230. $this->module->pressKey('#name', ['ctrl', 'a'], WebDriverKeys::DELETE);
  231. $this->module->pressKey('#name', 'test', ['shift', '111']);
  232. $this->module->pressKey('#name', '1');
  233. $this->module->seeInField('#name', 'test!!!1');
  234. }
  235. public function testWait()
  236. {
  237. $this->module->amOnPage('/');
  238. $time = time();
  239. $this->module->wait(3);
  240. $this->assertGreaterThanOrEqual($time+3, time());
  241. }
  242. public function testSelectInvalidOptionFails()
  243. {
  244. $this->shouldFail();
  245. $this->module->amOnPage('/form/select');
  246. $this->module->selectOption('#age', '13-22');
  247. }
  248. public function testAppendFieldSelect()
  249. {
  250. $this->module->amOnPage('/form/select_multiple');
  251. $this->module->selectOption('form #like', 'eat');
  252. $this->module->appendField('form #like', 'code');
  253. $this->module->click('Submit');
  254. $form = data::get('form');
  255. $this->assertEmpty(array_diff($form['like'], ["eat", "code"]));
  256. }
  257. public function testAppendFieldSelectFails()
  258. {
  259. $this->shouldFail();
  260. $this->module->amOnPage('/form/select_multiple');
  261. $this->module->appendField('form #like', 'code123');
  262. }
  263. public function testAppendFieldTextarea()
  264. {
  265. $this->module->amOnPage('/form/textarea');
  266. $this->module->fillField('form #description', 'eat');
  267. $this->module->appendField('form #description', ' code');
  268. $this->module->click('Submit');
  269. $form = data::get('form');
  270. $this->assertEquals('eat code', $form['description']);
  271. }
  272. public function testAppendFieldTextareaFails()
  273. {
  274. $this->shouldFail();
  275. $this->module->amOnPage('/form/textarea');
  276. $this->module->appendField('form #description123', ' code');
  277. }
  278. public function testAppendFieldText()
  279. {
  280. $this->module->amOnPage('/form/field');
  281. $this->module->appendField('form #name', ' code');
  282. $this->module->click('Submit');
  283. $form = data::get('form');
  284. $this->assertEquals('OLD_VALUE code', $form['name']);
  285. }
  286. public function testAppendFieldTextFails()
  287. {
  288. $this->shouldFail();
  289. $this->module->amOnPage('/form/field');
  290. $this->module->appendField('form #name123', ' code');
  291. }
  292. public function testAppendFieldCheckboxByValue()
  293. {
  294. $this->module->amOnPage('/form/checkbox');
  295. $this->module->appendField('form input[name=terms]', 'agree');
  296. $this->module->click('Submit');
  297. $form = data::get('form');
  298. $this->assertEquals('agree', $form['terms']);
  299. }
  300. public function testAppendFieldCheckboxByValueFails()
  301. {
  302. $this->shouldFail();
  303. $this->module->amOnPage('/form/checkbox');
  304. $this->module->appendField('form input[name=terms]', 'agree123');
  305. }
  306. public function testAppendFieldCheckboxByLabel()
  307. {
  308. $this->module->amOnPage('/form/checkbox');
  309. $this->module->appendField('form input[name=terms]', 'I Agree');
  310. $this->module->click('Submit');
  311. $form = data::get('form');
  312. $this->assertEquals('agree', $form['terms']);
  313. }
  314. public function testAppendFieldCheckboxByLabelFails()
  315. {
  316. $this->shouldFail();
  317. $this->module->amOnPage('/form/checkbox');
  318. $this->module->appendField('form input[name=terms]', 'I Agree123');
  319. }
  320. public function testAppendFieldRadioButtonByValue()
  321. {
  322. $this->module->amOnPage('/form/radio');
  323. $this->module->appendField('form input[name=terms]', 'disagree');
  324. $this->module->click('Submit');
  325. $form = data::get('form');
  326. $this->assertEquals('disagree', $form['terms']);
  327. }
  328. public function testAppendFieldRadioButtonByValueFails()
  329. {
  330. $this->shouldFail();
  331. $this->module->amOnPage('/form/radio');
  332. $this->module->appendField('form input[name=terms]', 'disagree123');
  333. }
  334. public function testAppendFieldRadioButtonByLabel()
  335. {
  336. $this->module->amOnPage('/form/radio');
  337. $this->module->appendField('form input[name=terms]', 'Get Off');
  338. $this->module->click('Submit');
  339. $form = data::get('form');
  340. $this->assertEquals('disagree', $form['terms']);
  341. }
  342. public function testAppendFieldRadioButtonByLabelFails()
  343. {
  344. $this->shouldFail();
  345. $this->module->amOnPage('/form/radio');
  346. $this->module->appendField('form input[name=terms]', 'Get Off123');
  347. }
  348. public function testPauseExecution()
  349. {
  350. $this->module->amOnPage('/');
  351. $this->module->pauseExecution();
  352. }
  353. // Issue https://github.com/Codeception/Codeception/pull/875
  354. public function testFillPasswordOnFormSubmit()
  355. {
  356. $this->module->amOnPage('/form/complex');
  357. $this->module->submitForm('form', [
  358. 'password' => '123456'
  359. ]);
  360. $form = data::get('form');
  361. $this->assertEquals('123456', $form['password']);
  362. }
  363. public function testEmptyFormSubmit()
  364. {
  365. $this->shouldFail();
  366. $this->module->amOnPage('/form/complex');
  367. $this->module->submitForm('form111', []);
  368. }
  369. public function testWebDriverByLocators()
  370. {
  371. $this->module->amOnPage('/login');
  372. $this->module->seeElement(WebDriverBy::id('submit-label'));
  373. $this->module->seeElement(WebDriverBy::name('password'));
  374. $this->module->seeElement(WebDriverBy::className('optional'));
  375. $this->module->seeElement(WebDriverBy::cssSelector('form.global_form_box'));
  376. $this->module->seeElement(WebDriverBy::xpath(\Codeception\Util\Locator::tabIndex(4)));
  377. $this->module->fillField(WebDriverBy::name('password'), '123456');
  378. $this->module->amOnPage('/form/select');
  379. $this->module->selectOption(WebDriverBy::name('age'), 'child');
  380. $this->module->amOnPage('/form/checkbox');
  381. $this->module->checkOption(WebDriverBy::name('terms'));
  382. $this->module->amOnPage('/');
  383. $this->module->seeElement(WebDriverBy::linkText('Test'));
  384. $this->module->click(WebDriverBy::linkText('Test'));
  385. $this->module->seeCurrentUrlEquals('/form/hidden');
  386. }
  387. public function testSeeVisible()
  388. {
  389. $this->module->amOnPage('/info');
  390. $this->module->dontSee('Invisible text');
  391. $this->module->dontSee('Invisible', '.hidden');
  392. $this->module->seeInPageSource('Invisible text');
  393. }
  394. public function testSeeInvisible()
  395. {
  396. $this->shouldFail();
  397. $this->module->amOnPage('/info');
  398. $this->module->see('Invisible text');
  399. }
  400. public function testFailWebDriverByLocator()
  401. {
  402. $this->shouldFail();
  403. $this->module->amOnPage('/form/checkbox');
  404. $this->module->checkOption(WebDriverBy::name('age'));
  405. }
  406. // fails in PhpBrowser :(
  407. public function testSubmitUnchecked()
  408. {
  409. $this->module->amOnPage('/form/unchecked');
  410. $this->module->seeCheckboxIsChecked('#checkbox');
  411. $this->module->uncheckOption('#checkbox');
  412. $this->module->click('#submit');
  413. ;
  414. $this->module->see('0', '#notice');
  415. }
  416. public function testCreateCeptScreenshotFail()
  417. {
  418. $fakeWd = Stub::make('\Facebook\WebDriver\Remote\RemoteWebDriver', [
  419. 'takeScreenshot' => Stub::once(function () {
  420. }),
  421. 'getPageSource' => Stub::once(function () {
  422. }),
  423. 'manage' => Stub::make('\Facebook\WebDriver\WebDriverOptions', [
  424. 'getAvailableLogTypes' => Stub::atLeastOnce(function () {
  425. return [];
  426. }),
  427. ]),
  428. ]);
  429. $module = Stub::make(self::MODULE_CLASS, ['webDriver' => $fakeWd]);
  430. $cept = (new \Codeception\Test\Cept('loginCept', 'loginCept.php'));
  431. $module->_failed($cept, new PHPUnit_Framework_AssertionFailedError());
  432. }
  433. public function testCreateCestScreenshotOnFail()
  434. {
  435. $fakeWd = Stub::make(self::WEBDRIVER_CLASS, [
  436. 'takeScreenshot' => Stub::once(function ($filename) {
  437. PHPUnit_Framework_Assert::assertEquals(codecept_log_dir('stdClass.login.fail.png'), $filename);
  438. }),
  439. 'getPageSource' => Stub::once(function () {
  440. }),
  441. 'manage' => Stub::make('\Facebook\WebDriver\WebDriverOptions', [
  442. 'getAvailableLogTypes' => Stub::atLeastOnce(function () {
  443. return [];
  444. }),
  445. ]),
  446. ]);
  447. $module = Stub::make(self::MODULE_CLASS, ['webDriver' => $fakeWd]);
  448. $cest = new \Codeception\Test\Cest(new stdClass(), 'login', 'someCest.php');
  449. $module->_failed($cest, new PHPUnit_Framework_AssertionFailedError());
  450. }
  451. public function testCreateTestScreenshotOnFail()
  452. {
  453. $test = Stub::make('\Codeception\TestCase\Test', ['getName' => 'testLogin']);
  454. $fakeWd = Stub::make(self::WEBDRIVER_CLASS, [
  455. 'takeScreenshot' => Stub::once(function ($filename) use ($test) {
  456. PHPUnit_Framework_Assert::assertEquals(
  457. codecept_log_dir(get_class($test).'.testLogin.fail.png'),
  458. $filename
  459. );
  460. }),
  461. 'getPageSource' => Stub::once(function () {
  462. }),
  463. 'manage' => Stub::make('\Facebook\WebDriver\WebDriverOptions', [
  464. 'getAvailableLogTypes' => Stub::atLeastOnce(function () {
  465. return [];
  466. }),
  467. ]),
  468. ]);
  469. $module = Stub::make(self::MODULE_CLASS, ['webDriver' => $fakeWd]);
  470. $module->_failed($test, new PHPUnit_Framework_AssertionFailedError());
  471. }
  472. public function testWebDriverWaits()
  473. {
  474. $fakeWd = Stub::make(self::WEBDRIVER_CLASS, ['wait' => Stub::exactly(12, function () {
  475. return new \Codeception\Util\Maybe();
  476. })]);
  477. $module = Stub::make(self::MODULE_CLASS, ['webDriver' => $fakeWd]);
  478. $module->waitForElement(WebDriverBy::partialLinkText('yeah'));
  479. $module->waitForElement(['id' => 'user']);
  480. $module->waitForElement(['css' => '.user']);
  481. $module->waitForElement('//xpath');
  482. $module->waitForElementVisible(WebDriverBy::partialLinkText('yeah'));
  483. $module->waitForElementVisible(['id' => 'user']);
  484. $module->waitForElementVisible(['css' => '.user']);
  485. $module->waitForElementVisible('//xpath');
  486. $module->waitForElementNotVisible(WebDriverBy::partialLinkText('yeah'));
  487. $module->waitForElementNotVisible(['id' => 'user']);
  488. $module->waitForElementNotVisible(['css' => '.user']);
  489. $module->waitForElementNotVisible('//xpath');
  490. }
  491. public function testWaitForElement()
  492. {
  493. $this->module->amOnPage('/form/timeout');
  494. $this->module->waitForElement('#btn');
  495. $this->module->click('Click');
  496. $this->module->see('Hello');
  497. }
  498. public function testImplicitWait()
  499. {
  500. $this->module->_reconfigure(['wait' => 5]);
  501. $this->module->amOnPage('/form/timeout');
  502. $this->module->click('#btn');
  503. $this->module->see('Hello');
  504. }
  505. public function testBug1467()
  506. {
  507. $this->module->amOnPage('/form/bug1467');
  508. $this->module->selectOption('form[name=form2] input[name=first_test_radio]', 'Yes');
  509. $this->module->selectOption('form[name=form2] input[name=second_test_radio]', 'No');
  510. $this->module->seeOptionIsSelected('form[name=form2] input[name=first_test_radio]', 'Yes');
  511. $this->module->seeOptionIsSelected('form[name=form2] input[name=second_test_radio]', 'No');
  512. // shouldn't have touched form1 at all
  513. $this->module->dontSeeOptionIsSelected('form[name=form1] input[name=first_test_radio]', 'No');
  514. $this->module->dontSeeOptionIsSelected('form[name=form1] input[name=first_test_radio]', 'Yes');
  515. $this->module->dontSeeOptionIsSelected('form[name=form1] input[name=second_test_radio]', 'No');
  516. $this->module->dontSeeOptionIsSelected('form[name=form1] input[name=second_test_radio]', 'Yes');
  517. }
  518. /**
  519. * @Issue 1598
  520. */
  521. public function testWaitForTextBug1598()
  522. {
  523. $this->module->amOnPage('/form/bug1598');
  524. $this->module->waitForText('12,345', 10, '#field');
  525. }
  526. public function testSeeElementMalformedWdLocator()
  527. {
  528. $this->setExpectedException('Codeception\Exception\MalformedLocatorException');
  529. $this->module->amOnPage('/');
  530. $this->module->seeElement(WebDriverBy::xpath('H---EY!'));
  531. }
  532. public function testBug1637()
  533. {
  534. $this->module->amOnPage('/form/bug1637');
  535. // confirm that options outside a form are still selectable
  536. $this->module->selectOption('input[name=first_test_radio]', 'Yes');
  537. // confirm that it did what we expected and did not do anything else
  538. $this->module->seeOptionIsSelected('input[name=first_test_radio]', 'Yes');
  539. $this->module->dontSeeOptionIsSelected('input[name=first_test_radio]', 'No');
  540. }
  541. public function testBug2046()
  542. {
  543. $this->module->webDriver = null;
  544. $this->module->_saveScreenshot(\Codeception\Configuration::outputDir().'testshot.png');
  545. }
  546. public function testSessionSnapshots()
  547. {
  548. $this->notForPhantomJS();
  549. $this->module->amOnPage('/');
  550. $this->module->setCookie('PHPSESSID', '123456', ['path' => '/']);
  551. $this->module->saveSessionSnapshot('login');
  552. $this->module->seeCookie('PHPSESSID');
  553. $this->webDriver->manage()->deleteAllCookies();
  554. $this->module->dontSeeCookie('PHPSESSID');
  555. $this->module->loadSessionSnapshot('login');
  556. $this->module->seeCookie('PHPSESSID');
  557. }
  558. public function testSaveSessionSnapshotsExcludeInvalidCookieDomains()
  559. {
  560. $this->notForPhantomJS();
  561. $fakeWdOptions = Stub::make('\Facebook\WebDriver\WebDriverOptions', [
  562. 'getCookies' => Stub::atLeastOnce(function () {
  563. return [
  564. [
  565. 'name' => 'PHPSESSID',
  566. 'value' => '123456',
  567. 'path' => '/',
  568. ],
  569. [
  570. 'name' => '3rdParty',
  571. 'value' => '_value_',
  572. 'path' => '/',
  573. 'domain' => '.3rd-party.net',
  574. ]
  575. ];
  576. }),
  577. ]);
  578. $fakeWd = Stub::make(self::WEBDRIVER_CLASS, [
  579. 'manage' => Stub::atLeastOnce(function () use ($fakeWdOptions) {
  580. return $fakeWdOptions;
  581. }),
  582. ]);
  583. // Mock the WebDriverOptions::getCookies() method on the first call to introduce a 3rd-party cookie
  584. // which has to be ignored when saving a snapshot.
  585. $originalWebDriver = $this->module->webDriver;
  586. $this->module->webDriver = $fakeWd;
  587. $this->module->seeCookie('PHPSESSID');
  588. $this->module->seeCookie('3rdParty');
  589. $this->module->saveSessionSnapshot('login');
  590. // Restore the original WebDriver
  591. $this->module->webDriver = $originalWebDriver;
  592. $this->webDriver->manage()->deleteAllCookies();
  593. $this->module->dontSeeCookie('PHPSESSID');
  594. $this->module->dontSeeCookie('3rdParty');
  595. $this->module->amOnPage('/');
  596. $this->module->loadSessionSnapshot('login');
  597. $this->module->seeCookie('PHPSESSID');
  598. $this->module->dontSeeCookie('3rdParty');
  599. }
  600. public function testSeeInFieldTextarea()
  601. {
  602. $this->module->amOnPage('/form/textarea');
  603. //make sure we see 'sunrise' which is the default text in the textarea
  604. $this->module->seeInField('#description', 'sunrise');
  605. if ($this->notForSelenium()) {
  606. $this->module->seeInField('#whitespaces', ' no_whitespaces ');
  607. }
  608. $this->module->seeInField('#whitespaces', 'no_whitespaces');
  609. //fill in some new text and see if we can see it
  610. $textarea_value = 'test string';
  611. $this->module->fillField('#description', $textarea_value);
  612. $this->module->seeInField('#description', $textarea_value);
  613. }
  614. public function testSeeInFieldSelect()
  615. {
  616. $this->module->amOnPage('/form/select_second');
  617. if ($this->notForSelenium()) {
  618. $this->module->seeInField('#select2', ' no_whitespaces ');
  619. }
  620. $this->module->seeInField('#select2', 'no_whitespaces');
  621. // select new option and check it
  622. $option_value = 'select2_value1';
  623. $this->module->selectOption('#select2', $option_value);
  624. $this->module->seeInField('#select2', $option_value);
  625. }
  626. public function testAppendFieldDiv()
  627. {
  628. $this->notForPhantomJS();
  629. $this->module->amOnPage('/form/div_content_editable');
  630. //make sure we see 'sunrise' which is the default text in the textarea
  631. $this->module->see('sunrise', '#description');
  632. //fill in some new text and see if we can see it
  633. $textarea_value = 'moonrise';
  634. $this->module->appendField('#description', $textarea_value);
  635. $this->module->see('sunrise' . $textarea_value, '#description');
  636. }
  637. public function testOpenPageException()
  638. {
  639. if (!$this->module->_getConfig('restart')) {
  640. $this->markTestSkipped('works only on restarts');
  641. }
  642. parent::testOpenPageException();
  643. }
  644. public function testCookies()
  645. {
  646. $this->notForPhantomJS();
  647. parent::testCookies();
  648. }
  649. public function testSendingCookies()
  650. {
  651. $this->notForPhantomJS();
  652. parent::testSendingCookies();
  653. }
  654. public function testCookiesWithPath()
  655. {
  656. $this->notForPhantomJS();
  657. parent::testCookiesWithPath();
  658. }
  659. protected function notForPhantomJS()
  660. {
  661. if ($this->module->_getConfig('browser') == 'phantomjs') {
  662. $this->markTestSkipped('does not work for phantomjs');
  663. }
  664. }
  665. protected function notForSelenium()
  666. {
  667. if ($this->module->_getConfig('browser') != 'phantom') {
  668. $this->markTestSkipped('does not work for selenium');
  669. }
  670. }
  671. public function testScrollTo()
  672. {
  673. $this->module->amOnPage('/form/example18');
  674. $this->module->scrollTo('#clickme');
  675. $this->module->click('Submit');
  676. $this->module->see('Welcome to test app!');
  677. }
  678. /**
  679. * @Issue 2921
  680. */
  681. public function testSeeInFieldForTextarea()
  682. {
  683. $this->module->amOnPage('/form/bug2921');
  684. $this->module->seeInField('foo', 'bar baz');
  685. }
  686. public function testClickHashLink()
  687. {
  688. $this->module->amOnPage('/form/anchor');
  689. $this->module->click('Hash Link');
  690. $this->module->seeCurrentUrlEquals('/form/anchor#b');
  691. }
  692. /**
  693. * @Issue 3865
  694. */
  695. public function testClickNumericLink()
  696. {
  697. $this->module->amOnPage('/form/bug3865');
  698. $this->module->click('222');
  699. $this->module->see('Welcome to test app');
  700. }
  701. public function testClickHashButton()
  702. {
  703. $this->module->amOnPage('/form/anchor');
  704. $this->module->click('Hash Button');
  705. $this->module->seeCurrentUrlEquals('/form/anchor#c');
  706. }
  707. public function testSubmitHashForm()
  708. {
  709. $this->module->amOnPage('/form/anchor');
  710. $this->module->click('Hash Form');
  711. $this->module->seeCurrentUrlEquals('/form/anchor#a');
  712. }
  713. public function testJSErrorLoggingPositive()
  714. {
  715. // arrange
  716. $this->module->_setConfig(['log_js_errors' => true]);
  717. $cept = new \Codeception\Test\Cept('foo', 'bar');
  718. // act
  719. $this->module->amOnPage('/jserroronload');
  720. $this->module->_failed($cept, 'anyFailMessage');
  721. // assert
  722. /* @var $steps Step[] */
  723. $steps = $cept->getScenario()->getSteps();
  724. $this->assertGreaterThan(0, count($steps));
  725. $lastStep = end($steps);
  726. $this->assertContains(
  727. "TypeError",
  728. $lastStep->getHtml()
  729. );
  730. }
  731. public function testJSErrorLoggingNegative()
  732. {
  733. // arrange
  734. $this->module->_setConfig(['log_js_errors' => false]);
  735. $cept = new \Codeception\Test\Cept('foo', 'bar');
  736. // act
  737. $this->module->amOnPage('/jserroronload');
  738. $this->module->_failed($cept, 'anyFailMessage');
  739. // assert
  740. /* @var $steps Step[] */
  741. $steps = $cept->getScenario()->getSteps();
  742. $this->assertEquals(0, count($steps));
  743. }
  744. public function testMoveMouseOver()
  745. {
  746. $this->module->amOnPage('/form/click');
  747. $this->module->moveMouseOver(null, 123, 88);
  748. $this->module->clickWithLeftButton(null, 0, 0);
  749. $this->module->see('click, offsetX: 123 - offsetY: 88');
  750. $this->module->moveMouseOver(null, 10, 10);
  751. $this->module->clickWithLeftButton(null, 0, 0);
  752. $this->module->see('click, offsetX: 133 - offsetY: 98');
  753. $this->module->moveMouseOver('#element2');
  754. $this->module->clickWithLeftButton(null, 0, 0);
  755. $this->module->see('click, offsetX: 58 - offsetY: 158');
  756. $this->module->moveMouseOver('#element2', 0, 0);
  757. $this->module->clickWithLeftButton(null, 0, 0);
  758. $this->module->see('click, offsetX: 8 - offsetY: 108');
  759. }
  760. public function testLeftClick()
  761. {
  762. $this->module->amOnPage('/form/click');
  763. $this->module->clickWithLeftButton(null, 123, 88);
  764. $this->module->see('click, offsetX: 123 - offsetY: 88');
  765. $this->module->clickWithLeftButton('body');
  766. $this->module->see('click, offsetX: 600 - offsetY: 384');
  767. $this->module->clickWithLeftButton('body', 50, 75);
  768. $this->module->see('click, offsetX: 58 - offsetY: 83');
  769. $this->module->clickWithLeftButton('body div');
  770. $this->module->see('click, offsetX: 58 - offsetY: 58');
  771. $this->module->clickWithLeftButton('#element2', 70, 75);
  772. $this->module->see('click, offsetX: 78 - offsetY: 183');
  773. }
  774. public function testRightClick()
  775. {
  776. // actually not supported in phantomjs see https://github.com/ariya/phantomjs/issues/14005
  777. $this->notForPhantomJS();
  778. $this->module->amOnPage('/form/click');
  779. $this->module->clickWithRightButton(null, 123, 88);
  780. $this->module->see('context, offsetX: 123 - offsetY: 88');
  781. $this->module->clickWithRightButton('body');
  782. $this->module->see('context, offsetX: 600 - offsetY: 384');
  783. $this->module->clickWithRightButton('body', 50, 75);
  784. $this->module->see('context, offsetX: 58 - offsetY: 83');
  785. $this->module->clickWithRightButton('body div');
  786. $this->module->see('context, offsetX: 58 - offsetY: 58');
  787. $this->module->clickWithRightButton('#element2', 70, 75);
  788. $this->module->see('context, offsetX: 78 - offsetY: 183');
  789. }
  790. public function testBrowserTabs()
  791. {
  792. $this->notForPhantomJS();
  793. $this->module->amOnPage('/form/example1');
  794. $this->module->openNewTab();
  795. $this->module->amOnPage('/form/example2');
  796. $this->module->openNewTab();
  797. $this->module->amOnPage('/form/example3');
  798. $this->module->openNewTab();
  799. $this->module->amOnPage('/form/example4');
  800. $this->module->openNewTab();
  801. $this->module->amOnPage('/form/example5');
  802. $this->module->closeTab();
  803. $this->module->seeInCurrentUrl('example4');
  804. $this->module->switchToPreviousTab(2);
  805. $this->module->seeInCurrentUrl('example2');
  806. $this->module->switchToNextTab();
  807. $this->module->seeInCurrentUrl('example3');
  808. $this->module->closeTab();
  809. $this->module->seeInCurrentUrl('example2');
  810. $this->module->switchToNextTab(2);
  811. $this->module->seeInCurrentUrl('example1');
  812. }
  813. public function testPerformOnWithArray()
  814. {
  815. $asserts = PHPUnit_Framework_Assert::getCount();
  816. $this->module->amOnPage('/form/example1');
  817. $this->module->performOn('.rememberMe', [
  818. 'see' => 'Remember me next time',
  819. 'seeElement' => '#LoginForm_rememberMe',
  820. 'dontSee' => 'Login'
  821. ]);
  822. $this->assertEquals(3, PHPUnit_Framework_Assert::getCount() - $asserts);
  823. $this->module->see('Login');
  824. }
  825. public function testPerformOnWithCallback()
  826. {
  827. $asserts = PHPUnit_Framework_Assert::getCount();
  828. $this->module->amOnPage('/form/example1');
  829. $this->module->performOn('.rememberMe', function (\Codeception\Module\WebDriver $I) {
  830. $I->see('Remember me next time');
  831. $I->seeElement('#LoginForm_rememberMe');
  832. $I->dontSee('Login');
  833. });
  834. $this->assertEquals(3, PHPUnit_Framework_Assert::getCount() - $asserts);
  835. $this->module->see('Login');
  836. }
  837. public function testPerformOnWithBuiltArray()
  838. {
  839. $asserts = PHPUnit_Framework_Assert::getCount();
  840. $this->module->amOnPage('/form/example1');
  841. $this->module->performOn('.rememberMe', \Codeception\Util\ActionSequence::build()
  842. ->see('Remember me next time')
  843. ->seeElement('#LoginForm_rememberMe')
  844. ->dontSee('Login')
  845. );
  846. $this->assertEquals(3, PHPUnit_Framework_Assert::getCount() - $asserts);
  847. $this->module->see('Login');
  848. }
  849. public function testPerformOnWithArrayAndSimilarActions()
  850. {
  851. $asserts = PHPUnit_Framework_Assert::getCount();
  852. $this->module->amOnPage('/form/example1');
  853. $this->module->performOn('.rememberMe', \Codeception\Util\ActionSequence::build()
  854. ->see('Remember me')
  855. ->see('next time')
  856. ->dontSee('Login')
  857. );
  858. $this->assertEquals(3, PHPUnit_Framework_Assert::getCount() - $asserts);
  859. $this->module->see('Login');
  860. }
  861. public function testPerformOnFail()
  862. {
  863. $this->shouldFail();
  864. $this->module->amOnPage('/form/example1');
  865. $this->module->performOn('.rememberMe', \Codeception\Util\ActionSequence::build()
  866. ->seeElement('#LoginForm_rememberMe')
  867. ->see('Remember me tomorrow')
  868. );
  869. }
  870. public function testPerformOnFail2()
  871. {
  872. $this->shouldFail();
  873. $this->module->amOnPage('/form/example1');
  874. $this->module->performOn('.rememberMe', ['see' => 'Login']);
  875. }
  876. public function testSwitchToIframe()
  877. {
  878. $this->module->amOnPage('iframe');
  879. $this->module->switchToIFrame('content');
  880. $this->module->see('Lots of valuable data here');
  881. $this->module->switchToIFrame();
  882. $this->module->see('Iframe test');
  883. }
  884. public function testGrabPageSourceWhenNotOnPage()
  885. {
  886. $this->setExpectedException(
  887. '\Codeception\Exception\ModuleException',
  888. 'Current url is blank, no page was opened'
  889. );
  890. $this->module->grabPageSource();
  891. }
  892. public function testGrabPageSourceWhenOnPage()
  893. {
  894. $this->module->amOnPage('/minimal');
  895. $sourceExpected =
  896. <<<HTML
  897. <!DOCTYPE html>
  898. <html>
  899. <head>
  900. <title>
  901. Minimal page
  902. </title>
  903. </head>
  904. <body>
  905. <h1>
  906. Minimal page
  907. </h1>
  908. </body>
  909. </html>
  910. HTML
  911. ;
  912. $sourceActualRaw = $this->module->grabPageSource();
  913. // `Selenium` adds the `xmlns` attribute while `PhantomJS` does not do that.
  914. $sourceActual = str_replace('xmlns="http://www.w3.org/1999/xhtml"', '', $sourceActualRaw);
  915. $this->assertXmlStringEqualsXmlString($sourceExpected, $sourceActual);
  916. }
  917. public function testChangingCapabilities()
  918. {
  919. $this->notForPhantomJS();
  920. $this->assertNotTrue($this->module->webDriver->getCapabilities()->getCapability('acceptInsecureCerts'));
  921. $this->module->_closeSession();
  922. $this->module->_capabilities(function($current) {
  923. $current['acceptInsecureCerts'] = true;
  924. return new DesiredCapabilities($current);
  925. });
  926. $this->assertNotTrue($this->module->webDriver->getCapabilities()->getCapability('acceptInsecureCerts'));
  927. $this->module->_initializeSession();
  928. $this->assertTrue(true, $this->module->webDriver->getCapabilities()->getCapability('acceptInsecureCerts'));
  929. }
  930. }