PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Component/Helper/Assert.php

https://github.com/andreaswolf/Menta
PHP | 134 lines | 60 code | 18 blank | 56 comment | 15 complexity | 6f60acd892e410fcae8a50fdbaba80cb MD5 | raw file
  1. <?php
  2. /**
  3. * Assert helper
  4. *
  5. * @author Fabrizio Branca
  6. * @since 2011-11-18
  7. */
  8. class Menta_Component_Helper_Assert extends Menta_Component_AbstractTest {
  9. /**
  10. * Assert page title
  11. *
  12. * @param $title
  13. * @param string $message
  14. * @return void
  15. */
  16. public function assertTitle($title, $message='') {
  17. if ($this->getConfiguration()) {
  18. if ($this->getConfiguration()->issetKey('testing.selenium.titlePrefix')) {
  19. $title = $this->getConfiguration()->getValue('testing.selenium.titlePrefix') . $title;
  20. }
  21. if ($this->getConfiguration()->issetKey('testing.selenium.titleSuffix')) {
  22. $title .= $this->getConfiguration()->getValue('testing.selenium.titleSuffix');
  23. }
  24. }
  25. $this->getTest()->assertEquals($title, $this->getSession()->title(), $message);
  26. }
  27. /**
  28. * Assert text present
  29. *
  30. * @param string $text
  31. * @param string $message
  32. * @return void
  33. */
  34. public function assertTextPresent($text, $message='') {
  35. if (empty($message)) {
  36. $message = "Text '$text' not found";
  37. }
  38. $this->getTest()->assertTrue($this->getHelperCommon()->isTextPresent($text), $message);
  39. }
  40. /**
  41. * Assert text not present
  42. *
  43. * @param string $text
  44. * @param string $message
  45. * @return void
  46. */
  47. public function assertTextNotPresent($text, $message='') {
  48. if (empty($message)) {
  49. $message = "Text '$text' found";
  50. }
  51. $this->getTest()->assertFalse($this->getHelperCommon()->isTextPresent($text), $message);
  52. }
  53. /**
  54. * Assert element present
  55. *
  56. * @param string|array|WebDriver_Element $element
  57. * @param string $message
  58. * @return void
  59. */
  60. public function assertElementPresent($element, $message='') {
  61. if (empty($message)) {
  62. $message = sprintf("Element '%s' not found", $this->getHelperCommon()->element2String($element));
  63. }
  64. $this->getTest()->assertTrue($this->getHelperCommon()->isElementPresent($element), $message);
  65. }
  66. /**
  67. * Assert element not present
  68. *
  69. * @param string|array|WebDriver_Element $element
  70. * @param string $message
  71. * @param bool $implictWait
  72. * @return void
  73. */
  74. public function assertElementNotPresent($element, $message='', $implictWait=false) {
  75. if (!$implictWait && $this->getConfiguration() && $this->getConfiguration()->issetKey('testing.selenium.timeoutImplicitWait')) {
  76. $time = $this->getConfiguration()->getValue('testing.selenium.timeoutImplicitWait');
  77. $this->getSession()->timeouts()->implicit_wait(array('ms' => 0)); // deactivate implicit wait
  78. }
  79. if (empty($message)) {
  80. $message = sprintf("Element '%s' found", $this->getHelperCommon()->element2String($element));
  81. }
  82. try {
  83. $elementPresent = $this->getHelperCommon()->isElementPresent($element);
  84. } catch (Exception $e) {}
  85. if (!empty($time)) {
  86. $this->getSession()->timeouts()->implicit_wait(array('ms' => $time)); // reactivate implicit wait
  87. }
  88. // "finally" workaround
  89. if (isset($e)) { throw $e; }
  90. if ($elementPresent) {
  91. $this->getTest()->fail($message);
  92. }
  93. }
  94. /**
  95. * Assert element containts text
  96. *
  97. * @param string|array|WebDriver_Element $element
  98. * @param string $text
  99. * @param string $message
  100. * @return void
  101. */
  102. public function assertElementContainsText($element, $text, $message='') {
  103. if ($message == '') {
  104. $message = sprintf('Element "%s" does not contain text "%s"', $this->getHelperCommon()->element2String($element), $text);
  105. }
  106. $this->getTest()->assertContains($text, $this->getHelperCommon()->getText($element), $message);
  107. }
  108. /**
  109. * Get common helper
  110. *
  111. * @return Menta_Component_Helper_Common
  112. */
  113. protected function getHelperCommon() {
  114. return Menta_ComponentManager::get('Menta_Component_Helper_Common');
  115. }
  116. }