PageRenderTime 66ms CodeModel.GetById 19ms RepoModel.GetById 8ms app.codeStats 0ms

/SessionManager.php

https://github.com/andreaswolf/Menta
PHP | 116 lines | 55 code | 13 blank | 48 comment | 9 complexity | 51fe999e04b2c0e59397bc4cd7b0390f MD5 | raw file
  1. <?php
  2. /**
  3. * Global session manager for connections to the Selenium 2 server
  4. */
  5. class Menta_SessionManager {
  6. /**
  7. * @var WebDriver_Session
  8. */
  9. protected static $session;
  10. /**
  11. * @var WebDriver
  12. */
  13. protected static $webdriver;
  14. /**
  15. * @var string
  16. */
  17. protected static $browser = 'firefox';
  18. /**
  19. * @var string
  20. */
  21. protected static $serverUrl = 'http://localhost:4444/wd/hub';
  22. /**
  23. * @var array
  24. */
  25. protected static $additionalCapabilities = array();
  26. /**
  27. * Init settings
  28. *
  29. * @static
  30. * @param $serverUrl
  31. * @param string $browser
  32. * @param array $additionalCapabilities
  33. * @return void
  34. */
  35. public static function init($serverUrl=NULL, $browser=NULL, array $additionalCapabilities=NULL) {
  36. if (!is_null($serverUrl)) {
  37. self::$serverUrl = $serverUrl;
  38. }
  39. if (!is_null($browser)) {
  40. self::$browser = $browser;
  41. }
  42. if (!is_null($additionalCapabilities)) {
  43. self::$additionalCapabilities = $additionalCapabilities;
  44. }
  45. }
  46. /**
  47. * @static
  48. * @return WebDriver
  49. */
  50. public static function getWebdriver() {
  51. if (is_null(self::$webdriver)) {
  52. if (empty(self::$serverUrl)) {
  53. throw new Exception('No serverUrl set. Call Menta_SessionManager::init() to configure first');
  54. }
  55. self::$webdriver = new WebDriver(self::$serverUrl);
  56. }
  57. return self::$webdriver;
  58. }
  59. /**
  60. * @param bool $forceNew
  61. * @static
  62. * @return WebDriver_Session
  63. */
  64. public static function getSession($forceNew=false) {
  65. if ($forceNew) {
  66. self::closeSession();
  67. }
  68. if (is_null(self::$session)) {
  69. self::$session = self::getWebdriver()->session(self::$browser, self::$additionalCapabilities);
  70. if (!self::$session instanceof WebDriver_Session) {
  71. throw new Exception('Error while creating new session');
  72. }
  73. Menta_Events::dispatchEvent('after_session_create', array(
  74. 'session' => self::$session,
  75. 'forceNew' => $forceNew
  76. ));
  77. }
  78. return self::$session;
  79. }
  80. /**
  81. * Check if an active session exists
  82. *
  83. * @static
  84. * @return bool
  85. */
  86. public static function activeSessionExists() {
  87. return (self::$session instanceof WebDriver_Session);
  88. }
  89. /**
  90. * Close existing session
  91. *
  92. * @static
  93. * @return void
  94. */
  95. public static function closeSession() {
  96. if (self::activeSessionExists()) {
  97. Menta_Events::dispatchEvent('before_session_close', array('session' => self::$session));
  98. self::$session->close();
  99. self::$session = NULL;
  100. Menta_Events::dispatchEvent('after_session_close');
  101. }
  102. }
  103. }