PageRenderTime 46ms CodeModel.GetById 20ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 18package org.fusesource.scalate.test
 19
 20import org.openqa.selenium.htmlunit.HtmlUnitDriver
 21import org.scalatest.{ ConfigMap, FunSuite, BeforeAndAfterAllConfigMap }
 22import org.openqa.selenium.{ WebDriver, WebElement }
 23import org.openqa.selenium.internal.FindsByXPath
 24
 25/**
 26 * A simple trait for testing web pages using Selenium WebDriver
 27 *
 28 * @version $Revision : 1.1 $
 29 */
 30trait WebDriverMixin extends BeforeAndAfterAllConfigMap { this: FunSuite =>
 31
 32  def rootUrl: String
 33
 34  var webDriver: WebDriver = new HtmlUnitDriver
 35
 36  def xpathDriver = webDriver.asInstanceOf[FindsByXPath]
 37
 38  override protected def afterAll(configMap: ConfigMap) = webDriver.close
 39
 40  def pageContains(textLines: String*): Unit = {
 41    val source = webDriver.getPageSource
 42    assume(source != null, "page source was null for " + webDriver.getCurrentUrl)
 43    var index = 0
 44    for (text <- textLines if index >= 0) {
 45      index = source.indexOf(text, index)
 46      if (index >= 0) {
 47        index += text.length
 48      } else {
 49        assume(false, "Page does not contain '" + text + "' for " + webDriver.getCurrentUrl + " when page was\n" + source)
 50      }
 51    }
 52  }
 53
 54  def pageNotContains(textLines: String*): Unit = {
 55    val source = webDriver.getPageSource
 56    assume(source != null, "page source was null for " + webDriver.getCurrentUrl)
 57    for (text <- textLines) {
 58      val index = source.indexOf(text)
 59      if (index >= 0) {
 60        assume(false, "Page contains '" + text + "' at index " + index + " for " + webDriver.getCurrentUrl + " when page was\n" + source)
 61      }
 62    }
 63  }
 64
 65  def pageMatches(regex: String): Unit = {
 66    val source = pageSource
 67    assume(source != null, "page source was null for " + webDriver.getCurrentUrl)
 68    assume(source.matches(regex), "Page does not match '" + regex + "' for " + webDriver.getCurrentUrl + " when page was\n" + source)
 69  }
 70
 71  def pageSource = webDriver.getPageSource
 72
 73  def testPageContains(uri: String, textLines: String*): Unit = {
 74    testPage(uri) {
 75      pageContains(textLines: _*)
 76    }
 77  }
 78
 79  def testPageNotContains(uri: String, textLines: String*): Unit = {
 80    testPage(uri) {
 81      pageNotContains(textLines: _*)
 82    }
 83  }
 84
 85  def testPageMatches(uri: String, matches: String): Unit = {
 86    testPage(uri) {
 87      pageMatches(matches)
 88    }
 89  }
 90
 91  def testPage(uri: String)(func: => Unit): Unit = {
 92    test("page: " + uri) {
 93      val fullUri = if (uri.startsWith("http")) { uri } else { rootUrl + uri }
 94
 95      println("Loading page: " + fullUri)
 96      webDriver.get(fullUri)
 97
 98      println("About to run test for: " + fullUri)
 99      func
100      println("Completed test for: " + fullUri)
101    }
102  }
103
104  /**
105   * Returns the XPath selector which can then be used for further navigation
106   */
107  def xpath(selector: String): WebElement = {
108    try {
109      val answer = xpathDriver.findElementByXPath(selector)
110      assume(answer != null, "xpath " + selector + " returned null!")
111      answer
112    } catch {
113      case e: Exception =>
114        println("Failed to find xpath: " + selector + " on page due to: " + e)
115        println(pageSource)
116        throw e
117    }
118  }
119
120}