From d7c29652e7207b8c861dbbb64a737047eed09116 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Thu, 5 Jan 2017 20:26:48 +0100 Subject: [PATCH] Polish contribution --- gradle.properties | 1 - .../java/sample/BootTests.java | 8 +- .../java/sample/pages/LoginPage.java | 43 +++-- samples/custom-cookie/build.gradle | 3 +- .../java/sample/AttributeTests.java | 22 ++- .../java/sample/pages/HomePage.java | 115 ++++++++----- samples/findbyusername/build.gradle | 3 +- .../java/sample/FindByUsernameTests.java | 7 +- .../java/sample/pages/HomePage.java | 19 ++- .../java/sample/pages/LoginPage.java | 41 +++-- .../java/sample/HazelcastSpringTests.java | 3 +- .../java/sample/pages/BasePage.java | 3 +- .../java/sample/pages/LoginPage.java | 42 +++-- samples/hazelcast/build.gradle | 3 +- .../java/sample/AttributeTests.java | 20 ++- .../java/sample/pages/HomePage.java | 115 ++++++++----- samples/httpsession-gemfire-boot/build.gradle | 3 +- .../build.gradle | 3 +- .../build.gradle | 3 +- .../httpsession-gemfire-p2p-xml/build.gradle | 3 +- samples/httpsession-gemfire-p2p/build.gradle | 3 +- samples/httpsession-jdbc-boot/build.gradle | 3 +- .../java/sample/BootTests.java | 10 +- .../java/sample/pages/LoginPage.java | 42 +++-- samples/httpsession-jdbc-xml/build.gradle | 3 +- .../java/sample/AttributeTests.java | 28 +++- .../java/sample/pages/HomePage.java | 117 +++++++++----- samples/httpsession-jdbc/build.gradle | 3 +- .../java/sample/AttributeTests.java | 28 +++- .../java/sample/pages/HomePage.java | 117 +++++++++----- samples/httpsession-redis-json/build.gradle | 3 +- .../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 | 105 ------------ .../src/main/resources/templates/home.html | 20 ++- samples/httpsession-xml/build.gradle | 3 +- .../java/sample/AttributeTests.java | 26 ++- .../java/sample/pages/HomePage.java | 121 +++++++++----- .../httpsession-xml/src/main/webapp/index.jsp | 2 +- samples/httpsession/build.gradle | 3 +- .../java/sample/AttributeTests.java | 28 +++- .../java/sample/pages/HomePage.java | 115 ++++++++----- 46 files changed, 972 insertions(+), 590 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/gradle.properties b/gradle.properties index bc882324..523c1f97 100644 --- a/gradle.properties +++ b/gradle.properties @@ -28,4 +28,3 @@ spockVersion=1.0-groovy-2.4 webjarsTaglibVersion=0.3 jstlVersion=1.2.1 groovyVersion=2.4.4 -lombokVersion=1.16.8 diff --git a/samples/boot/src/integration-test/java/sample/BootTests.java b/samples/boot/src/integration-test/java/sample/BootTests.java index c348db82..0caa9125 100644 --- a/samples/boot/src/integration-test/java/sample/BootTests.java +++ b/samples/boot/src/integration-test/java/sample/BootTests.java @@ -47,9 +47,7 @@ public class BootTests { @Before public void setup() { - this.driver = MockMvcHtmlUnitDriverBuilder - .mockMvcSetup(this.mockMvc) - .build(); + this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build(); } @After @@ -66,7 +64,7 @@ public class BootTests { @Test public void login() { LoginPage login = HomePage.go(this.driver); - HomePage home = login.login(); + HomePage home = login.form().login(HomePage.class); home.assertAt(); home.containCookie("SESSION"); home.doesNotContainCookie("JSESSIONID"); @@ -75,7 +73,7 @@ public class BootTests { @Test public void logout() { LoginPage login = HomePage.go(this.driver); - HomePage home = login.login(); + HomePage home = login.form().login(HomePage.class); home.logout(); login.assertAt(); } diff --git a/samples/boot/src/integration-test/java/sample/pages/LoginPage.java b/samples/boot/src/integration-test/java/sample/pages/LoginPage.java index f4be4d45..e3e89f36 100644 --- a/samples/boot/src/integration-test/java/sample/pages/LoginPage.java +++ b/samples/boot/src/integration-test/java/sample/pages/LoginPage.java @@ -16,27 +16,21 @@ 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 { - @FindBy(name = "username") - private WebElement username; - - @FindBy(name = "password") - private WebElement password; - - @FindBy(name = "submit") - private WebElement button; - public LoginPage(WebDriver driver) { super(driver); } @@ -45,10 +39,31 @@ public class LoginPage extends BasePage { assertThat(getDriver().getTitle()).isEqualTo("Login Page"); } - public HomePage login() { - this.username.sendKeys("user"); - this.password.sendKeys("password"); - this.button.click(); - return PageFactory.initElements(getDriver(), HomePage.class); + public Form form() { + return new Form(getDriver()); } + + public class Form { + + @FindBy(name = "username") + private WebElement username; + + @FindBy(name = "password") + private WebElement password; + + @FindBy(name = "submit") + 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/custom-cookie/build.gradle b/samples/custom-cookie/build.gradle index 41b0360a..2c575350 100644 --- a/samples/custom-cookie/build.gradle +++ b/samples/custom-cookie/build.gradle @@ -15,8 +15,7 @@ dependencies { providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion" testCompile "junit:junit:$junitVersion", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestCompile seleniumDependencies } diff --git a/samples/custom-cookie/src/integration-test/java/sample/AttributeTests.java b/samples/custom-cookie/src/integration-test/java/sample/AttributeTests.java index 191ab84e..97b8342f 100644 --- a/samples/custom-cookie/src/integration-test/java/sample/AttributeTests.java +++ b/samples/custom-cookie/src/integration-test/java/sample/AttributeTests.java @@ -16,17 +16,21 @@ package sample; +import java.util.List; + import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import sample.pages.HomePage; +import sample.pages.HomePage.Attribute; import static org.assertj.core.api.Assertions.assertThat; /** * @author Eddú Meléndez + * @author Rob Winch */ public class AttributeTests { @@ -51,16 +55,24 @@ public class AttributeTests { @Test public void noAttributes() { HomePage home = HomePage.go(this.driver); - assertThat(home.attributes().size()).isEqualTo(0); + assertThat(home.attributes()).isEmpty(); } @Test public void createAttribute() { HomePage home = HomePage.go(this.driver); - home.addAttribute("a", "b"); - assertThat(home.attributes().size()).isEqualTo(1); - assertThat(home.row(0).getAttributeName()).isEqualTo("a"); - assertThat(home.row(0).getAttributeValue()).isEqualTo("b"); + // @formatter:off + home = home.form() + .attributeName("a") + .attributeValue("b") + .submit(HomePage.class); + // @formatter:on + + List attributes = home.attributes(); + assertThat(attributes).hasSize(1); + Attribute row = attributes.get(0); + assertThat(row.getAttributeName()).isEqualTo("a"); + assertThat(row.getAttributeValue()).isEqualTo("b"); } } diff --git a/samples/custom-cookie/src/integration-test/java/sample/pages/HomePage.java b/samples/custom-cookie/src/integration-test/java/sample/pages/HomePage.java index 00e29651..ead76787 100644 --- a/samples/custom-cookie/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/custom-cookie/src/integration-test/java/sample/pages/HomePage.java @@ -19,33 +19,38 @@ package sample.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.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; - private List rows; + @FindBy(css = "form") + WebElement form; + + @FindBy(css = "table tbody tr") + List trs; + + List attributes; public HomePage(WebDriver driver) { this.driver = driver; - this.rows = new ArrayList(); + this.attributes = new ArrayList(); } private static void get(WebDriver driver, String get) { - String baseUrl = "http://localhost:" + System.getProperty("tomcat.port"); + String baseUrl = "http://localhost:" + System.getProperty("tomcat.port", "8080"); driver.get(baseUrl + get); } @@ -58,51 +63,73 @@ public class HomePage { assertThat(this.driver.getTitle()).isEqualTo("Session Attributes"); } - public void addAttribute(String name, String value) { - WebElement form = this.driver.findElement(By.tagName("form")); - WebElement attributeName = form.findElement(By.name("attributeName")); - WebElement attributeValue = form.findElement(By.name("attributeValue")); - - attributeName.sendKeys(name); - attributeValue.sendKeys(value); - - form.findElement(By.cssSelector("input[type=\"submit\"]")).click(); - } - - public List attributes() { - WebElement table = this.driver.findElement(By.tagName("table")); - WebElement tbody = table.findElement(By.tagName("tbody")); - List trs = tbody.findElements(By.tagName("tr")); - - List rows = new ArrayList(); - for (WebElement tr : trs) { - List tds = tr.findElements(By.cssSelector("td")); - Row row = Row.builder() - .driver(this.driver) - .attributeName(tds.get(0).getText()) - .attributeValue(tds.get(1).getText()) - .build(); - rows.add(row); + public List attributes() { + List rows = new ArrayList(); + for (WebElement tr : this.trs) { + rows.add(new Attribute(tr)); } - this.rows.addAll(rows); - return this.rows; + this.attributes.addAll(rows); + return this.attributes; } - public Row row(int index) { - return this.rows.get(index); + public Form form() { + return new Form(this.form); } - @Data - @Builder - public static class Row { + public class Form { + @FindBy(name = "attributeName") + WebElement attributeName; - final String attributeName; + @FindBy(name = "attributeValue") + WebElement attributeValue; - final String attributeValue; + @FindBy(css = "input[type=\"submit\"]") + WebElement submit; - @Getter(AccessLevel.PRIVATE) - final WebDriver driver; + 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/findbyusername/build.gradle b/samples/findbyusername/build.gradle index 6c3898d3..8e46b14f 100644 --- a/samples/findbyusername/build.gradle +++ b/samples/findbyusername/build.gradle @@ -29,8 +29,7 @@ dependencies { "org.apache.httpcomponents:httpclient" testCompile "org.springframework.boot:spring-boot-starter-test", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestCompile seleniumDependencies diff --git a/samples/findbyusername/src/integration-test/java/sample/FindByUsernameTests.java b/samples/findbyusername/src/integration-test/java/sample/FindByUsernameTests.java index cdf6de4c..12ed00bc 100644 --- a/samples/findbyusername/src/integration-test/java/sample/FindByUsernameTests.java +++ b/samples/findbyusername/src/integration-test/java/sample/FindByUsernameTests.java @@ -34,6 +34,7 @@ import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDr /** * @author Eddú Meléndez + * @author Rob Winch */ @RunWith(SpringRunner.class) @AutoConfigureMockMvc @@ -47,9 +48,7 @@ public class FindByUsernameTests { @Before public void setup() { - this.driver = MockMvcHtmlUnitDriverBuilder - .mockMvcSetup(this.mockMvc) - .build(); + this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build(); } @After @@ -66,7 +65,7 @@ public class FindByUsernameTests { @Test public void login() { LoginPage login = HomePage.go(this.driver); - HomePage home = login.login(); + HomePage home = login.form().login(HomePage.class); home.assertAt(); home.containCookie("SESSION"); home.doesNotContainCookie("JSESSIONID"); diff --git a/samples/findbyusername/src/integration-test/java/sample/pages/HomePage.java b/samples/findbyusername/src/integration-test/java/sample/pages/HomePage.java index e7487e29..32522d6e 100644 --- a/samples/findbyusername/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/findbyusername/src/integration-test/java/sample/pages/HomePage.java @@ -22,26 +22,26 @@ import org.openqa.selenium.By; import org.openqa.selenium.Cookie; 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 Eddú Meléndez + * @author Rob Winch */ public class HomePage extends BasePage { + @FindBy(css = "input[type=\"submit\"]") + WebElement logout; public HomePage(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("Spring Session Sample - Secured Content"); + assertThat(getDriver().getTitle()) + .isEqualTo("Spring Session Sample - Secured Content"); } public void containCookie(String cookieName) { @@ -67,9 +67,12 @@ public class HomePage extends BasePage { } public HomePage logout() { - WebElement logout = getDriver().findElement(By.cssSelector("input[type=\"submit\"]")); - logout.click(); + this.logout.click(); return PageFactory.initElements(getDriver(), HomePage.class); } + public static LoginPage go(WebDriver driver) { + get(driver, "/"); + return PageFactory.initElements(driver, LoginPage.class); + } } diff --git a/samples/findbyusername/src/integration-test/java/sample/pages/LoginPage.java b/samples/findbyusername/src/integration-test/java/sample/pages/LoginPage.java index 9c5388d0..16cf5621 100644 --- a/samples/findbyusername/src/integration-test/java/sample/pages/LoginPage.java +++ b/samples/findbyusername/src/integration-test/java/sample/pages/LoginPage.java @@ -16,27 +16,21 @@ 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 { - @FindBy(name = "username") - private WebElement username; - - @FindBy(name = "password") - private WebElement password; - - @FindBy(tagName = "button") - private WebElement button; - public LoginPage(WebDriver driver) { super(driver); } @@ -45,11 +39,30 @@ public class LoginPage extends BasePage { assertThat(getDriver().getTitle()).isEqualTo("Spring Session Sample - Log In"); } - public HomePage login() { - this.username.sendKeys("user"); - this.password.sendKeys("password"); - this.button.click(); - return PageFactory.initElements(getDriver(), HomePage.class); + 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/hazelcast-spring/src/integration-test/java/sample/HazelcastSpringTests.java b/samples/hazelcast-spring/src/integration-test/java/sample/HazelcastSpringTests.java index 44470337..4aeeb045 100644 --- a/samples/hazelcast-spring/src/integration-test/java/sample/HazelcastSpringTests.java +++ b/samples/hazelcast-spring/src/integration-test/java/sample/HazelcastSpringTests.java @@ -26,6 +26,7 @@ import sample.pages.LoginPage; /** * @author Eddú Meléndez + * @author Rob Winch */ public class HazelcastSpringTests { @@ -51,7 +52,7 @@ public class HazelcastSpringTests { public void login() { LoginPage login = HomePage.go(this.driver); login.assertAt(); - HomePage home = login.login(); + HomePage home = login.form().login(HomePage.class); home.containCookie("SESSION"); home.doesNotContainCookie("JSESSIONID"); } diff --git a/samples/hazelcast-spring/src/integration-test/java/sample/pages/BasePage.java b/samples/hazelcast-spring/src/integration-test/java/sample/pages/BasePage.java index a486c3d0..a471078a 100644 --- a/samples/hazelcast-spring/src/integration-test/java/sample/pages/BasePage.java +++ b/samples/hazelcast-spring/src/integration-test/java/sample/pages/BasePage.java @@ -20,6 +20,7 @@ import org.openqa.selenium.WebDriver; /** * @author Eddú Meléndez + * @author Rob Winch */ public class BasePage { @@ -34,7 +35,7 @@ public class BasePage { } public static void get(WebDriver driver, String get) { - String baseUrl = "http://localhost:" + System.getProperty("tomcat.port"); + String baseUrl = "http://localhost:" + System.getProperty("tomcat.port", "8080"); driver.get(baseUrl + get); } diff --git a/samples/hazelcast-spring/src/integration-test/java/sample/pages/LoginPage.java b/samples/hazelcast-spring/src/integration-test/java/sample/pages/LoginPage.java index ab67fcad..e3e89f36 100644 --- a/samples/hazelcast-spring/src/integration-test/java/sample/pages/LoginPage.java +++ b/samples/hazelcast-spring/src/integration-test/java/sample/pages/LoginPage.java @@ -16,27 +16,21 @@ 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 { - @FindBy(name = "username") - private WebElement username; - - @FindBy(name = "password") - private WebElement password; - - @FindBy(name = "submit") - private WebElement button; - public LoginPage(WebDriver driver) { super(driver); } @@ -45,11 +39,31 @@ public class LoginPage extends BasePage { assertThat(getDriver().getTitle()).isEqualTo("Login Page"); } - public HomePage login() { - this.username.sendKeys("user"); - this.password.sendKeys("password"); - this.button.click(); - return PageFactory.initElements(getDriver(), HomePage.class); + public Form form() { + return new Form(getDriver()); + } + + public class Form { + + @FindBy(name = "username") + private WebElement username; + + @FindBy(name = "password") + private WebElement password; + + @FindBy(name = "submit") + 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/hazelcast/build.gradle b/samples/hazelcast/build.gradle index 3d1a8955..2e589514 100644 --- a/samples/hazelcast/build.gradle +++ b/samples/hazelcast/build.gradle @@ -12,8 +12,7 @@ dependencies { providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion" testCompile "junit:junit:$junitVersion", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestCompile seleniumDependencies } diff --git a/samples/hazelcast/src/integration-test/java/sample/AttributeTests.java b/samples/hazelcast/src/integration-test/java/sample/AttributeTests.java index 191ab84e..587c638a 100644 --- a/samples/hazelcast/src/integration-test/java/sample/AttributeTests.java +++ b/samples/hazelcast/src/integration-test/java/sample/AttributeTests.java @@ -16,17 +16,21 @@ package sample; +import java.util.List; + import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import sample.pages.HomePage; +import sample.pages.HomePage.Attribute; import static org.assertj.core.api.Assertions.assertThat; /** * @author Eddú Meléndez + * @author Rob Winch */ public class AttributeTests { @@ -57,10 +61,18 @@ public class AttributeTests { @Test public void createAttribute() { HomePage home = HomePage.go(this.driver); - home.addAttribute("a", "b"); - assertThat(home.attributes().size()).isEqualTo(1); - assertThat(home.row(0).getAttributeName()).isEqualTo("a"); - assertThat(home.row(0).getAttributeValue()).isEqualTo("b"); + // @formatter:off + home = home.form() + .attributeName("a") + .attributeValue("b") + .submit(HomePage.class); + // @formatter:on + + List attributes = home.attributes(); + assertThat(attributes).hasSize(1); + Attribute row = attributes.get(0); + assertThat(row.getAttributeName()).isEqualTo("a"); + assertThat(row.getAttributeValue()).isEqualTo("b"); } } diff --git a/samples/hazelcast/src/integration-test/java/sample/pages/HomePage.java b/samples/hazelcast/src/integration-test/java/sample/pages/HomePage.java index 00e29651..ead76787 100644 --- a/samples/hazelcast/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/hazelcast/src/integration-test/java/sample/pages/HomePage.java @@ -19,33 +19,38 @@ package sample.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.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; - private List rows; + @FindBy(css = "form") + WebElement form; + + @FindBy(css = "table tbody tr") + List trs; + + List attributes; public HomePage(WebDriver driver) { this.driver = driver; - this.rows = new ArrayList(); + this.attributes = new ArrayList(); } private static void get(WebDriver driver, String get) { - String baseUrl = "http://localhost:" + System.getProperty("tomcat.port"); + String baseUrl = "http://localhost:" + System.getProperty("tomcat.port", "8080"); driver.get(baseUrl + get); } @@ -58,51 +63,73 @@ public class HomePage { assertThat(this.driver.getTitle()).isEqualTo("Session Attributes"); } - public void addAttribute(String name, String value) { - WebElement form = this.driver.findElement(By.tagName("form")); - WebElement attributeName = form.findElement(By.name("attributeName")); - WebElement attributeValue = form.findElement(By.name("attributeValue")); - - attributeName.sendKeys(name); - attributeValue.sendKeys(value); - - form.findElement(By.cssSelector("input[type=\"submit\"]")).click(); - } - - public List attributes() { - WebElement table = this.driver.findElement(By.tagName("table")); - WebElement tbody = table.findElement(By.tagName("tbody")); - List trs = tbody.findElements(By.tagName("tr")); - - List rows = new ArrayList(); - for (WebElement tr : trs) { - List tds = tr.findElements(By.cssSelector("td")); - Row row = Row.builder() - .driver(this.driver) - .attributeName(tds.get(0).getText()) - .attributeValue(tds.get(1).getText()) - .build(); - rows.add(row); + public List attributes() { + List rows = new ArrayList(); + for (WebElement tr : this.trs) { + rows.add(new Attribute(tr)); } - this.rows.addAll(rows); - return this.rows; + this.attributes.addAll(rows); + return this.attributes; } - public Row row(int index) { - return this.rows.get(index); + public Form form() { + return new Form(this.form); } - @Data - @Builder - public static class Row { + public class Form { + @FindBy(name = "attributeName") + WebElement attributeName; - final String attributeName; + @FindBy(name = "attributeValue") + WebElement attributeValue; - final String attributeValue; + @FindBy(css = "input[type=\"submit\"]") + WebElement submit; - @Getter(AccessLevel.PRIVATE) - final WebDriver driver; + 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-gemfire-boot/build.gradle b/samples/httpsession-gemfire-boot/build.gradle index 3cf16a63..99a88b75 100644 --- a/samples/httpsession-gemfire-boot/build.gradle +++ b/samples/httpsession-gemfire-boot/build.gradle @@ -28,8 +28,7 @@ dependencies { testCompile "org.springframework.boot:spring-boot-starter-test" - integrationTestCompile seleniumDependencies, - "org.projectlombok:lombok:$lombokVersion" + integrationTestCompile seleniumDependencies integrationTestRuntime "org.springframework.shell:spring-shell:1.0.0.RELEASE" } diff --git a/samples/httpsession-gemfire-clientserver-xml/build.gradle b/samples/httpsession-gemfire-clientserver-xml/build.gradle index 38b07825..23d0be94 100644 --- a/samples/httpsession-gemfire-clientserver-xml/build.gradle +++ b/samples/httpsession-gemfire-clientserver-xml/build.gradle @@ -20,8 +20,7 @@ dependencies { runtime "org.springframework.shell:spring-shell:1.0.0.RELEASE" testCompile "junit:junit:$junitVersion", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestCompile seleniumDependencies diff --git a/samples/httpsession-gemfire-clientserver/build.gradle b/samples/httpsession-gemfire-clientserver/build.gradle index ed125ba3..f4b80911 100644 --- a/samples/httpsession-gemfire-clientserver/build.gradle +++ b/samples/httpsession-gemfire-clientserver/build.gradle @@ -15,8 +15,7 @@ dependencies { runtime "org.springframework.shell:spring-shell:1.0.0.RELEASE" testCompile "junit:junit:$junitVersion", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestCompile seleniumDependencies diff --git a/samples/httpsession-gemfire-p2p-xml/build.gradle b/samples/httpsession-gemfire-p2p-xml/build.gradle index 3278a77e..d2443a35 100644 --- a/samples/httpsession-gemfire-p2p-xml/build.gradle +++ b/samples/httpsession-gemfire-p2p-xml/build.gradle @@ -14,8 +14,7 @@ dependencies { testCompile "junit:junit:$junitVersion" integrationTestCompile seleniumDependencies, - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestRuntime "org.springframework.shell:spring-shell:1.0.0.RELEASE" } diff --git a/samples/httpsession-gemfire-p2p/build.gradle b/samples/httpsession-gemfire-p2p/build.gradle index 3278a77e..d2443a35 100644 --- a/samples/httpsession-gemfire-p2p/build.gradle +++ b/samples/httpsession-gemfire-p2p/build.gradle @@ -14,8 +14,7 @@ dependencies { testCompile "junit:junit:$junitVersion" integrationTestCompile seleniumDependencies, - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestRuntime "org.springframework.shell:spring-shell:1.0.0.RELEASE" } diff --git a/samples/httpsession-jdbc-boot/build.gradle b/samples/httpsession-jdbc-boot/build.gradle index eb7663c3..cc701699 100644 --- a/samples/httpsession-jdbc-boot/build.gradle +++ b/samples/httpsession-jdbc-boot/build.gradle @@ -28,8 +28,7 @@ dependencies { "com.h2database:h2" testCompile "org.springframework.boot:spring-boot-starter-test", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestCompile seleniumDependencies diff --git a/samples/httpsession-jdbc-boot/src/integration-test/java/sample/BootTests.java b/samples/httpsession-jdbc-boot/src/integration-test/java/sample/BootTests.java index 73e27228..8ef19ba9 100644 --- a/samples/httpsession-jdbc-boot/src/integration-test/java/sample/BootTests.java +++ b/samples/httpsession-jdbc-boot/src/integration-test/java/sample/BootTests.java @@ -47,9 +47,7 @@ public class BootTests { @Before public void setup() { - this.driver = MockMvcHtmlUnitDriverBuilder - .mockMvcSetup(this.mockMvc) - .build(); + this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build(); } @After @@ -61,14 +59,14 @@ public class BootTests { public void home() { LoginPage login = HomePage.go(this.driver); login.assertAt(); - HomePage home = login.login(); + HomePage home = login.form().login(HomePage.class); home.assertAt(); } @Test public void login() { LoginPage login = HomePage.go(this.driver); - HomePage home = login.login(); + HomePage home = login.form().login(HomePage.class); home.containCookie("SESSION"); home.doesNotContainCookie("JSESSIONID"); } @@ -76,7 +74,7 @@ public class BootTests { @Test public void logout() { LoginPage login = HomePage.go(this.driver); - HomePage home = login.login(); + HomePage home = login.form().login(HomePage.class); login = home.logout(); login.assertAt(); } diff --git a/samples/httpsession-jdbc-boot/src/integration-test/java/sample/pages/LoginPage.java b/samples/httpsession-jdbc-boot/src/integration-test/java/sample/pages/LoginPage.java index ab67fcad..e3e89f36 100644 --- a/samples/httpsession-jdbc-boot/src/integration-test/java/sample/pages/LoginPage.java +++ b/samples/httpsession-jdbc-boot/src/integration-test/java/sample/pages/LoginPage.java @@ -16,27 +16,21 @@ 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 { - @FindBy(name = "username") - private WebElement username; - - @FindBy(name = "password") - private WebElement password; - - @FindBy(name = "submit") - private WebElement button; - public LoginPage(WebDriver driver) { super(driver); } @@ -45,11 +39,31 @@ public class LoginPage extends BasePage { assertThat(getDriver().getTitle()).isEqualTo("Login Page"); } - public HomePage login() { - this.username.sendKeys("user"); - this.password.sendKeys("password"); - this.button.click(); - return PageFactory.initElements(getDriver(), HomePage.class); + public Form form() { + return new Form(getDriver()); + } + + public class Form { + + @FindBy(name = "username") + private WebElement username; + + @FindBy(name = "password") + private WebElement password; + + @FindBy(name = "submit") + 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-jdbc-xml/build.gradle b/samples/httpsession-jdbc-xml/build.gradle index efb72306..6c418977 100644 --- a/samples/httpsession-jdbc-xml/build.gradle +++ b/samples/httpsession-jdbc-xml/build.gradle @@ -13,8 +13,7 @@ dependencies { providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion" testCompile "junit:junit:$junitVersion", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestCompile seleniumDependencies } diff --git a/samples/httpsession-jdbc-xml/src/integration-test/java/sample/AttributeTests.java b/samples/httpsession-jdbc-xml/src/integration-test/java/sample/AttributeTests.java index 6628f7a3..97b8342f 100644 --- a/samples/httpsession-jdbc-xml/src/integration-test/java/sample/AttributeTests.java +++ b/samples/httpsession-jdbc-xml/src/integration-test/java/sample/AttributeTests.java @@ -16,17 +16,21 @@ package sample; +import java.util.List; + import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import sample.pages.HomePage; +import sample.pages.HomePage.Attribute; import static org.assertj.core.api.Assertions.assertThat; /** * @author Eddú Meléndez + * @author Rob Winch */ public class AttributeTests { @@ -42,19 +46,33 @@ public class AttributeTests { this.driver.quit(); } + @Test + public void home() { + HomePage home = HomePage.go(this.driver); + home.assertAt(); + } + @Test public void noAttributes() { HomePage home = HomePage.go(this.driver); - assertThat(home.attributes().size()).isEqualTo(0); + assertThat(home.attributes()).isEmpty(); } @Test public void createAttribute() { HomePage home = HomePage.go(this.driver); - home.addAttribute("a", "b"); - assertThat(home.attributes().size()).isEqualTo(1); - assertThat(home.row(0).getAttributeName()).isEqualTo("a"); - assertThat(home.row(0).getAttributeValue()).isEqualTo("b"); + // @formatter:off + home = home.form() + .attributeName("a") + .attributeValue("b") + .submit(HomePage.class); + // @formatter:on + + List attributes = home.attributes(); + assertThat(attributes).hasSize(1); + Attribute row = attributes.get(0); + assertThat(row.getAttributeName()).isEqualTo("a"); + assertThat(row.getAttributeValue()).isEqualTo("b"); } } diff --git a/samples/httpsession-jdbc-xml/src/integration-test/java/sample/pages/HomePage.java b/samples/httpsession-jdbc-xml/src/integration-test/java/sample/pages/HomePage.java index dbb01fed..ead76787 100644 --- a/samples/httpsession-jdbc-xml/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/httpsession-jdbc-xml/src/integration-test/java/sample/pages/HomePage.java @@ -19,31 +19,38 @@ package sample.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.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; - private List rows; + @FindBy(css = "form") + WebElement form; + + @FindBy(css = "table tbody tr") + List trs; + + List attributes; public HomePage(WebDriver driver) { this.driver = driver; - this.rows = new ArrayList(); + this.attributes = new ArrayList(); } private static void get(WebDriver driver, String get) { - String baseUrl = "http://localhost:" + System.getProperty("tomcat.port"); + String baseUrl = "http://localhost:" + System.getProperty("tomcat.port", "8080"); driver.get(baseUrl + get); } @@ -52,51 +59,77 @@ public class HomePage { return PageFactory.initElements(driver, HomePage.class); } - public void addAttribute(String name, String value) { - WebElement form = this.driver.findElement(By.tagName("form")); - WebElement attributeName = form.findElement(By.name("attributeName")); - WebElement attributeValue = form.findElement(By.name("attributeValue")); - - attributeName.sendKeys(name); - attributeValue.sendKeys(value); - - form.findElement(By.cssSelector("input[type=\"submit\"]")).click(); + public void assertAt() { + assertThat(this.driver.getTitle()).isEqualTo("Session Attributes"); } - public List attributes() { - WebElement table = this.driver.findElement(By.tagName("table")); - WebElement tbody = table.findElement(By.tagName("tbody")); - List trs = tbody.findElements(By.tagName("tr")); - - List rows = new ArrayList(); - for (WebElement tr : trs) { - List tds = tr.findElements(By.cssSelector("td")); - Row row = Row.builder() - .driver(this.driver) - .attributeName(tds.get(0).getText()) - .attributeValue(tds.get(1).getText()) - .build(); - rows.add(row); + public List attributes() { + List rows = new ArrayList(); + for (WebElement tr : this.trs) { + rows.add(new Attribute(tr)); } - this.rows.addAll(rows); - return this.rows; + this.attributes.addAll(rows); + return this.attributes; } - public Row row(int index) { - return this.rows.get(index); + public Form form() { + return new Form(this.form); } - @Data - @Builder - public static class Row { + public class Form { + @FindBy(name = "attributeName") + WebElement attributeName; - final String attributeName; + @FindBy(name = "attributeValue") + WebElement attributeValue; - final String attributeValue; + @FindBy(css = "input[type=\"submit\"]") + WebElement submit; - @Getter(AccessLevel.PRIVATE) - final WebDriver driver; + 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-jdbc/build.gradle b/samples/httpsession-jdbc/build.gradle index efb72306..6c418977 100644 --- a/samples/httpsession-jdbc/build.gradle +++ b/samples/httpsession-jdbc/build.gradle @@ -13,8 +13,7 @@ dependencies { providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion" testCompile "junit:junit:$junitVersion", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestCompile seleniumDependencies } diff --git a/samples/httpsession-jdbc/src/integration-test/java/sample/AttributeTests.java b/samples/httpsession-jdbc/src/integration-test/java/sample/AttributeTests.java index 6628f7a3..97b8342f 100644 --- a/samples/httpsession-jdbc/src/integration-test/java/sample/AttributeTests.java +++ b/samples/httpsession-jdbc/src/integration-test/java/sample/AttributeTests.java @@ -16,17 +16,21 @@ package sample; +import java.util.List; + import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import sample.pages.HomePage; +import sample.pages.HomePage.Attribute; import static org.assertj.core.api.Assertions.assertThat; /** * @author Eddú Meléndez + * @author Rob Winch */ public class AttributeTests { @@ -42,19 +46,33 @@ public class AttributeTests { this.driver.quit(); } + @Test + public void home() { + HomePage home = HomePage.go(this.driver); + home.assertAt(); + } + @Test public void noAttributes() { HomePage home = HomePage.go(this.driver); - assertThat(home.attributes().size()).isEqualTo(0); + assertThat(home.attributes()).isEmpty(); } @Test public void createAttribute() { HomePage home = HomePage.go(this.driver); - home.addAttribute("a", "b"); - assertThat(home.attributes().size()).isEqualTo(1); - assertThat(home.row(0).getAttributeName()).isEqualTo("a"); - assertThat(home.row(0).getAttributeValue()).isEqualTo("b"); + // @formatter:off + home = home.form() + .attributeName("a") + .attributeValue("b") + .submit(HomePage.class); + // @formatter:on + + List attributes = home.attributes(); + assertThat(attributes).hasSize(1); + Attribute row = attributes.get(0); + assertThat(row.getAttributeName()).isEqualTo("a"); + assertThat(row.getAttributeValue()).isEqualTo("b"); } } diff --git a/samples/httpsession-jdbc/src/integration-test/java/sample/pages/HomePage.java b/samples/httpsession-jdbc/src/integration-test/java/sample/pages/HomePage.java index dbb01fed..ead76787 100644 --- a/samples/httpsession-jdbc/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/httpsession-jdbc/src/integration-test/java/sample/pages/HomePage.java @@ -19,31 +19,38 @@ package sample.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.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; - private List rows; + @FindBy(css = "form") + WebElement form; + + @FindBy(css = "table tbody tr") + List trs; + + List attributes; public HomePage(WebDriver driver) { this.driver = driver; - this.rows = new ArrayList(); + this.attributes = new ArrayList(); } private static void get(WebDriver driver, String get) { - String baseUrl = "http://localhost:" + System.getProperty("tomcat.port"); + String baseUrl = "http://localhost:" + System.getProperty("tomcat.port", "8080"); driver.get(baseUrl + get); } @@ -52,51 +59,77 @@ public class HomePage { return PageFactory.initElements(driver, HomePage.class); } - public void addAttribute(String name, String value) { - WebElement form = this.driver.findElement(By.tagName("form")); - WebElement attributeName = form.findElement(By.name("attributeName")); - WebElement attributeValue = form.findElement(By.name("attributeValue")); - - attributeName.sendKeys(name); - attributeValue.sendKeys(value); - - form.findElement(By.cssSelector("input[type=\"submit\"]")).click(); + public void assertAt() { + assertThat(this.driver.getTitle()).isEqualTo("Session Attributes"); } - public List attributes() { - WebElement table = this.driver.findElement(By.tagName("table")); - WebElement tbody = table.findElement(By.tagName("tbody")); - List trs = tbody.findElements(By.tagName("tr")); - - List rows = new ArrayList(); - for (WebElement tr : trs) { - List tds = tr.findElements(By.cssSelector("td")); - Row row = Row.builder() - .driver(this.driver) - .attributeName(tds.get(0).getText()) - .attributeValue(tds.get(1).getText()) - .build(); - rows.add(row); + public List attributes() { + List rows = new ArrayList(); + for (WebElement tr : this.trs) { + rows.add(new Attribute(tr)); } - this.rows.addAll(rows); - return this.rows; + this.attributes.addAll(rows); + return this.attributes; } - public Row row(int index) { - return this.rows.get(index); + public Form form() { + return new Form(this.form); } - @Data - @Builder - public static class Row { + public class Form { + @FindBy(name = "attributeName") + WebElement attributeName; - final String attributeName; + @FindBy(name = "attributeValue") + WebElement attributeValue; - final String attributeValue; + @FindBy(css = "input[type=\"submit\"]") + WebElement submit; - @Getter(AccessLevel.PRIVATE) - final WebDriver driver; + 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/build.gradle b/samples/httpsession-redis-json/build.gradle index 9d7b1963..5d09768a 100644 --- a/samples/httpsession-redis-json/build.gradle +++ b/samples/httpsession-redis-json/build.gradle @@ -32,8 +32,7 @@ dependencies { "org.apache.httpcomponents:httpclient" testCompile "org.springframework.boot:spring-boot-starter-test", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" testCompile "org.skyscreamer:jsonassert:$jsonassertVersion" integrationTestCompile seleniumDependencies 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 8a4403cb..00000000 --- a/samples/httpsession-redis-json/src/integration-test/java/samples/pages/LoginPage.java +++ /dev/null @@ -1,105 +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 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 attributes() { - WebElement table = getDriver().findElement(By.tagName("table")); - List trs = table.findElements(By.tagName("tr")); - - List rows = new ArrayList(); - for (WebElement tr : trs) { - List 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; - - } - -} diff --git a/samples/httpsession-redis-json/src/main/resources/templates/home.html b/samples/httpsession-redis-json/src/main/resources/templates/home.html index 6c8f667a..78c74c3d 100644 --- a/samples/httpsession-redis-json/src/main/resources/templates/home.html +++ b/samples/httpsession-redis-json/src/main/resources/templates/home.html @@ -19,14 +19,18 @@
- - - - - - - - + + + + + + + + + + + +
Attribute NameAttribute Value
Attribute NameAttribute Value
diff --git a/samples/httpsession-xml/build.gradle b/samples/httpsession-xml/build.gradle index 41b0360a..2c575350 100644 --- a/samples/httpsession-xml/build.gradle +++ b/samples/httpsession-xml/build.gradle @@ -15,8 +15,7 @@ dependencies { providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion" testCompile "junit:junit:$junitVersion", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestCompile seleniumDependencies } diff --git a/samples/httpsession-xml/src/integration-test/java/sample/AttributeTests.java b/samples/httpsession-xml/src/integration-test/java/sample/AttributeTests.java index 6628f7a3..14128609 100644 --- a/samples/httpsession-xml/src/integration-test/java/sample/AttributeTests.java +++ b/samples/httpsession-xml/src/integration-test/java/sample/AttributeTests.java @@ -16,17 +16,21 @@ package sample; +import java.util.List; + import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import sample.pages.HomePage; +import sample.pages.HomePage.Attribute; import static org.assertj.core.api.Assertions.assertThat; /** * @author Eddú Meléndez + * @author Rob Winch */ public class AttributeTests { @@ -44,17 +48,25 @@ public class AttributeTests { @Test public void noAttributes() { - HomePage home = HomePage.go(this.driver); - assertThat(home.attributes().size()).isEqualTo(0); + HomePage home = HomePage.go(this.driver, HomePage.class); + assertThat(home.attributes()).isEmpty(); } @Test public void createAttribute() { - HomePage home = HomePage.go(this.driver); - home.addAttribute("a", "b"); - assertThat(home.attributes().size()).isEqualTo(1); - assertThat(home.row(0).getAttributeName()).isEqualTo("a"); - assertThat(home.row(0).getAttributeValue()).isEqualTo("b"); + HomePage home = HomePage.go(this.driver, HomePage.class); + // @formatter:off + home = home.form() + .attributeName("a") + .attributeValue("b") + .submit(HomePage.class); + // @formatter:on + + List attributes = home.attributes(); + assertThat(attributes).hasSize(1); + Attribute row = attributes.get(0); + assertThat(row.getAttributeName()).isEqualTo("a"); + assertThat(row.getAttributeValue()).isEqualTo("b"); } } diff --git a/samples/httpsession-xml/src/integration-test/java/sample/pages/HomePage.java b/samples/httpsession-xml/src/integration-test/java/sample/pages/HomePage.java index dbb01fed..314334d0 100644 --- a/samples/httpsession-xml/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/httpsession-xml/src/integration-test/java/sample/pages/HomePage.java @@ -19,84 +19,117 @@ package sample.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.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; - private List rows; + @FindBy(css = "form") + WebElement form; + + @FindBy(css = "table tbody tr") + List trs; + + List attributes; public HomePage(WebDriver driver) { this.driver = driver; - this.rows = new ArrayList(); + this.attributes = new ArrayList(); } private static void get(WebDriver driver, String get) { - String baseUrl = "http://localhost:" + System.getProperty("tomcat.port"); + String baseUrl = "http://localhost:" + System.getProperty("tomcat.port", "8080"); driver.get(baseUrl + get); } - public static HomePage go(WebDriver driver) { + public static T go(WebDriver driver, Class page) { get(driver, "/"); - return PageFactory.initElements(driver, HomePage.class); + return PageFactory.initElements(driver, page); } - public void addAttribute(String name, String value) { - WebElement form = this.driver.findElement(By.tagName("form")); - WebElement attributeName = form.findElement(By.name("attributeName")); - WebElement attributeValue = form.findElement(By.name("attributeValue")); - - attributeName.sendKeys(name); - attributeValue.sendKeys(value); - - form.findElement(By.cssSelector("input[type=\"submit\"]")).click(); + public void assertAt() { + assertThat(this.driver.getTitle()).isEqualTo("Session Attributes"); } - public List attributes() { - WebElement table = this.driver.findElement(By.tagName("table")); - WebElement tbody = table.findElement(By.tagName("tbody")); - List trs = tbody.findElements(By.tagName("tr")); - - List rows = new ArrayList(); - for (WebElement tr : trs) { - List tds = tr.findElements(By.cssSelector("td")); - Row row = Row.builder() - .driver(this.driver) - .attributeName(tds.get(0).getText()) - .attributeValue(tds.get(1).getText()) - .build(); - rows.add(row); + public List attributes() { + List rows = new ArrayList(); + for (WebElement tr : this.trs) { + rows.add(new Attribute(tr)); } - this.rows.addAll(rows); - return this.rows; + this.attributes.addAll(rows); + return this.attributes; } - public Row row(int index) { - return this.rows.get(index); + public Form form() { + return new Form(this.form); } - @Data - @Builder - public static class Row { + public class Form { + @FindBy(name = "attributeName") + WebElement attributeName; - final String attributeName; + @FindBy(name = "attributeValue") + WebElement attributeValue; - final String attributeValue; + @FindBy(css = "input[type=\"submit\"]") + WebElement submit; - @Getter(AccessLevel.PRIVATE) - final WebDriver driver; + 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-xml/src/main/webapp/index.jsp b/samples/httpsession-xml/src/main/webapp/index.jsp index 97ac427c..d460c124 100644 --- a/samples/httpsession-xml/src/main/webapp/index.jsp +++ b/samples/httpsession-xml/src/main/webapp/index.jsp @@ -19,7 +19,7 @@

Try it

-
+ diff --git a/samples/httpsession/build.gradle b/samples/httpsession/build.gradle index 41b0360a..2c575350 100644 --- a/samples/httpsession/build.gradle +++ b/samples/httpsession/build.gradle @@ -15,8 +15,7 @@ dependencies { providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion" testCompile "junit:junit:$junitVersion", - "org.assertj:assertj-core:$assertjVersion", - "org.projectlombok:lombok:$lombokVersion" + "org.assertj:assertj-core:$assertjVersion" integrationTestCompile seleniumDependencies } diff --git a/samples/httpsession/src/integration-test/java/sample/AttributeTests.java b/samples/httpsession/src/integration-test/java/sample/AttributeTests.java index 6628f7a3..97b8342f 100644 --- a/samples/httpsession/src/integration-test/java/sample/AttributeTests.java +++ b/samples/httpsession/src/integration-test/java/sample/AttributeTests.java @@ -16,17 +16,21 @@ package sample; +import java.util.List; + import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import sample.pages.HomePage; +import sample.pages.HomePage.Attribute; import static org.assertj.core.api.Assertions.assertThat; /** * @author Eddú Meléndez + * @author Rob Winch */ public class AttributeTests { @@ -42,19 +46,33 @@ public class AttributeTests { this.driver.quit(); } + @Test + public void home() { + HomePage home = HomePage.go(this.driver); + home.assertAt(); + } + @Test public void noAttributes() { HomePage home = HomePage.go(this.driver); - assertThat(home.attributes().size()).isEqualTo(0); + assertThat(home.attributes()).isEmpty(); } @Test public void createAttribute() { HomePage home = HomePage.go(this.driver); - home.addAttribute("a", "b"); - assertThat(home.attributes().size()).isEqualTo(1); - assertThat(home.row(0).getAttributeName()).isEqualTo("a"); - assertThat(home.row(0).getAttributeValue()).isEqualTo("b"); + // @formatter:off + home = home.form() + .attributeName("a") + .attributeValue("b") + .submit(HomePage.class); + // @formatter:on + + List attributes = home.attributes(); + assertThat(attributes).hasSize(1); + Attribute row = attributes.get(0); + assertThat(row.getAttributeName()).isEqualTo("a"); + assertThat(row.getAttributeValue()).isEqualTo("b"); } } diff --git a/samples/httpsession/src/integration-test/java/sample/pages/HomePage.java b/samples/httpsession/src/integration-test/java/sample/pages/HomePage.java index 00e29651..ead76787 100644 --- a/samples/httpsession/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/httpsession/src/integration-test/java/sample/pages/HomePage.java @@ -19,33 +19,38 @@ package sample.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.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; - private List rows; + @FindBy(css = "form") + WebElement form; + + @FindBy(css = "table tbody tr") + List trs; + + List attributes; public HomePage(WebDriver driver) { this.driver = driver; - this.rows = new ArrayList(); + this.attributes = new ArrayList(); } private static void get(WebDriver driver, String get) { - String baseUrl = "http://localhost:" + System.getProperty("tomcat.port"); + String baseUrl = "http://localhost:" + System.getProperty("tomcat.port", "8080"); driver.get(baseUrl + get); } @@ -58,51 +63,73 @@ public class HomePage { assertThat(this.driver.getTitle()).isEqualTo("Session Attributes"); } - public void addAttribute(String name, String value) { - WebElement form = this.driver.findElement(By.tagName("form")); - WebElement attributeName = form.findElement(By.name("attributeName")); - WebElement attributeValue = form.findElement(By.name("attributeValue")); - - attributeName.sendKeys(name); - attributeValue.sendKeys(value); - - form.findElement(By.cssSelector("input[type=\"submit\"]")).click(); - } - - public List attributes() { - WebElement table = this.driver.findElement(By.tagName("table")); - WebElement tbody = table.findElement(By.tagName("tbody")); - List trs = tbody.findElements(By.tagName("tr")); - - List rows = new ArrayList(); - for (WebElement tr : trs) { - List tds = tr.findElements(By.cssSelector("td")); - Row row = Row.builder() - .driver(this.driver) - .attributeName(tds.get(0).getText()) - .attributeValue(tds.get(1).getText()) - .build(); - rows.add(row); + public List attributes() { + List rows = new ArrayList(); + for (WebElement tr : this.trs) { + rows.add(new Attribute(tr)); } - this.rows.addAll(rows); - return this.rows; + this.attributes.addAll(rows); + return this.attributes; } - public Row row(int index) { - return this.rows.get(index); + public Form form() { + return new Form(this.form); } - @Data - @Builder - public static class Row { + public class Form { + @FindBy(name = "attributeName") + WebElement attributeName; - final String attributeName; + @FindBy(name = "attributeValue") + WebElement attributeValue; - final String attributeValue; + @FindBy(css = "input[type=\"submit\"]") + WebElement submit; - @Getter(AccessLevel.PRIVATE) - final WebDriver driver; + 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(); + } } }