WindowTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Behat\Mink\Tests\Driver\Js;
  3. use Behat\Mink\Tests\Driver\TestCase;
  4. class WindowTest extends TestCase
  5. {
  6. public function testWindow()
  7. {
  8. $this->getSession()->visit($this->pathTo('/window.html'));
  9. $session = $this->getSession();
  10. $page = $session->getPage();
  11. $webAssert = $this->getAssertSession();
  12. $page->clickLink('Popup #1');
  13. $session->switchToWindow(null);
  14. $page->clickLink('Popup #2');
  15. $session->switchToWindow(null);
  16. $el = $webAssert->elementExists('css', '#text');
  17. $this->assertSame('Main window div text', $el->getText());
  18. $session->switchToWindow('popup_1');
  19. $el = $webAssert->elementExists('css', '#text');
  20. $this->assertSame('Popup#1 div text', $el->getText());
  21. $session->switchToWindow('popup_2');
  22. $el = $webAssert->elementExists('css', '#text');
  23. $this->assertSame('Popup#2 div text', $el->getText());
  24. $session->switchToWindow(null);
  25. $el = $webAssert->elementExists('css', '#text');
  26. $this->assertSame('Main window div text', $el->getText());
  27. }
  28. public function testGetWindowNames()
  29. {
  30. $this->getSession()->visit($this->pathTo('/window.html'));
  31. $session = $this->getSession();
  32. $page = $session->getPage();
  33. $windowName = $this->getSession()->getWindowName();
  34. $this->assertNotNull($windowName);
  35. $page->clickLink('Popup #1');
  36. $page->clickLink('Popup #2');
  37. $windowNames = $this->getSession()->getWindowNames();
  38. $this->assertNotNull($windowNames[0]);
  39. $this->assertNotNull($windowNames[1]);
  40. $this->assertNotNull($windowNames[2]);
  41. }
  42. public function testResizeWindow()
  43. {
  44. $this->getSession()->visit($this->pathTo('/index.html'));
  45. $session = $this->getSession();
  46. $session->resizeWindow(400, 300);
  47. $session->wait(1000, 'false');
  48. $jsWindowSizeScript = <<<JS
  49. (function(){
  50. var boolSizeCheck = Math.abs(window.outerHeight - 300) <= 100 && Math.abs(window.outerWidth - 400) <= 100;
  51. if (boolSizeCheck){
  52. return true;
  53. }
  54. var w = window,
  55. d = document,
  56. e = d.documentElement,
  57. g = d.getElementsByTagName('body')[0],
  58. x = w.innerWidth || e.clientWidth || g.clientWidth,
  59. y = w.innerHeight|| e.clientHeight|| g.clientHeight;
  60. boolSizeCheck = Math.abs(y - 300) <= 100 && Math.abs(x - 400) <= 100;
  61. return boolSizeCheck;
  62. })();
  63. JS;
  64. $this->assertTrue($session->evaluateScript($jsWindowSizeScript));
  65. }
  66. public function testWindowMaximize()
  67. {
  68. $this->getSession()->visit($this->pathTo('/index.html'));
  69. $session = $this->getSession();
  70. $session->maximizeWindow();
  71. $session->wait(1000, 'false');
  72. $script = 'return Math.abs(screen.availHeight - window.outerHeight) <= 100;';
  73. $this->assertTrue($session->evaluateScript($script));
  74. }
  75. }