Skip to content

Instantly share code, notes, and snippets.

@pbernet
Created October 23, 2012 16:09
Show Gist options
  • Save pbernet/3939729 to your computer and use it in GitHub Desktop.
Save pbernet/3939729 to your computer and use it in GitHub Desktop.
CareWebPage
package xx.testcase.gui.pageobjects.page
import org.openqa.selenium.{By, JavascriptExecutor, WebDriverBackedSelenium}
import net.liftweb.util.Helpers
import net.liftweb.common.{Failure, Full}
import com.thoughtworks.selenium.SeleniumException
import org.openqa.selenium.support.ui.{ExpectedConditions, WebDriverWait}
import xx.testcase.gui.exception.LinkNotFoundException
trait CareWebPage {
@volatile
private var ajaxHasEnded: Boolean = false
protected val ajaxCount = 1
def selenium: WebDriverBackedSelenium
def isAt: Boolean
def url: String
def ajaxAndWait(ajaxFuncToWaitFor: () => Any) {
ajaxFuncToWaitFor()
val js = selenium.getWrappedDriver.asInstanceOf[JavascriptExecutor]
while (!ajaxHasEnded) {
val bool = js.executeScript("return jQuery.active == " + ajaxCount).asInstanceOf[Boolean]
ajaxHasEnded = bool
Thread.sleep(25)
}
ajaxHasEnded = false
}
protected def waitToBeClickable(anID: String) = {
val wait: WebDriverWait = new WebDriverWait(selenium.getWrappedDriver, 10)
wait.until(ExpectedConditions.elementToBeClickable(By.id(anID)))
}
protected def waitToForTitleText(text: String) = {
waitForElem(text, "pagetitle")
}
protected def waitToForWizardTitleText(text: String) = {
waitForElem(text, "wizardTitle")
}
private def waitForElem(text: String, id: String) = {
val wait: WebDriverWait = new WebDriverWait(selenium.getWrappedDriver, 10)
wait.until(ExpectedConditions.textToBePresentInElement(By.id(id), text))
}
protected def clickAndWaitTitleText(selector: String, waitFor: String): WebDriverBackedSelenium = {
clickAndWaitText(selector, waitFor, waitToForTitleText)
}
protected def clickAndWaitWizardTitleText(selector: String, waitFor: String): WebDriverBackedSelenium = {
clickAndWaitText(selector, waitFor, waitToForWizardTitleText)
}
protected def clickAndWaitText(selector: String, waitFor: String, waitFunc: String => Boolean): WebDriverBackedSelenium = {
val toNextPage = Helpers.tryo {
selenium.click(selector)
waitFunc(waitFor)
}
toNextPage match {
case Full(b) => selenium
case Failure(_, Full(e: SeleniumException), _) => throw new LinkNotFoundException(e)
case a: Any => println(a); throw new LinkNotFoundException
}
}
def goTo() {
Helpers.tryo(selenium.open("/" + url)) match {
case Full(_) => selenium.waitForPageToLoad("30000") //success
case Failure(_, Full(e: SeleniumException), _) => throw new LinkNotFoundException(e)
case _ => throw new LinkNotFoundException
}
}
def toLogout = {
val result = Helpers.tryo(selenium.click("link=Logout")) match {
case Full(_) => selenium
case Failure(_, Full(e: SeleniumException), _) => throw new LinkNotFoundException(e)
case _ => throw new LinkNotFoundException
}
new LoginPage(result)
}
protected def ajaxSubmitFunc() {
selenium.submit("submitForm")
}
def getOkMessages = selenium.getText("id=feedbackok")
def getErrorMessages = selenium.getText("id=feedbackerror")
def submitAndGetErrors = {
ajaxAndWait(ajaxSubmitFunc)
getErrorMessages
}
def submitAndGetOks = {
ajaxAndWait(ajaxSubmitFunc)
getOkMessages
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment