Search code examples
intellij-ideageb

Running a GEB test using Intellij


Being a beginner in GEB testing, I am trying to run a simple login program in Intellij. Could you please help me run this test in Intellij? My question is what selections should I make in the edit configurations page? Please help. This example is from the book of geb.

import geb.Browser

Browser.drive {
  go "http://google.com/ncr"

  // make sure we actually got to the page
  assert title == "Google"

  // enter wikipedia into the search field
  $("input", name: "q").value("wikipedia")

  // wait for the change to results page to happen
  // (google updates the page dynamically without a new request)
  waitFor { title.endsWith("Google Search") }

  // is the first link to wikipedia?
  def firstLink = $("li.g", 0).find("a.l")
  assert firstLink.text() == "Wikipedia"

  // click the link 
  firstLink.click()

  // wait for Google's javascript to redirect to Wikipedia
  waitFor { title == "Wikipedia" }
}

Solution

  • If you are running this in IntelliJ you should be able to run this as a JUnit test (ctrl+F10). Make sure that this is inside of a Class and in a method.

    For ease of syntax, it would be good to use Spock as your BDD framework (include the library in your project; if using Maven, follow the guide on the site but update to Spock 0.7-groovy-2.0 and Geb 0.9.0-RC-1 for the latest libraries

    If you want to switch from straight JUnit to Spock (keep in mind you should use JUnit as a silent library) then your test case looks like this:

      def "show off the awesomeness of google"() {
        given:
        go "http://google.com/ncr"
    
        expect: "make sure we actually got to the page"
        title == "Google"
    
        when: "enter wikipedia into the search field"
        $("input", name: "q").value("wikipedia")
    
        then: "wait for the change to results page to happen and (google updates the page dynamically without a new request)"
        waitFor { title.endsWith("Google Search") }
        // is the first link to wikipedia?
        def firstLink = $("li.g", 0).find("a.l")
    
        and:
        firstLink.text() == "Wikipedia"
    
        when: "click the link"
        firstLink.click()
    
        then: "wait for Google's javascript to redirect to Wikipedia"
        waitFor { title == "Wikipedia" }
    }
    

    Just remember: Ctrl + F10 (best key shorcut for a test in IntelliJ!)