Skip to content

Commit 1a318b8

Browse files
eddumelendezjxblum
authored andcommitted
Convert groovy tests to java
1 parent f986974 commit 1a318b8

File tree

97 files changed

+3029
-1820
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+3029
-1820
lines changed

gradle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ spockVersion=1.0-groovy-2.4
2828
webjarsTaglibVersion=0.3
2929
jstlVersion=1.2.1
3030
groovyVersion=2.4.4
31+
lombokVersion=1.16.8

gradle/java.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ ext.gebDependencies = spockDependencies + [
2828
"org.codehaus.groovy:groovy:$groovyVersion"
2929
]
3030

31+
ext.seleniumDependencies = [
32+
"org.seleniumhq.selenium:selenium-htmlunit-driver:$seleniumVersion",
33+
"org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"
34+
]
35+
3136
ext.jstlDependencies = [
3237
"javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:$jstlVersion",
3338
"org.apache.taglibs:taglibs-standard-jstlel:1.2.1"

gradle/tomcat.gradle

+1-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ task integrationTomcatStop(type: com.bmuschko.gradle.tomcat.tasks.TomcatStop) {
4545
integrationTest {
4646
dependsOn integrationTomcatRun
4747
doFirst {
48-
def host = 'localhost:' + integrationTomcatRun.httpPort
49-
systemProperties['geb.build.baseUrl'] = 'http://'+host+'/' + integrationTomcatRun.contextPath
50-
systemProperties['geb.build.reportsDir'] = 'build/geb-reports'
48+
systemProperties['tomcat.port'] = integrationTomcatRun.httpPort
5149
}
5250
finalizedBy integrationTomcatStop
5351
}

samples/boot/build.gradle

+1-5
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,14 @@ dependencies {
2929

3030
testCompile "org.springframework.boot:spring-boot-starter-test"
3131

32-
integrationTestCompile gebDependencies,
33-
"org.spockframework:spock-spring:$spockVersion"
32+
integrationTestCompile seleniumDependencies
3433

3534
}
3635

3736
integrationTest {
3837
doFirst {
3938
def port = reservePort()
4039

41-
def host = 'localhost:' + port
42-
systemProperties['geb.build.baseUrl'] = 'http://'+host+'/'
43-
systemProperties['geb.build.reportsDir'] = 'build/geb-reports'
4440
systemProperties['server.port'] = port
4541
systemProperties['management.port'] = 0
4642

samples/boot/src/integration-test/groovy/sample/BootTests.groovy

-75
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2014-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import org.junit.After;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.openqa.selenium.WebDriver;
24+
import sample.pages.HomePage;
25+
import sample.pages.LoginPage;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
31+
import org.springframework.test.context.junit4.SpringRunner;
32+
import org.springframework.test.web.servlet.MockMvc;
33+
import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder;
34+
35+
/**
36+
* @author Eddú Meléndez
37+
*/
38+
@RunWith(SpringRunner.class)
39+
@AutoConfigureMockMvc
40+
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
41+
public class BootTests {
42+
43+
@Autowired
44+
private MockMvc mockMvc;
45+
46+
private WebDriver driver;
47+
48+
@Before
49+
public void setup() {
50+
this.driver = MockMvcHtmlUnitDriverBuilder
51+
.mockMvcSetup(this.mockMvc)
52+
.build();
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
this.driver.quit();
58+
}
59+
60+
@Test
61+
public void home() {
62+
LoginPage login = HomePage.go(this.driver);
63+
login.assertAt();
64+
}
65+
66+
@Test
67+
public void login() {
68+
LoginPage login = HomePage.go(this.driver);
69+
HomePage home = login.login();
70+
home.assertAt();
71+
home.containCookie("SESSION");
72+
home.doesNotContainCookie("JSESSIONID");
73+
}
74+
75+
@Test
76+
public void logout() {
77+
LoginPage login = HomePage.go(this.driver);
78+
HomePage home = login.login();
79+
home.logout();
80+
login.assertAt();
81+
}
82+
83+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,20 +14,28 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sample.pages
17+
package sample.pages;
1818

19-
import geb.*
19+
import org.openqa.selenium.WebDriver;
2020

2121
/**
22-
* The home page
23-
*
24-
* @author Rob Winch
22+
* @author Eddú Meléndez
2523
*/
26-
class HomePage extends Page {
27-
static url = ''
28-
static at = { assert driver.title == 'Spring Session Sample - Secured Content'; true}
29-
static content = {
30-
username { $('#un').text() }
31-
logout(to:LoginPage) { $('input[type=submit]').click() }
24+
public class BasePage {
25+
26+
private WebDriver driver;
27+
28+
public BasePage(WebDriver driver) {
29+
this.driver = driver;
30+
}
31+
32+
public WebDriver getDriver() {
33+
return this.driver;
34+
}
35+
36+
public static void get(WebDriver driver, String get) {
37+
String baseUrl = "http://localhost";
38+
driver.get(baseUrl + get);
3239
}
40+
3341
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2014-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.pages;
18+
19+
import java.util.Set;
20+
21+
import org.openqa.selenium.By;
22+
import org.openqa.selenium.Cookie;
23+
import org.openqa.selenium.WebDriver;
24+
import org.openqa.selenium.WebElement;
25+
import org.openqa.selenium.support.PageFactory;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* @author Eddú Meléndez
31+
*/
32+
public class HomePage extends BasePage {
33+
34+
public HomePage(WebDriver driver) {
35+
super(driver);
36+
}
37+
38+
public static LoginPage go(WebDriver driver) {
39+
get(driver, "/");
40+
return PageFactory.initElements(driver, LoginPage.class);
41+
}
42+
43+
public void assertAt() {
44+
assertThat(getDriver().getTitle()).isEqualTo("Spring Session Sample - Secured Content");
45+
}
46+
47+
public void containCookie(String cookieName) {
48+
Set<Cookie> cookies = getDriver().manage().getCookies();
49+
assertThat(cookies).extracting("name").contains(cookieName);
50+
}
51+
52+
public void doesNotContainCookie(String cookieName) {
53+
Set<Cookie> cookies = getDriver().manage().getCookies();
54+
assertThat(cookies).extracting("name").doesNotContain(cookieName);
55+
}
56+
57+
public HomePage logout() {
58+
WebElement logout = getDriver().findElement(By.cssSelector("input[type=\"submit\"]"));
59+
logout.click();
60+
return PageFactory.initElements(getDriver(), HomePage.class);
61+
}
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2014-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.pages;
18+
19+
import org.openqa.selenium.WebDriver;
20+
import org.openqa.selenium.WebElement;
21+
import org.openqa.selenium.support.FindBy;
22+
import org.openqa.selenium.support.PageFactory;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* @author Eddú Meléndez
28+
*/
29+
public class LoginPage extends BasePage {
30+
31+
@FindBy(name = "username")
32+
private WebElement username;
33+
34+
@FindBy(name = "password")
35+
private WebElement password;
36+
37+
@FindBy(name = "submit")
38+
private WebElement button;
39+
40+
public LoginPage(WebDriver driver) {
41+
super(driver);
42+
}
43+
44+
public void assertAt() {
45+
assertThat(getDriver().getTitle()).isEqualTo("Login Page");
46+
}
47+
48+
public HomePage login() {
49+
this.username.sendKeys("user");
50+
this.password.sendKeys("password");
51+
this.button.click();
52+
return PageFactory.initElements(getDriver(), HomePage.class);
53+
}
54+
}

samples/custom-cookie/build.gradle

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ dependencies {
1414

1515
providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion"
1616

17-
testCompile "junit:junit:$junitVersion"
17+
testCompile "junit:junit:$junitVersion",
18+
"org.assertj:assertj-core:$assertjVersion",
19+
"org.projectlombok:lombok:$lombokVersion"
1820

19-
integrationTestCompile gebDependencies
21+
integrationTestCompile seleniumDependencies
2022
}

0 commit comments

Comments
 (0)