Convert groovy tests to java

This commit is contained in:
Eddú Meléndez
2016-11-21 20:48:11 -05:00
committed by John Blum
parent f98697416e
commit 1a318b89d9
97 changed files with 3029 additions and 1820 deletions

View File

@@ -1,54 +0,0 @@
package samples
import geb.spock.*
import sample.Application
import samples.pages.*
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
/**
* @author jitendra on 15/3/16.
*/
@Stepwise
@ContextConfiguration(classes = Application, loader = SpringApplicationContextLoader)
@WebAppConfiguration
@IntegrationTest
class HttpRedisJsonTest extends GebSpec {
def'login page test'() {
when:
to LoginPage
then:
at LoginPage
}
def"Unauthenticated user sent to login page"() {
when:
via HomePage
then:
at LoginPage
}
def"Successful Login test"() {
when:
login()
then:
at HomePage
driver.manage().cookies.find {it.name == "SESSION"}
!driver.manage().cookies.find {it.name == "JSESSIONID"}
}
def"Set and get attributes in session"() {
when:
setAttribute("Demo Key", "Demo Value")
then:
at SetAttributePage
tdKey()*.text().contains("Demo Key")
tdKey()*.text().contains("Demo Value")
}
}

View File

@@ -1,26 +0,0 @@
package samples.pages
import geb.Page
/**
* @author jitendra on 15/3/16.
*/
class HomePage extends Page {
static url="/"
static at=
{
driver.title == "Spring Session Sample - Home"
}
static content=
{
form { $('form') }
submit { $('button[type=submit]') }
setAttribute(required: false) { key = 'project', value = 'SessionRedisJson' ->
form.key = key
form.value = value
submit.click(SetAttributePage)
}
}
}

View File

@@ -1,27 +0,0 @@
package samples.pages
import geb.Page
/**
* @author jitendra on 15/3/16.
*/
class LoginPage extends Page {
static url="/login"
static at=
{
assert title == "Spring Session Sample - Login"
return true
}
static content=
{
form { $('form') }
submit { $('button[type=submit]') }
login(required: false) { user = 'user', pass = 'password' ->
form.username = user
form.password = pass
submit.click(HomePage)
}
}
}

View File

@@ -1,20 +0,0 @@
package samples.pages
import geb.Page
/**
* @author jitendra on 15/3/16.
*/
class SetAttributePage extends Page {
static url="/setValue"
static at=
{
title == "Spring Session Sample - Home"
}
static content=
{
tdKey { $('td') }
}
}

View File

@@ -0,0 +1,95 @@
/*
* 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 samples;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import sample.Application;
import samples.pages.HomePage;
import samples.pages.LoginPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootContextLoader;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.ContextConfiguration;
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 Eddú Meléndez
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@AutoConfigureMockMvc
@ContextConfiguration(classes = Application.class, loader = SpringBootContextLoader.class)
public class HttpRedisJsonTest {
@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 goLoginRedirectToLogin() {
LoginPage login = LoginPage.go(this.driver);
login.assertAt();
}
@Test
public void goHomeRedirectLoginPage() {
LoginPage login = HomePage.go(this.driver);
login.assertAt();
}
@Test
public void login() {
LoginPage login = LoginPage.go(this.driver);
HomePage home = login.login();
home.containCookie("SESSION");
home.doesNotContainCookie("JSESSIONID");
}
@Test
public void createAttribute() {
LoginPage login = LoginPage.go(this.driver);
login.login();
login.addAttribute("Demo Key", "Demo Value");
assertThat(login.attributes()).extracting("key").contains("Demo Key");
assertThat(login.attributes()).extracting("value").contains("Demo Value");
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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 samples.pages;
import org.openqa.selenium.WebDriver;
/**
* @author Eddú Meléndez
*/
public 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);
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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 samples.pages;
import java.util.Set;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Eddú Meléndez
*/
public class HomePage extends BasePage {
public HomePage(WebDriver driver) {
super(driver);
}
public static LoginPage go(WebDriver driver) {
get(driver, "/");
return PageFactory.initElements(driver, LoginPage.class);
}
public void containCookie(String cookieName) {
Set<Cookie> cookies = getDriver().manage().getCookies();
assertThat(cookies).extracting("name").contains(cookieName);
}
public void doesNotContainCookie(String cookieName) {
Set<Cookie> cookies = getDriver().manage().getCookies();
assertThat(cookies).extracting("name").doesNotContain(cookieName);
}
}

View File

@@ -0,0 +1,105 @@
/*
* 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 samples.pages;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
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 Eddú Meléndez
*/
public class LoginPage extends BasePage {
public LoginPage(WebDriver driver) {
super(driver);
}
public void assertAt() {
assertThat(getDriver().getTitle()).isEqualTo("Spring Session Sample - Login");
}
public static LoginPage go(WebDriver driver) {
get(driver, "/login");
return PageFactory.initElements(driver, LoginPage.class);
}
public HomePage login() {
WebElement username = getDriver().findElement(By.name("username"));
WebElement password = getDriver().findElement(By.name("password"));
WebElement button = getDriver().findElement(By.cssSelector("button[type=\"submit\"]"));
username.sendKeys("user");
password.sendKeys("password");
button.click();
return PageFactory.initElements(getDriver(), HomePage.class);
}
public void addAttribute(String name, String value) {
WebElement form = getDriver().findElement(By.name("f"));
WebElement attributeName = form.findElement(By.name("key"));
WebElement attributeValue = form.findElement(By.name("value"));
attributeName.sendKeys(name);
attributeValue.sendKeys(value);
form.findElement(By.cssSelector("button[type=\"submit\"]")).click();
}
public List<Row> attributes() {
WebElement table = getDriver().findElement(By.tagName("table"));
List<WebElement> trs = table.findElements(By.tagName("tr"));
List<Row> rows = new ArrayList<Row>();
for (WebElement tr : trs) {
List<WebElement> tds = tr.findElements(By.cssSelector("td"));
if (!tds.isEmpty()) {
Row row = Row.builder()
.driver(getDriver())
.key(tds.get(0).getText())
.value(tds.get(1).getText())
.build();
rows.add(row);
}
}
return rows;
}
@Data
@Builder
public static class Row {
final String key;
final String value;
@Getter(AccessLevel.PRIVATE)
final WebDriver driver;
}
}