Apply POM to Tables

This commit is contained in:
Ramon Caballero 2024-07-25 17:37:23 +01:00
parent 702348b16a
commit c018aee877
3 changed files with 90 additions and 52 deletions

View File

@ -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<WebElement> rows;
List<WebElement> 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<WebElement> cellsInRow = row.findElements(By.xpath("td"));
String rowAsString = cellsInRow.stream().map(WebElement::getText).collect(Collectors.joining(", "));
rowsAsString += rowAsString + '\n';
}
return rowsAsString;
}
}

View File

@ -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();
}
}

View File

@ -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<WebElement> rows = driver.findElements(By.xpath(".//table[@id='products-batman']/descendant::*/tr"));
// Take the cells of the 1st row (header row):
List<WebElement> 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<WebElement> 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();
}
}