0

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" }
}
Gergely Toth
  • 6,638
  • 2
  • 38
  • 40
Dee
  • 223
  • 5
  • 11

2 Answers2

2

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!)

mensor
  • 190
  • 8
0

The above is close but no cigar, so to speak.

If you want to run bulk standard Gebish test from WITHIN Intellij, I tried 2 things.

  1. I added my geckodriver.exe to the test/resources under my Spock/Geb tests
  2. I literally in the given: part of my Spok/Geb test did the following:

        given:  
    System.setProperty("webdriver.gecko.driver", "C:\\repo\\geb-example-gradle\\src\\test\\resources" + "\\geckodriver.exe");
    
    1. Failed
    2. Succeeded

Now the usual deal with answers is, that someone writes something, you try it and then it fails. So, if it did not work for you, use the reference Geb/Spock project on Github as follows and import it into intellij (remember, New Project, then find the gradle.build script and then intellij will import it nicely)...it also kicks off a build so dont freak out:

  1. https://github.com/geb/geb-example-gradle
  2. Download the driver: https://github.com/mozilla/geckodriver/releases and move it to the test/resource folder of the reference project you just imported under test/groovy...(see image)

Now add the above given: clause to the GebishOrgSpec Sock/Geb test: Configure the geckodriver.exe in a Spock/Geb test

The test runs nicely from WITHIN Intellij. Evidence the browser open and the test running:

enter image description here

LOVELY JOBBLY :=)

Beezer
  • 1,084
  • 13
  • 18