Skip to content

Instantly share code, notes, and snippets.

@fmpwizard
Created August 15, 2011 01:43
Show Gist options
  • Save fmpwizard/1145577 to your computer and use it in GitHub Desktop.
Save fmpwizard/1145577 to your computer and use it in GitHub Desktop.
import org.specs2._
import specification._
import matcher._
import lib.{OptionFormatter => OF}
import net.liftweb.common.Logger
import net.liftweb.mapper.Like
import model._
trait SeleniumSpec extends Specification with BeforeExample with Logger {
import org.mortbay.jetty.{ Connector, Server}
import org.mortbay.jetty.webapp.{ WebAppContext }
import org.openqa.selenium.server.RemoteControlConfiguration
import org.openqa.selenium.server.SeleniumServer
import com.thoughtworks.selenium._
/** the map method allows to "post-process" the fragments after their creation */
override def map(fs: =>Fragments) = Step(startSelenium) ^ fs ^ Step(stopSelenium)
lazy val GUI_PORT = 8090
lazy val SELENIUM_SERVER_PORT = 4444
lazy val rc = new RemoteControlConfiguration()
rc.setPort(SELENIUM_SERVER_PORT)
lazy val seleniumserver = new SeleniumServer(rc)
lazy val server = new Server(GUI_PORT)
lazy val selenium = new DefaultSelenium(
"localhost", SELENIUM_SERVER_PORT, "*firefox", "http://localhost:"+GUI_PORT+"/"
)
// Setting up the Selenium Server for the duration of the tests
def startSelenium {
/* This code takes care of the following:
1. Start an instance of your web application
2. Start an instance of the Selenium backend
3. Start an instance of the Selenium client
4. Reset the Categories table
*/
// Setting up the jetty instance which will be running the
// GUI for the duration of the tests
lazy val context = new WebAppContext()
context.setServer(server)
context.setContextPath("/")
context.setWar("src/main/webapp")
server.addHandler(context)
server.start()
seleniumserver.boot()
seleniumserver.start()
seleniumserver.getPort()
// Setting up the Selenium Client for the duration of the tests
selenium.start()
}
def stopSelenium {
// Close everyhing when done
selenium.close()
server.stop()
seleniumserver.stop()
}
}
object InventoryCategoriesCreateTestSpecs extends SeleniumSpec with DataTables { def is =
"This is a specification to create a category 'InventoryCategoriesCreate' " ^
p^
"The 'InventoryCategoriesCreate' class should create a Category" ^
args(sequential=true) ^
"Empty the categories table" ! cleanDb^
"Create Display, Storage and Power" ! e1^
"Create Display, LCD subcategory, Storage, HDD and DVD subcategories" ! e4^
"Fail to create Storage -> DVD subcategory" ! e7^
"Fail to create Storage" ! e8^
end
def before = getToPage
/**
* Actual test code
*/
def e1 ={
(createVerCat(_: String)).forall(Seq("Display", "Storage", "Power"))
}
/**
* Using Data Tables to simplify writing tests
*/
def e4 =
"cat" || "sub" |
"Display" !! "LCD" |
"Storage" !! "HDD" |
"Storage" !! "DVD" |>
{
(cat, sub) => createVerSubCat(cat, sub)
}
/**
* You cannot duplicate a category
*/
def e7 = {
createVerSubCat("Storage", "DVD")(false)
}
def e8 = {
createVerCat("Storage")(false)
}
/**
* Boiler plate code
*/
def getToPage = {
selenium.open("/")
selenium.click("link=Inventory")
selenium.waitForPageToLoad("30000")
selenium.click("link=Categories")
selenium.waitForPageToLoad("30000")
}
/**
* Create category and verify it
*/
def createVerCat(cat: String)(implicit pass: Boolean= true) = {
selenium.`type`("category_name", cat);
selenium.`type`("category_lang", "EN");
selenium.`type`("category_sort", "0");
selenium.click("//fieldset[2]/input");
selenium.waitForPageToLoad("30000");
selenium.isTextPresent("Added Category: " + cat) must_== pass
}
/**
* Create and verify subcategory
*/
def createVerSubCat(cat: String, sub: String)(implicit pass: Boolean= true) = {
selenium.`type`("category_name", sub);
selenium.`type`("category_lang", "EN");
selenium.select("id=category_parent", "label=" + cat)
selenium.`type`("category_sort", "0");
selenium.click("//fieldset[2]/input");
selenium.waitForPageToLoad("30000");
selenium.isTextPresent("Added Category: " + sub) must_== pass
}
def cleanDb = {
Categories.bulkDelete_!!(Like(Categories.category_name, "%"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment