Today we are going to learn how to use Geb in Cucumber functional automated testing? If you are new to Cucumber than please go through my blog.
Cucumber Basic concept
Fist-of-all if you want use Geb in your application than add these lines in BuildConfig.groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[php] def gebVersion = "0.7.0" dependencies { compile "org.spockframework:spock-grails-support:0.7-groovy-2.0" compile "org.gebish:geb-spock:0.9.2" compile "org.codehaus.geb:geb-spock:$gebVersion" } plugins { compile(":spock:0.7") { exclude "spock-grails-support" } compile ":geb:0.9.2" } [/php] |
and create a GebConfig.groovy in test/functional folder and create driver instance in that like
1 2 3 4 5 6 7 8 |
[php] import org.openqa.selenium.firefox.FirefoxDriver driver = { def driverInstance = new FirefoxDriver() driverInstance.manage().window().maximize() } [/php] |
Now create a Groovy script like env.groovy in support folder like test/cucumber/support and add these lines of code to open browser and initialize URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[php] import geb.binding.BindingUpdater import geb.Browser import static cucumber.api.groovy.Hooks.* Before (){ bindingUpdater = new BindingUpdater (binding, new Browser ()) bindingUpdater.initialize () } After () { bindingUpdater.remove () } [/php] |
Create a feature file as TestingPage.feature
1 2 3 4 5 6 7 8 |
[php] Feature: Logging in to Application Scenario: Successful login as a user Given I go to the start page When I enter username "user1" and password "user123" Then I am logged in [/php] |
Now create a folder page in test/functional and create TestingPage.groovy and add these lines of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[php] class TestingPage extends Page{ static URL = "auth/login" static content = { userName{$('#j_username')} pass{$('#j_password')} login{$('#loginButton')} } def login(String username, String password){ userName = username pass = password login.click() } } [/php] |
We define static URL because it tells bowser to open different URL and inside static content we define all the fields which will be interacted by browser.We have defined a login method which will be called for login by TestingPage_step
Now define TestingPage_step in cucumber folder and add these lines of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
[php] import static cucumber.api.groovy.EN.Given import static cucumber.api.groovy.EN.Then import static cucumber.api.groovy.EN.When Given(~'^I go to the start page There is no need to create an instance of browser and use findby method of selenium. While execution application will go to TestingPage and use all field to login. ) { -> to TestingPage at TestingPage } When(~'^I enter username "([^"]*)" and password "([^"]*)" There is no need to create an instance of browser and use findby method of selenium. While execution application will go to TestingPage and use all field to login. ) { String username, String password -> page.login(username,password) } Then(~'^I am logged in There is no need to create an instance of browser and use findby method of selenium. While execution application will go to TestingPage and use all field to login. ) { -> assert true } [/php] |
There is no need to create an instance of browser and use findby method of selenium. While execution
application will go to TestingPage and use all field to login.
Recent Comments