diff --git a/samples/mongo/build.gradle b/samples/mongo/build.gradle index 811c5d5..89cc687 100644 --- a/samples/mongo/build.gradle +++ b/samples/mongo/build.gradle @@ -30,8 +30,7 @@ dependencies { testCompile "org.springframework.boot:spring-boot-starter-test" - integrationTestCompile gebDependencies, - "org.spockframework:spock-spring:$spockVersion" + integrationTestCompile seleniumDependencies } @@ -39,9 +38,6 @@ integrationTest { doFirst { def port = reservePort() - def host = 'localhost:' + port - systemProperties['geb.build.baseUrl'] = 'http://'+host+'/' - systemProperties['geb.build.reportsDir'] = 'build/geb-reports' systemProperties['server.port'] = port systemProperties['management.port'] = 0 diff --git a/samples/mongo/src/integration-test/groovy/sample/BootTests.groovy b/samples/mongo/src/integration-test/groovy/sample/BootTests.groovy deleted file mode 100644 index 28cfa04..0000000 --- a/samples/mongo/src/integration-test/groovy/sample/BootTests.groovy +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2014-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package sample - -import geb.spock.* -import pages.* -import sample.pages.HomePage -import sample.pages.LoginPage -import spock.lang.Stepwise - -import org.springframework.boot.test.IntegrationTest -import org.springframework.boot.test.SpringApplicationContextLoader -import org.springframework.test.context.ContextConfiguration -import org.springframework.test.context.web.WebAppConfiguration - -/** - * Tests the demo that supports multiple sessions - * - * @author Rob Winch - */ -@Stepwise -@ContextConfiguration(classes = Application, loader = SpringApplicationContextLoader) -@WebAppConfiguration -@IntegrationTest -class BootTests extends GebReportingSpec { - - def 'Unauthenticated user sent to log in page'() { - when: 'unauthenticated user request protected page' - via HomePage - then: 'sent to the log in page' - at LoginPage - } - - def 'Log in views home page'() { - when: 'log in successfully' - login() - then: 'sent to original page' - at HomePage - and: 'the username is displayed' - username == 'user' - and: 'Spring Session Management is being used' - driver.manage().cookies.find { it.name == 'SESSION' } - and: 'Standard Session is NOT being used' - !driver.manage().cookies.find { it.name == 'JSESSIONID' } - } - - def 'Log out success'() { - when: - logout() - then: - at LoginPage - } - - def 'Logged out user sent to log in page'() { - when: 'logged out user request protected page' - via HomePage - then: 'sent to the log in page' - at LoginPage - } -} diff --git a/samples/mongo/src/integration-test/java/sample/BootTests.java b/samples/mongo/src/integration-test/java/sample/BootTests.java new file mode 100644 index 0000000..cd40559 --- /dev/null +++ b/samples/mongo/src/integration-test/java/sample/BootTests.java @@ -0,0 +1,104 @@ +/* + * Copyright 2014-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample; + +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openqa.selenium.By; +import org.openqa.selenium.Cookie; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import sample.pages.HomePage; +import sample.pages.LoginPage; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Pool Dolorier + */ +@RunWith(SpringRunner.class) +@AutoConfigureMockMvc +@SpringBootTest(webEnvironment = WebEnvironment.MOCK) +public class BootTests { + + @Autowired + private MockMvc mockMvc; + + private WebDriver driver; + + @Before + public void setUp() { + this.driver = MockMvcHtmlUnitDriverBuilder + .mockMvcSetup(this.mockMvc) + .build(); + } + + @After + public void tearDown() { + this.driver.quit(); + } + + @Test + public void unauthenticatedUserSentToLogInPage() { + HomePage homePage = HomePage.go(this.driver); + LoginPage loginPage = homePage.unauthenticated(); + loginPage.assertAt(); + } + + @Test + public void logInViewsHomePage() { + LoginPage loginPage = LoginPage.go(this.driver); + loginPage.assertAt(); + HomePage homePage = loginPage.login("user", "password"); + homePage.assertAt(); + WebElement username = homePage.getDriver().findElement(By.id("un")); + assertThat(username.getText()).isEqualTo("user"); + Set cookies = homePage.getDriver().manage().getCookies(); + assertThat(cookies).extracting("name").contains("SESSION"); + assertThat(cookies).extracting("name").doesNotContain("JSESSIONID"); + } + + @Test + public void logoutSuccess() { + LoginPage loginPage = LoginPage.go(this.driver); + HomePage homePage = loginPage.login("user", "password"); + LoginPage successLogoutPage = homePage.logout(); + successLogoutPage.assertAt(); + } + + @Test + public void loggedOutUserSentToLoginPage() { + LoginPage loginPage = LoginPage.go(this.driver); + HomePage homePage = loginPage.login("user", "password"); + homePage.logout(); + HomePage backHomePage = HomePage.go(this.driver); + LoginPage backLoginPage = backHomePage.unauthenticated(); + backLoginPage.assertAt(); + } +} diff --git a/samples/mongo/src/integration-test/groovy/sample/pages/LoginPage.groovy b/samples/mongo/src/integration-test/java/sample/pages/BasePage.java similarity index 55% rename from samples/mongo/src/integration-test/groovy/sample/pages/LoginPage.groovy rename to samples/mongo/src/integration-test/java/sample/pages/BasePage.java index ceeb790..3613a7f 100644 --- a/samples/mongo/src/integration-test/groovy/sample/pages/LoginPage.groovy +++ b/samples/mongo/src/integration-test/java/sample/pages/BasePage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,25 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package sample.pages -import geb.* +package sample.pages; + +import org.openqa.selenium.WebDriver; /** - * The Links Page - * - * @author Rob Winch + * @author Pool Dolorier */ -class LoginPage extends Page { - static url = '/login' - static at = { assert driver.title == 'Login Page'; true} - static content = { - form { $('form') } - submit { $('input[type=submit]') } - login(required:false) { user='user', pass='password' -> - form.username = user - form.password = pass - submit.click(HomePage) - } +public abstract class BasePage { + + private WebDriver driver; + + public BasePage(WebDriver driver) { + this.driver = driver; + } + + public WebDriver getDriver() { + return this.driver; + } + + public static void get(WebDriver driver, String get) { + String baseUrl = "http://localhost"; + driver.get(baseUrl + get); } } diff --git a/samples/mongo/src/integration-test/java/sample/pages/HomePage.java b/samples/mongo/src/integration-test/java/sample/pages/HomePage.java new file mode 100644 index 0000000..6a82ae2 --- /dev/null +++ b/samples/mongo/src/integration-test/java/sample/pages/HomePage.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Pool Dolorier + */ +public class HomePage extends BasePage { + + @FindBy(css = "input[type='submit']") + private WebElement submit; + + public HomePage(WebDriver driver) { + super(driver); + } + + public static HomePage go(WebDriver driver) { + get(driver, "/"); + return PageFactory.initElements(driver, HomePage.class); + } + + public LoginPage unauthenticated() { + return LoginPage.go(getDriver()); + } + + public LoginPage logout() { + this.submit.click(); + return LoginPage.go(getDriver()); + } + + public void assertAt() { + assertThat(getDriver().getTitle()).isEqualTo("Spring Session Sample - Secured Content"); + } +} diff --git a/samples/mongo/src/integration-test/java/sample/pages/LoginPage.java b/samples/mongo/src/integration-test/java/sample/pages/LoginPage.java new file mode 100644 index 0000000..5e8e102 --- /dev/null +++ b/samples/mongo/src/integration-test/java/sample/pages/LoginPage.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Pool Dolorier + */ +public class LoginPage extends BasePage { + + @FindBy(name = "username") + private WebElement username; + + @FindBy(name = "password") + private WebElement password; + + @FindBy(css = "input[name='submit']") + private WebElement submit; + + public LoginPage(WebDriver driver) { + super(driver); + } + + public static LoginPage go(WebDriver driver) { + get(driver, "/login"); + return PageFactory.initElements(driver, LoginPage.class); + } + + public void assertAt() { + assertThat(getDriver().getTitle()).isEqualTo("Login Page"); + } + + public HomePage login(String user, String password) { + this.username.sendKeys(user); + this.password.sendKeys(password); + this.submit.click(); + return HomePage.go(getDriver()); + } +} diff --git a/samples/rest/build.gradle b/samples/rest/build.gradle index ec8665d..20943fc 100644 --- a/samples/rest/build.gradle +++ b/samples/rest/build.gradle @@ -16,8 +16,8 @@ dependencies { providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion" testCompile "junit:junit:$junitVersion", - "org.springframework.security:spring-security-test:$springSecurityVersion" - - integrationTestCompile spockDependencies, - 'org.codehaus.groovy.modules.http-builder:http-builder:0.7' + "org.springframework.security:spring-security-test:$springSecurityVersion", + "org.assertj:assertj-core:$assertjVersion", + "org.springframework:spring-test:$springVersion", + "commons-codec:commons-codec:1.6" } \ No newline at end of file diff --git a/samples/rest/src/integration-test/groovy/sample/RestTests.groovy b/samples/rest/src/integration-test/groovy/sample/RestTests.groovy deleted file mode 100644 index 7ac4100..0000000 --- a/samples/rest/src/integration-test/groovy/sample/RestTests.groovy +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2014-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sample - -import groovyx.net.http.HttpResponseException -import groovyx.net.http.RESTClient -import spock.lang.Shared -import spock.lang.Specification -import spock.lang.Stepwise - -import javax.servlet.http.HttpServletResponse - -/** - * Ensures that Spring Security and Session are working - * - * @author Rob Winch - */ -@Stepwise -class RestTests extends Specification { - - @Shared - RESTClient client = new RESTClient(System.properties.'geb.build.baseUrl') - - @Shared - String session - - def 'Unauthenticated user sent to log in page'() { - when: 'unauthenticated user request protected page' - def resp = client.get path: '/', headers: ['Accept':'application/json'] - then: 'sent to the log in page' - def e = thrown(HttpResponseException) - e.response.status == HttpServletResponse.SC_UNAUTHORIZED - } - - def 'Authenticate with Basic Works'() { - when: 'Authenticate with Basic' - def username, response - client.get(path: '/', headers: ['Authorization': 'Basic ' + 'user:password'.bytes.encodeBase64() ]) { resp, json -> - response = resp - username = json.username - session = resp.headers.'x-auth-token' - } - then: 'Access the User information and obtain session via x-auth-token header' - response.status == HttpServletResponse.SC_OK - username == 'user' - session - } - - def 'Authenticate with x-auth-token works'() { - when: 'Authenticate with x-auth-token' - def username, response - client.get(path: '/', headers: ['x-auth-token': session ]) { resp, json -> - response = resp - username = json.username - } - then: 'Access the User information' - response.status == HttpServletResponse.SC_OK - username == 'user' - } - - def 'Logout'() { - when: 'invalide session' - def response - client.get(path: '/logout', headers: ['x-auth-token': session ]) { resp, json -> - response = resp - session = resp.headers.'x-auth-token' - } - then: 'The session is deleted and an empty x-auth-token is returned' - response.status == HttpServletResponse.SC_NO_CONTENT - session == '' - } -} diff --git a/samples/rest/src/integration-test/java/sample/RestTests.java b/samples/rest/src/integration-test/java/sample/RestTests.java new file mode 100644 index 0000000..dad95af --- /dev/null +++ b/samples/rest/src/integration-test/java/sample/RestTests.java @@ -0,0 +1,127 @@ +/* + * Copyright 2014-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample; + +import java.util.Arrays; + +import org.apache.commons.codec.binary.Base64; +import org.junit.Before; +import org.junit.Test; + +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Pool Dolorier + */ +public class RestTests { + + private static final String AUTHORIZATION = "Authorization"; + private static final String BASIC = "Basic "; + private static final String X_AUTH_TOKEN = "x-auth-token"; + + private RestTemplate restTemplate; + + private String baseUrl; + + @Before + public void setUp() { + this.baseUrl = "http://localhost:" + System.getProperty("tomcat.port"); + this.restTemplate = new RestTemplate(); + } + + @Test(expected = HttpClientErrorException.class) + public void unauthenticatedUserSentToLogInPage() { + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); + ResponseEntity entity = getForUser(this.baseUrl + "/", + headers, String.class); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); + } + + @Test + public void authenticateWithBasicWorks() { + String auth = getAuth("user", "password"); + HttpHeaders headers = getHttpHeaders(); + headers.set(AUTHORIZATION, BASIC + auth); + ResponseEntity entity = getForUser(this.baseUrl + "/", + headers, User.class); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(entity.getHeaders().containsKey(X_AUTH_TOKEN)).isTrue(); + assertThat(entity.getBody().getUsername()).isEqualTo("user"); + } + + @Test + public void authenticateWithXAuthTokenWorks() { + String auth = getAuth("user", "password"); + HttpHeaders headers = getHttpHeaders(); + headers.set(AUTHORIZATION, BASIC + auth); + ResponseEntity entity = getForUser(this.baseUrl + "/", + headers, User.class); + + String token = entity.getHeaders().getFirst(X_AUTH_TOKEN); + + HttpHeaders authTokenHeader = new HttpHeaders(); + authTokenHeader.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); + authTokenHeader.set(X_AUTH_TOKEN, token); + ResponseEntity authTokenResponse = getForUser(this.baseUrl + "/", + authTokenHeader, User.class); + assertThat(authTokenResponse.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(authTokenResponse.getBody().getUsername()).isEqualTo("user"); + } + + @Test + public void logout() { + String auth = getAuth("user", "password"); + HttpHeaders headers = getHttpHeaders(); + headers.set(AUTHORIZATION, BASIC + auth); + ResponseEntity entity = getForUser(this.baseUrl + "/", + headers, User.class); + + String token = entity.getHeaders().getFirst(X_AUTH_TOKEN); + + HttpHeaders logoutHeader = getHttpHeaders(); + logoutHeader.set(X_AUTH_TOKEN, token); + ResponseEntity logoutResponse = getForUser(this.baseUrl + "/logout", + logoutHeader, User.class); + assertThat(logoutResponse.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); + } + + private ResponseEntity getForUser(String resourceUrl, HttpHeaders headers, Class type) { + return this.restTemplate.exchange(resourceUrl, + HttpMethod.GET, new HttpEntity(headers), type); + } + + private HttpHeaders getHttpHeaders() { + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); + return headers; + } + + private String getAuth(String user, String password) { + String auth = user + ":" + password; + return new String(Base64.encodeBase64(auth.getBytes())); + } +} diff --git a/samples/security/src/integration-test/groovy/sample/pages/HomePage.groovy b/samples/rest/src/integration-test/java/sample/User.java similarity index 62% rename from samples/security/src/integration-test/groovy/sample/pages/HomePage.groovy rename to samples/rest/src/integration-test/java/sample/User.java index b98a276..9f4052e 100644 --- a/samples/security/src/integration-test/groovy/sample/pages/HomePage.groovy +++ b/samples/rest/src/integration-test/java/sample/User.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,20 +14,20 @@ * limitations under the License. */ -package sample.pages - -import geb.Page +package sample; /** - * The home page - * - * @author Rob Winch + * @author Pool Dolorier */ -class HomePage extends Page { - static url = '' - static at = { assert driver.title == 'Secured Content'; true} - static content = { - username { $('#un').text() } - logout(to:LoginPage) { $('input[type=submit]').click() } +public class User { + + private String username; + + public String getUsername() { + return this.username; + } + + public void setUsername(String username) { + this.username = username; } } diff --git a/samples/security/build.gradle b/samples/security/build.gradle index 03a0758..a4112a5 100644 --- a/samples/security/build.gradle +++ b/samples/security/build.gradle @@ -17,7 +17,9 @@ dependencies { providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion", "javax.servlet:jsp-api:$jspApiVersion" - testCompile "junit:junit:$junitVersion" + testCompile "junit:junit:$junitVersion", + "org.assertj:assertj-core:$assertjVersion", + "org.springframework:spring-test:$springVersion" - integrationTestCompile gebDependencies + integrationTestCompile seleniumDependencies } diff --git a/samples/security/src/integration-test/groovy/sample/SecurityTests.groovy b/samples/security/src/integration-test/groovy/sample/SecurityTests.groovy deleted file mode 100644 index 340fdc5..0000000 --- a/samples/security/src/integration-test/groovy/sample/SecurityTests.groovy +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2014-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sample - -import geb.spock.GebReportingSpec -import pages.* -import sample.pages.HomePage -import sample.pages.LoginPage -import spock.lang.Stepwise - -/** - * Ensures that Spring Security and Session are working - * - * @author Rob Winch - */ -@Stepwise -class SecurityTests extends GebReportingSpec { - - def 'Unauthenticated user sent to log in page'() { - when: 'unauthenticated user request protected page' - via HomePage - then: 'sent to the log in page' - at LoginPage - } - - def 'Log in views home page'() { - when: 'log in successfully' - login() - then: 'sent to original page' - at HomePage - and: 'the username is displayed' - username == 'user' - and: 'Spring Session Management is being used' - driver.manage().cookies.find { it.name == 'SESSION' } - and: 'Standard Session is NOT being used' - !driver.manage().cookies.find { it.name == 'JSESSIONID' } - } - - def 'Log out success'() { - when: - logout() - then: - at LoginPage - } - - def 'Logged out user sent to log in page'() { - when: 'logged out user request protected page' - via HomePage - then: 'sent to the log in page' - at LoginPage - } -} diff --git a/samples/security/src/integration-test/java/sample/SecurityTests.java b/samples/security/src/integration-test/java/sample/SecurityTests.java new file mode 100644 index 0000000..bc58220 --- /dev/null +++ b/samples/security/src/integration-test/java/sample/SecurityTests.java @@ -0,0 +1,88 @@ +/* + * Copyright 2014-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample; + +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.Cookie; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.htmlunit.HtmlUnitDriver; +import sample.pages.HomePage; +import sample.pages.LoginPage; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * + * @author Pool Dolorier + */ +public class SecurityTests { + + private WebDriver driver; + + @Before + public void setUp() { + this.driver = new HtmlUnitDriver(); + } + + @After + public void tearDown() { + this.driver.quit(); + } + + @Test + public void unauthenticatedUserSentToLogInPage() { + HomePage homePage = HomePage.go(this.driver); + LoginPage loginPage = homePage.unauthenticated(); + loginPage.assertAt(); + } + + @Test + public void logInViewsHomePage() { + LoginPage loginPage = LoginPage.go(this.driver); + HomePage homePage = loginPage.login("user", "password"); + homePage.assertAt(); + WebElement username = homePage.getDriver().findElement(By.id("un")); + assertThat(username.getText()).isEqualTo("user"); + Set cookies = homePage.getDriver().manage().getCookies(); + assertThat(cookies).extracting("name").contains("SESSION"); + assertThat(cookies).extracting("name").doesNotContain("JSESSIONID"); + } + + @Test + public void logOutSuccess() { + LoginPage loginPage = LoginPage.go(this.driver); + HomePage homePage = loginPage.login("user", "password"); + LoginPage successLogoutPage = homePage.logout(); + successLogoutPage.assertAt(); + } + + @Test + public void loggedOutUserSentToLoginPage() { + LoginPage loginPage = LoginPage.go(this.driver); + HomePage homePage = loginPage.login("user", "password"); + homePage.logout(); + HomePage backHomePage = HomePage.go(this.driver); + LoginPage backLoginPage = backHomePage.unauthenticated(); + backLoginPage.assertAt(); + } +} diff --git a/samples/security/src/integration-test/groovy/sample/pages/LoginPage.groovy b/samples/security/src/integration-test/java/sample/pages/BasePage.java similarity index 54% rename from samples/security/src/integration-test/groovy/sample/pages/LoginPage.groovy rename to samples/security/src/integration-test/java/sample/pages/BasePage.java index d8a5bea..661ff96 100644 --- a/samples/security/src/integration-test/groovy/sample/pages/LoginPage.groovy +++ b/samples/security/src/integration-test/java/sample/pages/BasePage.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,25 +14,28 @@ * limitations under the License. */ -package sample.pages +package sample.pages; -import geb.Page +import org.openqa.selenium.WebDriver; /** - * The Links Page * - * @author Rob Winch + * @author Pool Dolorier */ -class LoginPage extends Page { - static url = '/login' - static at = { assert driver.title == 'Login Page'; true} - static content = { - form { $('form') } - submit { $('input[type=submit]') } - login(required:false) { user='user', pass='password' -> - form.username = user - form.password = pass - submit.click(HomePage) - } +public abstract class BasePage { + + private WebDriver driver; + + public BasePage(WebDriver driver) { + this.driver = driver; + } + + public WebDriver getDriver() { + return this.driver; + } + + public static void get(WebDriver driver, String get) { + String baseUrl = "http://localhost:" + System.getProperty("tomcat.port"); + driver.get(baseUrl + get); } } diff --git a/samples/security/src/integration-test/java/sample/pages/HomePage.java b/samples/security/src/integration-test/java/sample/pages/HomePage.java new file mode 100644 index 0000000..b8a46cf --- /dev/null +++ b/samples/security/src/integration-test/java/sample/pages/HomePage.java @@ -0,0 +1,55 @@ +/* + * Copyright 2014-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Pool Dolorier + */ +public class HomePage extends BasePage { + + @FindBy(css = "input[type='submit']") + private WebElement button; + + public HomePage(WebDriver driver) { + super(driver); + } + + public static HomePage go(WebDriver driver) { + get(driver, "/"); + return PageFactory.initElements(driver, HomePage.class); + } + + public LoginPage unauthenticated() { + return LoginPage.go(getDriver()); + } + + public void assertAt() { + assertThat(getDriver().getTitle()).isEqualTo("Secured Content"); + } + + public LoginPage logout() { + this.button.click(); + return LoginPage.go(getDriver()); + } +} diff --git a/samples/security/src/integration-test/java/sample/pages/LoginPage.java b/samples/security/src/integration-test/java/sample/pages/LoginPage.java new file mode 100644 index 0000000..62db81b --- /dev/null +++ b/samples/security/src/integration-test/java/sample/pages/LoginPage.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Pool Dolorier + */ +public class LoginPage extends BasePage { + + @FindBy(name = "username") + private WebElement username; + + @FindBy(name = "password") + private WebElement password; + + @FindBy(css = "input[type='submit']") + private WebElement button; + + public LoginPage(WebDriver driver) { + super(driver); + } + + public static LoginPage go(WebDriver driver) { + get(driver, "/"); + return PageFactory.initElements(driver, LoginPage.class); + } + + public void assertAt() { + assertThat(getDriver().getTitle()).isEqualTo("Login Page"); + } + + public HomePage login(String user, String password) { + this.username.sendKeys(user); + this.password.sendKeys(password); + this.button.click(); + return HomePage.go(getDriver()); + } +} diff --git a/samples/users/build.gradle b/samples/users/build.gradle index 7edb6f8..c4873fb 100644 --- a/samples/users/build.gradle +++ b/samples/users/build.gradle @@ -14,7 +14,9 @@ dependencies { providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion" - testCompile "junit:junit:$junitVersion" + testCompile "junit:junit:$junitVersion", + "org.springframework:spring-test:$springVersion", + "org.assertj:assertj-core:$assertjVersion" - integrationTestCompile gebDependencies + integrationTestCompile seleniumDependencies } diff --git a/samples/users/src/integration-test/groovy/sample/UserTests.groovy b/samples/users/src/integration-test/groovy/sample/UserTests.groovy deleted file mode 100644 index dc16e84..0000000 --- a/samples/users/src/integration-test/groovy/sample/UserTests.groovy +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright 2014-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sample - -import geb.spock.* -import sample.pages.HomePage; -import sample.pages.LinkPage; -import spock.lang.Stepwise -import pages.* - -/** - * Tests the demo that supports multiple sessions - * - * @author Rob Winch - */ -@Stepwise -class UserTests extends GebReportingSpec { - def setup() { - browser.driver.javascriptEnabled = true - } - - def 'first visit not authenticated'() { - when: - to HomePage - then: - form - !username - } - - def 'invalid login'() { - setup: - def user = 'rob' - when: - login(user, user+'invalid') - then: - !username - error == 'Invalid username / password. Please ensure the username is the same as the password.' - } - - def 'empty username'() { - setup: - def user = '' - when: - login(user, user) - then: - !username - error == 'Invalid username / password. Please ensure the username is the same as the password.' - } - - def 'login single user'() { - setup: - def user = 'rob' - when: - login(user, user) - then: - username == user - } - - def 'add account'() { - when: - addAccount.click(HomePage) - then: - form - !username - } - - def 'log in second user'() { - setup: - def user = 'luke' - when: - login(user, user) - then: - username == user - } - - def 'following links keeps new session'() { - when: - navLink.click(LinkPage) - then: - username == 'luke' - } - - def 'switch account rob'() { - setup: - def user = 'rob' - when: - switchAccount(user) - then: - username == user - } - - def 'following links keeps original session'() { - when: - navLink.click(LinkPage) - then: - username == 'rob' - } - - def 'switch account luke'() { - setup: - def user = 'luke' - when: - switchAccount(user) - then: - username == user - } - - def 'logout luke'() { - when: - logout.click(HomePage) - then: - !username - } - - def 'switch back rob'() { - setup: - def user = 'rob' - when: - switchAccount(user) - then: - username == user - } - - def 'logout rob'() { - when: - logout.click(HomePage) - then: - !username - } -} diff --git a/samples/users/src/integration-test/groovy/sample/pages/HomePage.groovy b/samples/users/src/integration-test/groovy/sample/pages/HomePage.groovy deleted file mode 100644 index f1b34d8..0000000 --- a/samples/users/src/integration-test/groovy/sample/pages/HomePage.groovy +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2014-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sample.pages - -import geb.* - -/** - * The home page - * - * @author Rob Winch - */ -class HomePage extends Page { - static url = '' - static at = { assert driver.title == 'Demonstrates Multi User Log In'; true} - static content = { - navLink { $('#navLink') } - error { $('#error').text() } - form { $('form') } - username(required:false) { - $('#un').text() - } - userMenu() { - if(!$('#user-menu').displayed) { - $('#toggle').jquery.click() - } - waitFor { - $('#user-menu').displayed - } - } - logout(required:false) { - userMenu() - $('#logout') - } - addAccount(required:false) { - userMenu() - $('#addAccount') - } - submit { $('input[type=submit]') } - login(required:false) { user, pass -> - form.username = user - form.password = pass - submit.click(HomePage) - } - switchAccount{ un -> - userMenu() - $("#switchAccount${un}").click(HomePage) - } - attributes { moduleList AttributeRow, $("table tr").tail() } - } -} diff --git a/samples/users/src/integration-test/groovy/sample/pages/LinkPage.groovy b/samples/users/src/integration-test/groovy/sample/pages/LinkPage.groovy deleted file mode 100644 index c8c1995..0000000 --- a/samples/users/src/integration-test/groovy/sample/pages/LinkPage.groovy +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2014-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sample.pages - -import geb.* - -/** - * The Links Page - * - * @author Rob Winch - */ -class LinkPage extends Page { - static url = '' - static at = { assert driver.title == 'Linked Page'; true} - static content = { - form { $('#navLinks') } - username(required:false) { $('#un').text() } - userMenu() { - if(!$('#user-menu').displayed) { - $('#toggle').jquery.click() - } - waitFor { - $('#user-menu').displayed - } - } - switchAccount{ un -> - userMenu() - $("#switchAccount${un}").click(HomePage) - } - } -} diff --git a/samples/users/src/integration-test/java/sample/UserTests.java b/samples/users/src/integration-test/java/sample/UserTests.java new file mode 100644 index 0000000..c3f2ae2 --- /dev/null +++ b/samples/users/src/integration-test/java/sample/UserTests.java @@ -0,0 +1,236 @@ +/* + * Copyright 2014-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.htmlunit.HtmlUnitDriver; +import sample.pages.HomePage; +import sample.pages.LinkPage; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Pool Dolorier + */ +public class UserTests { + + private static final String ROB = "rob"; + private static final String USERNAME = "username"; + private static final String LUKE = "luke"; + private static final String NAV_LINK = "navLink"; + private static final String HREF = "href"; + private static final String ADD_ACCOUNT = "addAccount"; + private static final String UN = "un"; + private static final String LOGOUT = "logout"; + private static final String ERROR = "error"; + + private WebDriver driver; + + @Before + public void setup() { + this.driver = new HtmlUnitDriver(); + } + + @After + public void tearDown() { + this.driver.quit(); + } + + @Test + public void firstVisitNotAuthenticated() { + HomePage homePage = HomePage.go(this.driver); + homePage.assertAt(); + homePage.assertUserNameEmpty(); + } + + @Test + public void invalidLogin() { + HomePage homePage = HomePage.go(this.driver); + String user = ROB; + homePage.login(user, user + "invalid"); + WebElement errorMessage = homePage.getElementById(ERROR); + homePage.assertAt(); + homePage.assertErrorInvalidAuthentication(errorMessage); + } + + @Test + public void emptyUsername() { + HomePage homePage = HomePage.go(this.driver); + homePage.login("", ""); + WebElement errorMessage = homePage.getElementById(ERROR); + homePage.assertAt(); + homePage.assertErrorInvalidAuthentication(errorMessage); + } + + @Test + public void loginSingleUser() { + loginRob(); + } + + @Test + public void addAccount() { + backHomeForAddLukeAccount(); + } + + @Test + public void logInSecondUser() { + logInLukeAccount(); + } + + @Test + public void followingLinksKeepsNewSession() { + followingLukeLinkSession(); + + } + + @Test + public void switchAccountRob() { + switchAccountRobHomePage(); + } + + @Test + public void followingLinksKeepsOriginalSession() { + followingRobLinkSession(); + } + + @Test + public void switchAccountLuke() { + switchAccountLukeHomePage(); + } + + @Test + public void logoutLuke() { + logoutLukeAccount(); + } + + @Test + public void switchBackRob() { + switchBackRobHomePage(); + } + + @Test + public void logoutRob() { + logoutRobAccount(); + } + + private HomePage loginRob() { + HomePage home = HomePage.go(this.driver); + + String user = ROB; + home.login(user, user); + WebElement username = home.getElementById(UN); + assertThat(username.getText()).isEqualTo(user); + return home; + } + + private HomePage backHomeForAddLukeAccount() { + HomePage robHome = loginRob(); + + String addAccountLink = robHome.getContentAttributeByElementId(ADD_ACCOUNT, HREF); + HomePage backHome = robHome.home(this.driver, addAccountLink); + WebElement username = backHome.getElementById(USERNAME); + assertThat(username.getText()).isEmpty(); + return backHome; + } + + private HomePage logInLukeAccount() { + HomePage home = backHomeForAddLukeAccount(); + + String secondUser = LUKE; + home.login(secondUser, secondUser); + WebElement secondUserName = home.getElementById(UN); + assertThat(secondUserName.getText()).isEqualTo(secondUser); + return home; + } + + private LinkPage followingLukeLinkSession() { + HomePage lukeHome = logInLukeAccount(); + + String navLink = lukeHome.getContentAttributeByElementId(NAV_LINK, HREF); + LinkPage lukeLinkPage = lukeHome.linkPage(this.driver, navLink); + lukeLinkPage.assertAt(); + WebElement username = lukeLinkPage.getElementById(UN); + assertThat(username.getText()).isEqualTo(LUKE); + return lukeLinkPage; + } + + private HomePage switchAccountRobHomePage() { + LinkPage lukeLinkPage = followingLukeLinkSession(); + + String robSwitch = lukeLinkPage.getSwitchElementId(ROB); + String switchLink = lukeLinkPage.getContentAttributeByElementId(robSwitch, HREF); + HomePage robHome = lukeLinkPage.home(this.driver, switchLink); + WebElement username = robHome.getElementById(UN); + assertThat(username.getText()).isEqualTo(ROB); + return robHome; + } + + private LinkPage followingRobLinkSession() { + HomePage robHome = switchAccountRobHomePage(); + + String navLink = robHome.getContentAttributeByElementId(NAV_LINK, HREF); + LinkPage robLinkPage = robHome.linkPage(this.driver, navLink); + robLinkPage.assertAt(); + WebElement username = robLinkPage.getElementById(UN); + assertThat(username.getText()).isEqualTo(ROB); + return robLinkPage; + } + + private HomePage switchAccountLukeHomePage() { + LinkPage robLinkPage = followingRobLinkSession(); + + String lukeSwitch = robLinkPage.getSwitchElementId(LUKE); + String lukeSwitchLink = robLinkPage.getContentAttributeByElementId(lukeSwitch, HREF); + HomePage lukeHome = robLinkPage.home(this.driver, lukeSwitchLink); + WebElement username = lukeHome.getElementById(UN); + assertThat(username.getText()).isEqualTo(LUKE); + return lukeHome; + } + + private HomePage logoutLukeAccount() { + HomePage lukeHome = switchAccountLukeHomePage(); + + String logoutLink = lukeHome.getContentAttributeByElementId(LOGOUT, HREF); + HomePage home = lukeHome.home(this.driver, logoutLink); + home.assertUserNameEmpty(); + return home; + } + + private HomePage switchBackRobHomePage() { + HomePage homePage = logoutLukeAccount(); + + String robSwitch = homePage.getSwitchElementId(ROB); + String robSwitchLink = homePage.getContentAttributeByElementId(robSwitch, HREF); + HomePage robHome = homePage.home(this.driver, robSwitchLink); + WebElement username = robHome.getElementById(UN); + assertThat(username.getText()).isEqualTo(ROB); + return robHome; + } + + private HomePage logoutRobAccount() { + HomePage robHome = switchBackRobHomePage(); + + String logoutLink = robHome.getContentAttributeByElementId(LOGOUT, HREF); + HomePage home = robHome.home(this.driver, logoutLink); + home.assertUserNameEmpty(); + return home; + } +} diff --git a/samples/users/src/integration-test/java/sample/pages/BasePage.java b/samples/users/src/integration-test/java/sample/pages/BasePage.java new file mode 100644 index 0000000..c141a49 --- /dev/null +++ b/samples/users/src/integration-test/java/sample/pages/BasePage.java @@ -0,0 +1,74 @@ +/* + * Copyright 2014-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.pages; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Pool Dolorier + */ +public abstract class BasePage { + + private WebDriver driver; + + public BasePage(WebDriver driver) { + this.driver = driver; + } + + public WebDriver getDriver() { + return this.driver; + } + + public static void get(WebDriver driver, String get) { + String baseUrl = "http://localhost:" + System.getProperty("tomcat.port"); + driver.get(baseUrl + get); + } + + public static void getFrom(WebDriver driver, String resourceUrl) { + driver.get(resourceUrl); + } + + public HomePage home(WebDriver driver, String resourceUrl) { + getFrom(driver, resourceUrl); + return PageFactory.initElements(driver, HomePage.class); + } + + public LinkPage linkPage(WebDriver driver, String resourceUrl) { + getFrom(driver, resourceUrl); + return PageFactory.initElements(driver, LinkPage.class); + } + + public String getSwitchElementId(String user) { + return "switchAccount" + user; + } + + public WebElement getElementById(String id) { + return this.driver.findElement(By.id(id)); + } + + public String getContentAttributeByElementId(String id, String attribute) { + WebElement element = getElementById(id); + assertThat(element.getAttribute(attribute)).isNotEmpty(); + return element.getAttribute(attribute); + } + +} diff --git a/samples/users/src/integration-test/java/sample/pages/HomePage.java b/samples/users/src/integration-test/java/sample/pages/HomePage.java new file mode 100644 index 0000000..a994bdd --- /dev/null +++ b/samples/users/src/integration-test/java/sample/pages/HomePage.java @@ -0,0 +1,66 @@ +/* + * Copyright 2014-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sample.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Pool Dolorier + */ +public class HomePage extends BasePage { + + @FindBy(name = "username") + private WebElement username; + + @FindBy(name = "password") + private WebElement password; + + @FindBy(css = "form[method='post']") + private WebElement form; + + public HomePage(WebDriver driver) { + super(driver); + } + + public static HomePage go(WebDriver driver) { + get(driver, "/"); + return PageFactory.initElements(driver, HomePage.class); + } + + public void assertAt() { + assertThat(getDriver().getTitle()).isEqualTo("Demonstrates Multi User Log In"); + } + + public void assertUserNameEmpty() { + assertThat(this.username.getText()).isEmpty(); + } + + public void assertErrorInvalidAuthentication(WebElement errorMessage) { + assertThat(errorMessage.getText()).isEqualTo("Invalid username / password. Please ensure the username is the same as the password."); + } + + public void login(String user, String password) { + this.username.sendKeys(user); + this.password.sendKeys(password); + this.form.submit(); + } +} diff --git a/samples/mongo/src/integration-test/groovy/sample/pages/HomePage.groovy b/samples/users/src/integration-test/java/sample/pages/LinkPage.java similarity index 59% rename from samples/mongo/src/integration-test/groovy/sample/pages/HomePage.groovy rename to samples/users/src/integration-test/java/sample/pages/LinkPage.java index 867b81a..1d27240 100644 --- a/samples/mongo/src/integration-test/groovy/sample/pages/HomePage.groovy +++ b/samples/users/src/integration-test/java/sample/pages/LinkPage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,20 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package sample.pages -import geb.* +package sample.pages; + +import org.openqa.selenium.WebDriver; + +import static org.assertj.core.api.Assertions.assertThat; /** - * The home page - * - * @author Rob Winch + * @author Pool Dolorier */ -class HomePage extends Page { - static url = '' - static at = { assert driver.title == 'Spring Session Sample - Secured Content'; true} - static content = { - username { $('#un').text() } - logout(to:LoginPage) { $('input[type=submit]').click() } +public class LinkPage extends BasePage { + + public LinkPage(WebDriver driver) { + super(driver); + } + + public void assertAt() { + assertThat(getDriver().getTitle()).isEqualTo("Linked Page"); } }