BrowserWithJavascriptTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests;
  3. /**
  4. * Tests if we can execute JavaScript in the browser.
  5. *
  6. * @group javascript
  7. */
  8. class BrowserWithJavascriptTest extends WebDriverTestBase {
  9. public function testJavascript() {
  10. $this->drupalGet('<front>');
  11. $session = $this->getSession();
  12. $session->resizeWindow(400, 300);
  13. $javascript = <<<JS
  14. (function(){
  15. var w = window,
  16. d = document,
  17. e = d.documentElement,
  18. g = d.getElementsByTagName('body')[0],
  19. x = w.innerWidth || e.clientWidth || g.clientWidth,
  20. y = w.innerHeight || e.clientHeight|| g.clientHeight;
  21. return x == 400 && y == 300;
  22. }());
  23. JS;
  24. $this->assertJsCondition($javascript);
  25. }
  26. public function testAssertJsCondition() {
  27. $this->drupalGet('<front>');
  28. $session = $this->getSession();
  29. $session->resizeWindow(500, 300);
  30. $javascript = <<<JS
  31. (function(){
  32. var w = window,
  33. d = document,
  34. e = d.documentElement,
  35. g = d.getElementsByTagName('body')[0],
  36. x = w.innerWidth || e.clientWidth || g.clientWidth,
  37. y = w.innerHeight || e.clientHeight|| g.clientHeight;
  38. return x == 400 && y == 300;
  39. }());
  40. JS;
  41. // We expected the following assertion to fail because the window has been
  42. // re-sized to have a width of 500 not 400.
  43. $this->setExpectedException(\PHPUnit_Framework_AssertionFailedError::class);
  44. $this->assertJsCondition($javascript, 100);
  45. }
  46. /**
  47. * Tests creating screenshots.
  48. */
  49. public function testCreateScreenshot() {
  50. $this->drupalGet('<front>');
  51. $this->createScreenshot('public://screenshot.jpg');
  52. $this->assertFileExists('public://screenshot.jpg');
  53. }
  54. }