/scalate-test/src/main/scala/org/fusesource/scalate/test/WebDriverMixin.scala

http://github.com/scalate/scalate · Scala · 120 lines · 77 code · 18 blank · 25 comment · 12 complexity · 84495048b383f78bdd6b616ac1eee882 MD5 · raw file

  1. /**
  2. * Copyright (C) 2009-2011 the original author or authors.
  3. * See the notice.md file distributed with this work for additional
  4. * information regarding copyright ownership.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.fusesource.scalate.test
  19. import org.openqa.selenium.htmlunit.HtmlUnitDriver
  20. import org.scalatest.{ ConfigMap, FunSuite, BeforeAndAfterAllConfigMap }
  21. import org.openqa.selenium.{ WebDriver, WebElement }
  22. import org.openqa.selenium.internal.FindsByXPath
  23. /**
  24. * A simple trait for testing web pages using Selenium WebDriver
  25. *
  26. * @version $Revision : 1.1 $
  27. */
  28. trait WebDriverMixin extends BeforeAndAfterAllConfigMap { this: FunSuite =>
  29. def rootUrl: String
  30. var webDriver: WebDriver = new HtmlUnitDriver
  31. def xpathDriver = webDriver.asInstanceOf[FindsByXPath]
  32. override protected def afterAll(configMap: ConfigMap) = webDriver.close
  33. def pageContains(textLines: String*): Unit = {
  34. val source = webDriver.getPageSource
  35. assume(source != null, "page source was null for " + webDriver.getCurrentUrl)
  36. var index = 0
  37. for (text <- textLines if index >= 0) {
  38. index = source.indexOf(text, index)
  39. if (index >= 0) {
  40. index += text.length
  41. } else {
  42. assume(false, "Page does not contain '" + text + "' for " + webDriver.getCurrentUrl + " when page was\n" + source)
  43. }
  44. }
  45. }
  46. def pageNotContains(textLines: String*): Unit = {
  47. val source = webDriver.getPageSource
  48. assume(source != null, "page source was null for " + webDriver.getCurrentUrl)
  49. for (text <- textLines) {
  50. val index = source.indexOf(text)
  51. if (index >= 0) {
  52. assume(false, "Page contains '" + text + "' at index " + index + " for " + webDriver.getCurrentUrl + " when page was\n" + source)
  53. }
  54. }
  55. }
  56. def pageMatches(regex: String): Unit = {
  57. val source = pageSource
  58. assume(source != null, "page source was null for " + webDriver.getCurrentUrl)
  59. assume(source.matches(regex), "Page does not match '" + regex + "' for " + webDriver.getCurrentUrl + " when page was\n" + source)
  60. }
  61. def pageSource = webDriver.getPageSource
  62. def testPageContains(uri: String, textLines: String*): Unit = {
  63. testPage(uri) {
  64. pageContains(textLines: _*)
  65. }
  66. }
  67. def testPageNotContains(uri: String, textLines: String*): Unit = {
  68. testPage(uri) {
  69. pageNotContains(textLines: _*)
  70. }
  71. }
  72. def testPageMatches(uri: String, matches: String): Unit = {
  73. testPage(uri) {
  74. pageMatches(matches)
  75. }
  76. }
  77. def testPage(uri: String)(func: => Unit): Unit = {
  78. test("page: " + uri) {
  79. val fullUri = if (uri.startsWith("http")) { uri } else { rootUrl + uri }
  80. println("Loading page: " + fullUri)
  81. webDriver.get(fullUri)
  82. println("About to run test for: " + fullUri)
  83. func
  84. println("Completed test for: " + fullUri)
  85. }
  86. }
  87. /**
  88. * Returns the XPath selector which can then be used for further navigation
  89. */
  90. def xpath(selector: String): WebElement = {
  91. try {
  92. val answer = xpathDriver.findElementByXPath(selector)
  93. assume(answer != null, "xpath " + selector + " returned null!")
  94. answer
  95. } catch {
  96. case e: Exception =>
  97. println("Failed to find xpath: " + selector + " on page due to: " + e)
  98. println(pageSource)
  99. throw e
  100. }
  101. }
  102. }