Skip to content

Instantly share code, notes, and snippets.

@david-w-millar
Forked from crazy4groovy/geb-script.groovy
Created May 15, 2019 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-w-millar/acf8eed7ac0c237685d24ffc905da654 to your computer and use it in GitHub Desktop.
Save david-w-millar/acf8eed7ac0c237685d24ffc905da654 to your computer and use it in GitHub Desktop.
A gentle intro script for the Geb setup you'll need to do basic web testing/scraping. (browse, navigate, click, download, screenshot)
//updated Jan 12 2013
//check out Geb/Phantomjs integration via RemoteWebDriver support!
@Grapes([
@Grab("junit:junit-dep:4.8.2"),
@Grab("org.codehaus.geb:geb-junit4:0.7.2"), //soon to go 0.9.0
@Grab("org.seleniumhq.selenium:selenium-firefox-driver:2.28.0"),
@Grab("org.seleniumhq.selenium:selenium-remote-driver:2.28.0"),
@Grab("org.seleniumhq.selenium:selenium-support:2.28.0")
])
import geb.*
import geb.driver.CachingDriverFactory
import org.openqa.selenium.*
import org.openqa.selenium.remote.*
//import org.openqa.selenium.firefox.FirefoxProfile
//import org.openqa.selenium.firefox.FirefoxDriver
//import java.awt.Robot // see http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html#createScreenCapture(java.awt.Rectangle)
String searchFor = "cats"
String downloadImgURL = 'http://freemailtemplates.com/wp-content/themes/psdfiles-freemailtemplates/images/download-button.png' // or null = off
String downloadImgPathFile = 'C:\\pic.png'
String screenshotURL = 'http://www.yahoo.com' // or null = off
String screenshotPathFile = 'C:\\screenshot.png'
// these two drivers ONLY init'ed for the screenshots (which need a headless/remote browser like phantomjs) //
final RemoteWebDriver rdriver
final WebDriver augmentedDriver
if (screenshotURL) {
rdriver = new RemoteWebDriver( "http://localhost:9134".toURL(), DesiredCapabilities.firefox() )
augmentedDriver = new Augmenter().augment(rdriver)
}
def cachedDriver = CachingDriverFactory.clearCacheAndQuitDriver() // clean-up old window & driver if any :)
try{
Browser.drive {
go "http://www.google.ca/"
assert title.contains("Google")
// basic searching/reading/etc ////////////////////////////////////////////
def inputs = waitFor { $("input, button") }
assert inputs.size() > 2
inputs.each {
println ":"+it.value()
}
// try a real Google search! ////////////////////////////////////////////
def textSearch = waitFor { $("form[action='/search'] input[type='text']") }
assert textSearch.size() == 1
textSearch.value( searchFor )
def buttonSearch = waitFor { $("input[type='button'], button") }.findAll{ it.text().toLowerCase().contains("search") }
if (buttonSearch.size() == 0)
// try another DOM node search //
buttonSearch = waitFor { $("input[type='button']#gbqfb, button#gbqfb", 0) }
assert buttonSearch.size() == 1
buttonSearch.click()
def items = waitFor { $("li.g") }
// show the first item's text
println items[0].text()
def imgs = waitFor { $("img") }
def srcs = imgs*.@src
srcs.each { src ->
// show the img sources
println "img: " + (src ?: 'n/a')
}
// try another page! ////////////////////////////////////////////
go "http://cnn.com"
println ">>>" + title
assert title.contains("CNN.com")
// try a file download! ////////////////////////////////////////////
if (downloadImgURL)
use (FileBinaryCategory) { new File (downloadImgPathFile) << downloadImgURL.toURL() }
// try a page screenshot! ////////////////////////////////////////////
try {
if (screenshotURL)
screenshot(screenshotURL, screenshotPathFile, augmentedDriver)
}
catch(Exception e) { println "[ERROR] screenshot: ${e.message}" }
println '==Done=='
//close() // close window, not neccesary with CachingDriverFactory.clearCacheAndQuitDriver()
//quit() // quit window, not neccesary with CachingDriverFactory.clearCacheAndQuitDriver()
}
cachedDriver = CachingDriverFactory.clearCacheAndQuitDriver() // clean-up window & driver if successful :)
}
catch(Exception e) { println "[ERROR] Please try again: ${e.message}" }
//==END MAIN==//
def screenshot(String url, String filename, final WebDriver driver) {
/*********************************************************
* Note: you need a headless/remote web server running (like phantomjs)
* - beware, this doesn't seem to like HTTP redirects
*********************************************************/
driver.get(url)
final File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE)
scrFile.renameTo(filename ?: 'screenshot.png')
}
class FileBinaryCategory {
def static leftShift(File a_file, URL a_url) {
def input, output
try {
input = a_url.openStream()
output = new BufferedOutputStream(new FileOutputStream(a_file))
output << input
}
finally {
input?.close()
output?.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment