selenium.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage Extensions
  6. * @version $Id: selenium.php 1802 2008-09-08 10:43:58Z maetl_ $
  7. */
  8. require_once dirname(__FILE__) . '/../unit_tester.php';
  9. require_once dirname(__FILE__) . '/selenium/remote-control.php';
  10. /**
  11. * Provides test case wrapper to a Selenium remote
  12. * control instance.
  13. */
  14. class SeleniumTestCase extends UnitTestCase
  15. {
  16. /**#@+
  17. * Selenium instantiation variables
  18. */
  19. protected $browser = '';
  20. protected $browserUrl = '';
  21. protected $host = 'localhost';
  22. protected $port = '4444';
  23. protected $timeout = 30000;
  24. /**#@-*/
  25. protected $selenium = null;
  26. protected $newInstanceEachTest = true;
  27. public function __construct($name = 'Selenium Test Case') {
  28. parent::__construct($name);
  29. if (empty($this->browser)) {
  30. trigger_error('browser property must be set in ' . get_class($this));
  31. exit;
  32. }
  33. if (empty($this->browserUrl)) {
  34. trigger_error('browserUrl property must be set in ' . get_class($this));
  35. exit;
  36. }
  37. }
  38. public function setUp() {
  39. parent::setUp();
  40. if (is_null($this->selenium)) {
  41. $this->selenium = new SimpleSeleniumRemoteControl(
  42. $this->browser,
  43. $this->browserUrl,
  44. $this->host,
  45. $this->port,
  46. $this->timeout
  47. );
  48. $this->selenium->start();
  49. }
  50. }
  51. public function tearDown() {
  52. parent::tearDown();
  53. if ($this->newInstanceEachTest) {
  54. $this->selenium->stop();
  55. $this->selenium = null;
  56. }
  57. }
  58. public function __call($method, $arguments) {
  59. if (substr($method, 0, 6) == 'verify') {
  60. return $this->assertTrue(
  61. call_user_func_array(
  62. array($this->selenium, $method),
  63. $arguments
  64. ),
  65. sprintf('%s failed', $method)
  66. );
  67. }
  68. return call_user_func_array(
  69. array($this->selenium, $method),
  70. $arguments
  71. );
  72. }
  73. public function verifyText($text) {
  74. return $this->assertTrue(
  75. $this->selenium->verifyText($text),
  76. sprintf(
  77. 'verifyText failed when on [%s]',
  78. $text
  79. )
  80. );
  81. }
  82. public function verifyTextPresent($text) {
  83. return $this->assertTrue(
  84. $this->selenium->verifyTextPresent($text),
  85. sprintf(
  86. 'verifyTextPresent failed when on [%s]',
  87. $text
  88. )
  89. );
  90. }
  91. public function verifyTextNotPresent($text) {
  92. return $this->assertTrue(
  93. $this->selenium->verifyTextNotPresent($text),
  94. sprintf(
  95. 'verifyTextNotPresent failed on [%s]',
  96. $text
  97. )
  98. );
  99. }
  100. public function verifyValue($selector, $value) {
  101. return $this->assertTrue(
  102. $this->selenium->verifyValue($selector, $value),
  103. sprintf(
  104. 'verifyValue failed on [%s] == [%s]',
  105. $selector,
  106. $value
  107. )
  108. );
  109. }
  110. public function verifyTitle($pattern) {
  111. return $this->assertTrue(
  112. $this->selenium->verifyTitle($pattern),
  113. sprintf(
  114. 'verifyTitle failed on [%s]',
  115. $pattern
  116. )
  117. );
  118. }
  119. }