//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) def cachedDriver = CachingDriverFactory.clearCacheAndQuitDriver() // clean-up old window & driver if any :) File path = new File("C:\\geb-test\\") path.mkdirs() String searchFor = "dogs" // or null = off String downloadImgURL = 'http://freemailtemplates.com/wp-content/themes/psdfiles-freemailtemplates/images/download-button.png' // or null = off String downloadImgPathFile = path.absolutePath+'\\pic.png' String screenshotURL = 'http://www.yahoo.com' // or null = off String screenshotPathFile = path.absolutePath+'\\screenshot.png' // NOTE: these two webdrivers ONLY init'ed for the URL page screenshots (which need a headless/remote browser like phantomjs) // // you could probably just go to the URL and generate a report, too...? // final RemoteWebDriver rdriver final WebDriver augmentedDriver if (screenshotURL) { try { rdriver = new RemoteWebDriver( "http://localhost:9134".toURL(), DesiredCapabilities.firefox() ) augmentedDriver = new Augmenter().augment(rdriver) } catch (Exception e) { screenshotURL = null } } // NOTE: config ONLY init'ed for reporting purposes // def config = new Browser().getConfig() // steal a default config(??) config.reportsDir = path try{ Browser.drive(config) { if (searchFor) { go "http://www.google.ca/" assert title.contains("Google") // basic searching/reading/etc //////////////////////////////////////////// def inputs = $("input, button") assert inputs.size() > 2 inputs.each { println ":"+it.value() } // try a real Google search! //////////////////////////////////////////// def textSearch = $("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 = $("img") def srcs = imgs*.@src srcs.each { src -> // show the img sources println "img: " + (src ?: 'n/a') } } // try another page! //////////////////////////////////////////// go "http://cnn.com" assert title.contains("CNN.com") println ">>>" + title // try to dump the page's HTML and screenshot! //////////////////////////////////////////// reportGroup "cnn" report "homepage" // try to inject jQuery into the page! //////////////////////////////////////////// injectLibrary(js, 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js') //////////////////////////////////////////// js."jQuery.noConflict"() // try to grab the body's raw HTML! //////////////////////////////////////////// String body = ''+js."document.body.innerHTML"+'' // try a URL file download! //////////////////////////////////////////// if (downloadImgURL) { try { downloadFile(browser, downloadImgURL, downloadImgPathFile) } catch (Exception e) { println "[ERROR] downloading: ${e.message}\n${e.printStackTrace()}" } } // try a URL page screenshot! //////////////////////////////////////////// if (screenshotURL) { try { screenshot(augmentedDriver, screenshotURL, screenshotPathFile) } catch(Exception e) { println "[ERROR] screenshot: ${e.message}\n${e.printStackTrace()}" } } 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==// //these util methods all get their context object passed in as a final// def downloadFile(final Browser browser, String downloadImgURL, String downloadImgPathFile) { input = browser.downloadStream(downloadImgURL) // geb function output = new BufferedOutputStream(new FileOutputStream(downloadImgPathFile)) output << input } def screenshot(final WebDriver driver, String url, String filename) { /********************************************************* * 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') } def injectLibrary(final js, String library){ if (!js."document.body.innerHTML".contains(library)) js.exec("document.body.appendChild(document.createElement('script')).src='$library'"); //assert js."document.body.innerHTML".contains(library) }
Tuesday, February 7, 2012
Geb Web Test
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment