diff --git a/src/pages/webelements/TablesPage.java b/src/pages/webelements/TablesPage.java new file mode 100644 index 0000000..a0d7876 --- /dev/null +++ b/src/pages/webelements/TablesPage.java @@ -0,0 +1,57 @@ +package pages.webelements; + +import java.util.List; +import java.util.stream.Collectors; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +public class TablesPage +{ + @FindBy(xpath = "//button[contains(text(),'Tables')]") + private WebElement accordionItem; + + @FindBy(xpath = ".//table[@id='products-batman']/descendant::*/tr") + private List rows; + + List headerRow; + + private WebDriver driver = null; + + public TablesPage(WebDriver driver) + { + this.driver = driver; + PageFactory.initElements(this.driver, this); + + // Take the cells of the 1st row (header row): + headerRow = rows.remove(0).findElements(By.xpath("th")); + } + + public void clickOnAccordionItem() + { + accordionItem.click(); + } + + public String batmanProductsTableHeaderAsString() + { + return headerRow.stream().map(WebElement::getText).collect(Collectors.joining(", ")); + } + + public String batmanProductsTableRowsAsString() + { + String rowsAsString = ""; + + for (WebElement row : rows) + { + List cellsInRow = row.findElements(By.xpath("td")); + + String rowAsString = cellsInRow.stream().map(WebElement::getText).collect(Collectors.joining(", ")); + rowsAsString += rowAsString + '\n'; + } + + return rowsAsString; + } +} diff --git a/src/tests/webelements/TablesTests.java b/src/tests/webelements/TablesTests.java new file mode 100644 index 0000000..971907e --- /dev/null +++ b/src/tests/webelements/TablesTests.java @@ -0,0 +1,33 @@ +package tests.webelements; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; + +import pages.webelements.TablesPage; + +public class TablesTests +{ + public static void main(String[] args) + { + // Specify path to WebDriver: + System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver"); + + // Launch browser and navigate to test page: + WebDriver driver = new FirefoxDriver(); + driver.manage().window().maximize(); + driver.get("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/"); + + // Instantiate the page model: + TablesPage page = new TablesPage(driver); + + // Perform actions on the page: + page.clickOnAccordionItem(); + System.out.println("Batman Products Table Header: " + page.batmanProductsTableHeaderAsString()); + System.out.println("Batman Products Table Rows: " + page.batmanProductsTableRowsAsString()); + + // + + // This is commented out so you can actually see what happened in the web page: + // driver.quit(); + } +} diff --git a/src/webelements/Tables.java b/src/webelements/Tables.java deleted file mode 100644 index 4004c30..0000000 --- a/src/webelements/Tables.java +++ /dev/null @@ -1,52 +0,0 @@ -package webelements; - -import java.util.List; -import java.util.stream.Collectors; - -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.firefox.FirefoxDriver; - -public class Tables -{ - public static void main(String[] args) - { - // Specify path to WebDriver: - System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver"); - - // Launch browser and navigate to test page: - WebDriver driver = new FirefoxDriver(); - driver.manage().window().maximize(); - driver.get("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/"); - - // Click on the 'Tables' accordion item: - driver.findElement(By.xpath("//button[contains(text(),'Tables')]")).click(); - - // - - // Obtain the rows in the 'products-batman' table: - List rows = driver.findElements(By.xpath(".//table[@id='products-batman']/descendant::*/tr")); - - // Take the cells of the 1st row (header row): - List headerRow = rows.remove(0).findElements(By.xpath("th")); - - // Print the table header cells: - String headerString = headerRow.stream().map(WebElement::getText).collect(Collectors.joining(", ")); - System.out.println("Header: " + headerString); - - // Print table body cells: - for (WebElement row : rows) - { - List cellsInRow = row.findElements(By.xpath("td")); - - String rowString = cellsInRow.stream().map(WebElement::getText).collect(Collectors.joining(", ")); - System.out.println("Row: " + rowString); - } - - // - - // This is commented out so you can actually see what happened in the web page: - // driver.quit(); - } -}