diff --git a/samples/httpsession-gemfire-boot/src/integration-test/java/sample/AttributeTests.java b/samples/httpsession-gemfire-boot/src/integration-test/java/sample/AttributeTests.java index e89e2ba0..0a5b36d8 100644 --- a/samples/httpsession-gemfire-boot/src/integration-test/java/sample/AttributeTests.java +++ b/samples/httpsession-gemfire-boot/src/integration-test/java/sample/AttributeTests.java @@ -16,6 +16,8 @@ package sample; +import java.util.List; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -23,13 +25,12 @@ import org.junit.runner.RunWith; import org.openqa.selenium.WebDriver; import sample.client.Application; import sample.pages.HomePage; +import sample.pages.HomePage.Attribute; 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; @@ -38,11 +39,11 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author Eddú Meléndez + * @author Rob Winch */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.MOCK) +@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.MOCK) @AutoConfigureMockMvc -@ContextConfiguration(classes = Application.class, loader = SpringBootContextLoader.class) public class AttributeTests { @Autowired @@ -62,21 +63,34 @@ 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(2); - assertThat(home.row(0).getAttributeName()).isEqualTo("requestCount"); - assertThat(home.row(0).getAttributeValue()).isEqualTo("1"); - assertThat(home.row(1).getAttributeName()).isEqualTo("a"); - assertThat(home.row(1).getAttributeValue()).isEqualTo("b"); + // @formatter:off + home = home.form() + .attributeName("a") + .attributeValue("b") + .submit(HomePage.class); + // @formatter:on + + List attributes = home.attributes(); + assertThat(attributes).hasSize(2); + assertThat(attributes.get(0).getAttributeName()).isEqualTo("requestCount"); + assertThat(attributes.get(0).getAttributeValue()).isEqualTo("1"); + assertThat(attributes.get(1).getAttributeName()).isEqualTo("a"); + assertThat(attributes.get(1).getAttributeValue()).isEqualTo("b"); } } diff --git a/samples/httpsession-gemfire-boot/src/integration-test/java/sample/pages/HomePage.java b/samples/httpsession-gemfire-boot/src/integration-test/java/sample/pages/HomePage.java index 8d3442f4..7b177c82 100644 --- a/samples/httpsession-gemfire-boot/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/httpsession-gemfire-boot/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"; + 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-gemfire-clientserver-xml/src/integration-test/java/sample/AttributeTests.java b/samples/httpsession-gemfire-clientserver-xml/src/integration-test/java/sample/AttributeTests.java index 6628f7a3..97b8342f 100644 --- a/samples/httpsession-gemfire-clientserver-xml/src/integration-test/java/sample/AttributeTests.java +++ b/samples/httpsession-gemfire-clientserver-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-gemfire-clientserver-xml/src/integration-test/java/sample/pages/HomePage.java b/samples/httpsession-gemfire-clientserver-xml/src/integration-test/java/sample/pages/HomePage.java index dbb01fed..ead76787 100644 --- a/samples/httpsession-gemfire-clientserver-xml/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/httpsession-gemfire-clientserver-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-gemfire-clientserver/src/integration-test/java/sample/AttributeTests.java b/samples/httpsession-gemfire-clientserver/src/integration-test/java/sample/AttributeTests.java index 6628f7a3..97b8342f 100644 --- a/samples/httpsession-gemfire-clientserver/src/integration-test/java/sample/AttributeTests.java +++ b/samples/httpsession-gemfire-clientserver/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-gemfire-clientserver/src/integration-test/java/sample/pages/HomePage.java b/samples/httpsession-gemfire-clientserver/src/integration-test/java/sample/pages/HomePage.java index dbb01fed..ead76787 100644 --- a/samples/httpsession-gemfire-clientserver/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/httpsession-gemfire-clientserver/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-gemfire-p2p-xml/src/integration-test/java/sample/AttributeTests.java b/samples/httpsession-gemfire-p2p-xml/src/integration-test/java/sample/AttributeTests.java index 6628f7a3..97b8342f 100644 --- a/samples/httpsession-gemfire-p2p-xml/src/integration-test/java/sample/AttributeTests.java +++ b/samples/httpsession-gemfire-p2p-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-gemfire-p2p-xml/src/integration-test/java/sample/pages/HomePage.java b/samples/httpsession-gemfire-p2p-xml/src/integration-test/java/sample/pages/HomePage.java index dbb01fed..ead76787 100644 --- a/samples/httpsession-gemfire-p2p-xml/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/httpsession-gemfire-p2p-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-gemfire-p2p/src/integration-test/java/sample/AttributeTests.java b/samples/httpsession-gemfire-p2p/src/integration-test/java/sample/AttributeTests.java index 6628f7a3..97b8342f 100644 --- a/samples/httpsession-gemfire-p2p/src/integration-test/java/sample/AttributeTests.java +++ b/samples/httpsession-gemfire-p2p/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-gemfire-p2p/src/integration-test/java/sample/pages/HomePage.java b/samples/httpsession-gemfire-p2p/src/integration-test/java/sample/pages/HomePage.java index dbb01fed..ead76787 100644 --- a/samples/httpsession-gemfire-p2p/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/httpsession-gemfire-p2p/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(); + } } }