im.yagni.driveby.driver.webdriver.WebDriverDriver.scala Maven / Gradle / Ivy
The newest version!
package im.yagni.driveby.driver.webdriver
import im.yagni.driveby.driver.NakedDriver
import im.yagni.driveby.{Id, By}
import org.apache.commons.io.FileUtils
import org.openqa.selenium.{JavascriptExecutor, OutputType, TakesScreenshot, WebDriver}
import collection.JavaConversions._
import java.io.File
case class WebDriverDriver(webDriver: WebDriver) extends NakedDriver {
def goto(url: String) { webDriver.get(url) }
def title = webDriver.getTitle
def html = webDriver.getPageSource
def close() { webDriver.close() }
def findElements(by: By) = webDriver.findElements(convertBy(by)).map(WebDriverElement(_))
def refresh() { webDriver.get(webDriver.getCurrentUrl) }
def screenshot(file: File) {
try {
if (webDriver.isInstanceOf[TakesScreenshot]) FileUtils.writeByteArrayToFile(file, webDriver.asInstanceOf[TakesScreenshot].getScreenshotAs(OutputType.BYTES))
} catch {
case e: Exception => println("unable to takescreenshot: " + e.getMessage)
}
}
def maximise() {
val javascript = webDriver.asInstanceOf[JavascriptExecutor]
javascript.executeScript("window.moveTo(0, 0);")
javascript.executeScript("window.resizeTo(screen.availWidth, screen.availHeight);")
}
private def convertBy(by: By) = {
by match {
case Id(id) => org.openqa.selenium.By.id(id)
case _ => throw new RuntimeException("Unsupported By for this driver")
}
}
}