Apply POM to Tables
This commit is contained in:
parent
702348b16a
commit
c018aee877
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue