From 7dc3e12e07f235a88daa72fe2369d95018fad3d8 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Mon, 9 Jan 2017 12:07:13 -0600 Subject: [PATCH] Polish httpsession-redis-json --- .../HttpRedisJsonTest.java | 42 ++--- .../RedisSerializerTest.java | 7 +- .../{samples => sample}/pages/BasePage.java | 2 +- .../java/sample/pages/HomePage.java | 151 ++++++++++++++++++ .../java/sample/pages/LoginPage.java | 69 ++++++++ .../java/samples/pages/HomePage.java | 51 ------ .../java/samples/pages/LoginPage.java | 110 ------------- 7 files changed, 247 insertions(+), 185 deletions(-) rename samples/httpsession-redis-json/src/integration-test/java/{samples => sample}/HttpRedisJsonTest.java (67%) rename samples/httpsession-redis-json/src/integration-test/java/{samples => sample}/RedisSerializerTest.java (90%) rename samples/httpsession-redis-json/src/integration-test/java/{samples => sample}/pages/BasePage.java (97%) create mode 100644 samples/httpsession-redis-json/src/integration-test/java/sample/pages/HomePage.java create mode 100644 samples/httpsession-redis-json/src/integration-test/java/sample/pages/LoginPage.java delete mode 100644 samples/httpsession-redis-json/src/integration-test/java/samples/pages/HomePage.java delete mode 100644 samples/httpsession-redis-json/src/integration-test/java/samples/pages/LoginPage.java diff --git a/samples/httpsession-redis-json/src/integration-test/java/samples/HttpRedisJsonTest.java b/samples/httpsession-redis-json/src/integration-test/java/sample/HttpRedisJsonTest.java similarity index 67% rename from samples/httpsession-redis-json/src/integration-test/java/samples/HttpRedisJsonTest.java rename to samples/httpsession-redis-json/src/integration-test/java/sample/HttpRedisJsonTest.java index 02ab3736..d777fc34 100644 --- a/samples/httpsession-redis-json/src/integration-test/java/samples/HttpRedisJsonTest.java +++ b/samples/httpsession-redis-json/src/integration-test/java/sample/HttpRedisJsonTest.java @@ -14,23 +14,23 @@ * limitations under the License. */ -package samples; +package sample; + +import java.util.List; 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 sample.pages.HomePage; +import sample.pages.HomePage.Attribute; +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.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; @@ -43,7 +43,6 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.MOCK) @AutoConfigureMockMvc -@ContextConfiguration(classes = Application.class, loader = SpringBootContextLoader.class) public class HttpRedisJsonTest { @Autowired @@ -53,9 +52,7 @@ public class HttpRedisJsonTest { @Before public void setup() { - this.driver = MockMvcHtmlUnitDriverBuilder - .mockMvcSetup(this.mockMvc) - .build(); + this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build(); } @After @@ -65,31 +62,38 @@ public class HttpRedisJsonTest { @Test public void goLoginRedirectToLogin() { - LoginPage login = LoginPage.go(this.driver); + LoginPage login = HomePage.go(this.driver, LoginPage.class); login.assertAt(); } @Test public void goHomeRedirectLoginPage() { - LoginPage login = HomePage.go(this.driver); + LoginPage login = HomePage.go(this.driver, LoginPage.class); login.assertAt(); } @Test public void login() { - LoginPage login = LoginPage.go(this.driver); - HomePage home = login.login(); + LoginPage login = HomePage.go(this.driver, LoginPage.class); + HomePage home = login.form().login(HomePage.class); 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"); + LoginPage login = HomePage.go(this.driver, LoginPage.class); + HomePage home = login.form().login(HomePage.class); + // @formatter:off + home = home.form() + .attributeName("Demo Key") + .attributeValue("Demo Value") + .submit(HomePage.class); + // @formatter:on + + List attributes = home.attributes(); + assertThat(attributes).extracting("attributeName").contains("Demo Key"); + assertThat(attributes).extracting("attributeValue").contains("Demo Value"); } } diff --git a/samples/httpsession-redis-json/src/integration-test/java/samples/RedisSerializerTest.java b/samples/httpsession-redis-json/src/integration-test/java/sample/RedisSerializerTest.java similarity index 90% rename from samples/httpsession-redis-json/src/integration-test/java/samples/RedisSerializerTest.java rename to samples/httpsession-redis-json/src/integration-test/java/sample/RedisSerializerTest.java index 0dc68f22..faf0f659 100644 --- a/samples/httpsession-redis-json/src/integration-test/java/samples/RedisSerializerTest.java +++ b/samples/httpsession-redis-json/src/integration-test/java/sample/RedisSerializerTest.java @@ -13,14 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package samples; +package sample; import org.junit.Test; import org.junit.runner.RunWith; -import sample.Application; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -31,7 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author jitendra on 8/3/16. */ @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(Application.class) +@SpringBootTest public class RedisSerializerTest { @Autowired diff --git a/samples/httpsession-redis-json/src/integration-test/java/samples/pages/BasePage.java b/samples/httpsession-redis-json/src/integration-test/java/sample/pages/BasePage.java similarity index 97% rename from samples/httpsession-redis-json/src/integration-test/java/samples/pages/BasePage.java rename to samples/httpsession-redis-json/src/integration-test/java/sample/pages/BasePage.java index a8d2859a..763c0787 100644 --- a/samples/httpsession-redis-json/src/integration-test/java/samples/pages/BasePage.java +++ b/samples/httpsession-redis-json/src/integration-test/java/sample/pages/BasePage.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package samples.pages; +package sample.pages; import org.openqa.selenium.WebDriver; diff --git a/samples/httpsession-redis-json/src/integration-test/java/sample/pages/HomePage.java b/samples/httpsession-redis-json/src/integration-test/java/sample/pages/HomePage.java new file mode 100644 index 00000000..77332404 --- /dev/null +++ b/samples/httpsession-redis-json/src/integration-test/java/sample/pages/HomePage.java @@ -0,0 +1,151 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import org.openqa.selenium.By; +import org.openqa.selenium.Cookie; +import org.openqa.selenium.SearchContext; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; +import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Eddú Meléndez + * @author Rob Winch + */ +public class HomePage { + + private WebDriver driver; + + @FindBy(css = "form[name=\"f\"]") + WebElement form; + + @FindBy(css = "table tbody tr") + List trs; + + List attributes; + + public HomePage(WebDriver driver) { + this.driver = driver; + this.attributes = new ArrayList(); + } + + private static void get(WebDriver driver, String get) { + String baseUrl = "http://localhost:" + System.getProperty("tomcat.port", "8080"); + driver.get(baseUrl + get); + } + + public static T go(WebDriver driver, Class page) { + get(driver, "/"); + return PageFactory.initElements(driver, page); + } + + public void containCookie(String cookieName) { + Set cookies = this.driver.manage().getCookies(); + assertThat(cookies).extracting("name").contains(cookieName); + } + + public void doesNotContainCookie(String cookieName) { + Set cookies = this.driver.manage().getCookies(); + assertThat(cookies).extracting("name").doesNotContain(cookieName); + } + + public HomePage logout() { + WebElement logout = this.driver + .findElement(By.cssSelector("input[type=\"submit\"]")); + logout.click(); + return PageFactory.initElements(this.driver, HomePage.class); + } + + public List attributes() { + List rows = new ArrayList(); + for (WebElement tr : this.trs) { + rows.add(new Attribute(tr)); + } + this.attributes.addAll(rows); + return this.attributes; + } + + public Form form() { + return new Form(this.form); + } + + public class Form { + @FindBy(name = "key") + WebElement attributeName; + + @FindBy(name = "value") + WebElement attributeValue; + + @FindBy(css = "button[type=\"submit\"]") + WebElement submit; + + public Form(SearchContext context) { + PageFactory.initElements(new DefaultElementLocatorFactory(context), this); + } + + public Form attributeName(String text) { + this.attributeName.sendKeys(text); + return this; + } + + public Form attributeValue(String text) { + this.attributeValue.sendKeys(text); + return this; + } + + public T submit(Class page) { + this.submit.click(); + return PageFactory.initElements(HomePage.this.driver, page); + } + } + + public static class Attribute { + @FindBy(xpath = "//td[1]") + WebElement attributeName; + + @FindBy(xpath = "//td[2]") + WebElement attributeValue; + + public Attribute(SearchContext context) { + PageFactory.initElements(new DefaultElementLocatorFactory(context), this); + } + + /** + * @return the attributeName + */ + public String getAttributeName() { + return this.attributeName.getText(); + } + + /** + * @return the attributeValue + */ + public String getAttributeValue() { + return this.attributeValue.getText(); + } + } + +} diff --git a/samples/httpsession-redis-json/src/integration-test/java/sample/pages/LoginPage.java b/samples/httpsession-redis-json/src/integration-test/java/sample/pages/LoginPage.java new file mode 100644 index 00000000..18859c68 --- /dev/null +++ b/samples/httpsession-redis-json/src/integration-test/java/sample/pages/LoginPage.java @@ -0,0 +1,69 @@ +/* + * 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.SearchContext; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; +import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Eddú Meléndez + * @author Rob Winch + */ +public class LoginPage extends BasePage { + + public LoginPage(WebDriver driver) { + super(driver); + } + + public void assertAt() { + assertThat(getDriver().getTitle()).isEqualTo("Spring Session Sample - Login"); + } + + public Form form() { + return new Form(getDriver()); + } + + public class Form { + + @FindBy(name = "username") + private WebElement username; + + @FindBy(name = "password") + private WebElement password; + + @FindBy(tagName = "button") + private WebElement button; + + public Form(SearchContext context) { + PageFactory.initElements(new DefaultElementLocatorFactory(context), this); + } + + public T login(Class page) { + this.username.sendKeys("user"); + this.password.sendKeys("password"); + this.button.click(); + return PageFactory.initElements(getDriver(), page); + } + } + +} diff --git a/samples/httpsession-redis-json/src/integration-test/java/samples/pages/HomePage.java b/samples/httpsession-redis-json/src/integration-test/java/samples/pages/HomePage.java deleted file mode 100644 index 092a54e2..00000000 --- a/samples/httpsession-redis-json/src/integration-test/java/samples/pages/HomePage.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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 cookies = getDriver().manage().getCookies(); - assertThat(cookies).extracting("name").contains(cookieName); - } - - public void doesNotContainCookie(String cookieName) { - Set cookies = getDriver().manage().getCookies(); - assertThat(cookies).extracting("name").doesNotContain(cookieName); - } - -} diff --git a/samples/httpsession-redis-json/src/integration-test/java/samples/pages/LoginPage.java b/samples/httpsession-redis-json/src/integration-test/java/samples/pages/LoginPage.java deleted file mode 100644 index 265c6046..00000000 --- a/samples/httpsession-redis-json/src/integration-test/java/samples/pages/LoginPage.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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 org.openqa.selenium.By; -import org.openqa.selenium.SearchContext; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.support.FindBy; -import org.openqa.selenium.support.PageFactory; -import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory; - -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 attributes() { - List trs = getDriver().findElements(By.cssSelector("table tbody tr")); - - List rows = new ArrayList(); - for (WebElement tr : trs) { - rows.add(new Row(tr)); - } - return rows; - } - - public static class Row { - @FindBy(css = "td:text") - String key; - - @FindBy(css = "td:last.text()") - String value; - - public Row(SearchContext context) { - super(); - DefaultElementLocatorFactory factory = new DefaultElementLocatorFactory(context); - PageFactory.initElements(factory, this); - } - - /** - * @return the key - */ - public String getKey() { - return this.key; - } - - /** - * @return the value - */ - public String getValue() { - return this.value; - } - } - -}