JavascriptEvaluationTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Behat\Mink\Tests\Driver\Js;
  3. use Behat\Mink\Tests\Driver\TestCase;
  4. class JavascriptEvaluationTest extends TestCase
  5. {
  6. /**
  7. * Tests, that `wait` method returns check result after exit.
  8. */
  9. public function testWaitReturnValue()
  10. {
  11. $this->getSession()->visit($this->pathTo('/js_test.html'));
  12. $found = $this->getSession()->wait(5000, '$("#draggable").length == 1');
  13. $this->assertTrue($found);
  14. }
  15. public function testWaitReturnValueAlwaysBoolean()
  16. {
  17. $this->getSession()->visit($this->pathTo('/js_test.html'));
  18. $found = $this->getSession()->wait(5000, '$("#draggable").length');
  19. $this->assertTrue($found);
  20. }
  21. public function testWait()
  22. {
  23. $this->getSession()->visit($this->pathTo('/js_test.html'));
  24. $waitable = $this->findById('waitable');
  25. $waitable->click();
  26. $this->getSession()->wait(3000, '$("#waitable").has("div").length > 0');
  27. $this->assertEquals('arrived', $this->getAssertSession()->elementExists('css', '#waitable > div')->getText());
  28. $waitable->click();
  29. $this->getSession()->wait(3000, 'false');
  30. $this->assertEquals('timeout', $this->getAssertSession()->elementExists('css', '#waitable > div')->getText());
  31. }
  32. /**
  33. * @dataProvider provideExecutedScript
  34. */
  35. public function testExecuteScript($script)
  36. {
  37. $this->getSession()->visit($this->pathTo('/index.html'));
  38. $this->getSession()->executeScript($script);
  39. sleep(1);
  40. $heading = $this->getAssertSession()->elementExists('css', 'h1');
  41. $this->assertEquals('Hello world', $heading->getText());
  42. }
  43. public function provideExecutedScript()
  44. {
  45. return array(
  46. array('document.querySelector("h1").textContent = "Hello world"'),
  47. array('document.querySelector("h1").textContent = "Hello world";'),
  48. array('function () {document.querySelector("h1").textContent = "Hello world";}()'),
  49. array('function () {document.querySelector("h1").textContent = "Hello world";}();'),
  50. array('(function () {document.querySelector("h1").textContent = "Hello world";})()'),
  51. array('(function () {document.querySelector("h1").textContent = "Hello world";})();'),
  52. );
  53. }
  54. /**
  55. * @dataProvider provideEvaluatedScript
  56. */
  57. public function testEvaluateJavascript($script)
  58. {
  59. $this->getSession()->visit($this->pathTo('/index.html'));
  60. $this->assertSame(2, $this->getSession()->evaluateScript($script));
  61. }
  62. public function provideEvaluatedScript()
  63. {
  64. return array(
  65. array('1 + 1'),
  66. array('1 + 1;'),
  67. array('return 1 + 1'),
  68. array('return 1 + 1;'),
  69. array('function () {return 1+1;}()'),
  70. array('(function () {return 1+1;})()'),
  71. array('return function () { return 1+1;}()'),
  72. array('return (function () {return 1+1;})()'),
  73. );
  74. }
  75. }