Skip to content

Instantly share code, notes, and snippets.

@pbernet
Created October 23, 2012 16:14
Show Gist options
  • Save pbernet/3939784 to your computer and use it in GitHub Desktop.
Save pbernet/3939784 to your computer and use it in GitHub Desktop.
Browser
package xx.testcase.gui
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.WebDriverBackedSelenium
import org.openqa.selenium.remote.RemoteWebDriver
import org.openqa.selenium.firefox.internal.ProfilesIni
import java.io.File
import net.liftweb.util.Helpers
object Browser {
val downloadFolder = System.getProperty("java.io.tmpdir")+"archivebox"
def openTestAndClose[T](url: String = "http://10.1.2.127")(func: WebDriverBackedSelenium => T): T = {
val selenium = createSeleniumInstance(url)
val res = func(selenium)
selenium.stop()
res
}
def openAndTest(url: String = "http://10.1.2.127"): WebDriverBackedSelenium = {
createSeleniumInstance(url)
}
private def createSeleniumInstance(url: String) = {
val browserProfile = "selenium"
val allProfiles = new ProfilesIni()
System.setProperty("webdriver.firefox.profile", browserProfile)
val profile = allProfiles.getProfile(browserProfile)
if (profile == null) {
throw new RuntimeException("Please add the empty firefox profile with the name: " + browserProfile + ". Execute in cmd shell: firefox.exe -ProfileManager -no-remote")
}
//set up download folder
deleteDownloadFolder()
profile.setAcceptUntrustedCertificates(true)
profile.setPreference("browser.download.folderList", 2)
profile.setPreference("browser.download.manager.showWhenStarting", false)
profile.setPreference("browser.download.dir", downloadFolder)
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv," +
"application/gzip," +
"application/gzip-compressed," +
"application/gzipped," +
"application/octet-stream," +
"application/x-compress," +
"application/x-compressed," +
"application/x-gunzip," +
"application/x-gzip," +
"application/x-gzip-compressed," +
"image/xcf")
val driver = new FirefoxDriver(profile)
//full screen
//driver.manage().window().maximize()
val selenium = new WebDriverBackedSelenium(driver, url)
selenium.open("/")
selenium
}
def deleteDownloadFolder() {
//set up download folder
val folder = new File(downloadFolder)
if (folder.exists() && folder.isDirectory) {
folder.listFiles().foreach(_.delete())
} else {
Helpers.tryo(folder.mkdirs())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment