|
Play Framework/Scala example source code file (Selenium.scala)
The Selenium.scala Play Framework example source code
/*
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package play.api.test
import play.api._
import play.core.server.{ NettyServer, ServerConfig }
import org.openqa.selenium._
import org.openqa.selenium.firefox._
import org.openqa.selenium.htmlunit._
import org.fluentlenium.core._
import java.util.Properties
import java.util.concurrent.TimeUnit
import com.google.common.base.Function
import org.openqa.selenium.support.ui.FluentWait
import scala.util.control.NonFatal
/**
* A test browser (Using Selenium WebDriver) with the FluentLenium API (https://github.com/Fluentlenium/FluentLenium).
*
* @param webDriver The WebDriver instance to use.
*/
case class TestBrowser(webDriver: WebDriver, baseUrl: Option[String]) extends FluentAdapter(webDriver) {
baseUrl.map(baseUrl => withDefaultUrl(baseUrl))
/**
* Submits a form with the given field values
*
* @example {{{
* submit("#login", fields =
* "email" -> email,
* "password" -> password
* )
* }}}
*/
def submit(selector: String, fields: (String, String)*): Fluent = {
fields.foreach {
case (fieldName, fieldValue) =>
fill(s"${selector} *[name=${fieldName}]").`with`(fieldValue)
}
super.submit(selector)
}
/**
* Repeatedly applies this instance's input value to the given block until one of the following occurs:
* the function returns neither null nor false,
* the function throws an unignored exception,
* the timeout expires
*
* @param timeout
* @param timeUnit duration
* @param block code to be executed
*/
def waitUntil[T](timeout: Int, timeUnit: TimeUnit)(block: => T): T = {
val wait = new FluentWait[WebDriver](webDriver).withTimeout(timeout, timeUnit)
val f = new Function[WebDriver, T]() {
def apply(driver: WebDriver): T = {
block
}
}
wait.until(f)
}
/**
* Repeatedly applies this instance's input value to the given block until one of the following occurs:
* the function returns neither null nor false,
* the function throws an unignored exception,
* the default timeout expires
*
* @param block code to be executed
*/
def waitUntil[T](block: => T): T = waitUntil(3000, TimeUnit.MILLISECONDS)(block)
/**
* retrieves the underlying option interface that can be used
* to set cookies, manage timeouts among other things
*/
def manage: WebDriver.Options = super.getDriver.manage
}
/**
* Helper utilities to build TestBrowsers
*/
object TestBrowser {
/**
* Creates an in-memory WebBrowser (using HtmlUnit)
*
* @param baseUrl The default base URL that will be used for relative URLs
*/
def default(baseUrl: Option[String] = None) = of(classOf[HtmlUnitDriver], baseUrl)
/**
* Creates a firefox WebBrowser.
*
* @param baseUrl The default base URL that will be used for relative URLs
*/
def firefox(baseUrl: Option[String] = None) = of(classOf[FirefoxDriver], baseUrl)
/**
* Creates a WebBrowser of the specified class name.
*
* @param baseUrl The default base URL that will be used for relative URLs
*/
def of[WEBDRIVER <: WebDriver](webDriver: Class[WEBDRIVER], baseUrl: Option[String] = None) = TestBrowser(WebDriverFactory(webDriver), baseUrl)
}
object WebDriverFactory {
/**
* Creates a Selenium Web Driver and configures it
* @param clazz Type of driver to create
* @return The driver instance
*/
def apply[D <: WebDriver](clazz: Class[D]): WebDriver = {
val driver = clazz.newInstance
// Driver-specific configuration
driver match {
case htmlunit: HtmlUnitDriver => htmlunit.setJavascriptEnabled(true)
case _ =>
}
driver
}
}
/**
* A test Netty web server.
*
* @param port HTTP port to bind on.
* @param application The FakeApplication to load in this server.
*/
case class TestServer(port: Int, application: FakeApplication = FakeApplication(), sslPort: Option[Int] = None) {
private var server: NettyServer = _
/**
* Starts this server.
*/
def start() {
if (server != null) {
sys.error("Server already started!")
}
//play.core.Invoker.uninit()
try {
val config = ServerConfig(
rootDir = application.path,
port = Option(port), sslPort = sslPort, mode = Mode.Test,
properties = System.getProperties
)
val appProvider = new play.core.TestApplication(application)
server = new NettyServer(config, appProvider)
} catch {
case NonFatal(t) =>
t.printStackTrace
throw new RuntimeException(t)
}
}
/**
* Stops this server.
*/
def stop() {
if (server != null) {
server.stop()
server = null
}
//play.api.libs.concurrent.Promise.resetSystem()
}
}
Other Play Framework source code examplesHere is a short list of links related to this Play Framework Selenium.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.