Aayush Tuladhar
spock.lang.Specification
class MyFirstSpec extends Specification {
// fields
// feature methods
// fixture methods
// helper methods
}
def "descriptive name"(){
//blocks
}
setup:
given:
when: // Stimulus
then: // Response
expect: // Response
cleanup:
// Run Before Every Feature Method
def setup(){}
// Run After Every Feature Method
def cleanup(){}
// Run Before the First Feature Method
def setupSpec(){}
// Run After the Last Feature Method
def cleanupSpec(){}
Spock | Junit |
---|---|
setup |
setUp , @Before |
cleanup |
tearDown , @After |
setupSpec |
@BeforeClass |
cleanupSpec |
@AfterClass |
$("#submitBtn").click();
assert title == 'Hello World'
src/test/resources/GebConfig.groovy
driver
baseUrl
reportsDir
reportOnTestFailureOnly
import org.openqa.selenium.chrome.ChromeDriver
def final String DRIVER = 'src/test/resources/driver/chromedriver'
System.setProperty('webdriver.chrome.driver', DRIVER);
driver = { new ChromeDriver() }
baseUrl = "http://localhost:3000/"
reportsDir = new File("target/geb-reports")
reportOnTestFailureOnly = true
src/test/groovy/specs/FirstSpec.groovy
package specs
import geb.spock.GebReportingSpec
class HomePageSpec extends GebReportingSpec{
def "User Should be able to visit Digi Marketplace Homepage"(){
when: "User enters Digi Marketplace Website"
go "http://digimarketplace.aayushtuladhar.com/"
then: "User should be able to see top Navbar"
$("a.navbar-brand").text() == "Digi Marketplace"
then: "User should be able to see Digi Marketplace Jumbotrom"
$("h2").text() == "Digi Marketplace"
}
}
go()
- Using go method, you can make a new URL request# Make a direct URL request
go "http://www.google.com"
# Go to URL relative to base URL
go "signup"
# Go to URL with request parameter
go "signup", param1: "value1", param2: "value2"
$()
- Interacting with Content$()
function is the access point to the browser's page content.$(css_selector, index_or_range, attribute)
// CSS Selector
$("h1", class: heading1)
// Index and Ranges
$("p", 0).text() == "Hello World"
// Attribute and Text Matching
$("p", attr1: "a").size() == 1
// Finding and Filtering Descendants
$("div").find(".heroElement")
$("div").has("elments")
// Clicking Elements
$("a.help").click()
// Form Values
$("input#userName").value() == "Hello World"
$("input#userEmail").value("helloWorld@gmail.com")
# Select Button
$("form").state="MN"
# Checkbox
$("form").isRockStar = true